Krita Source Code Documentation
Loading...
Searching...
No Matches
debugger.py
Go to the documentation of this file.
1"""
2SPDX-FileCopyrightText: 2017 Eliakin Costa <eliakim170@gmail.com>
3
4SPDX-License-Identifier: GPL-2.0-or-later
5"""
6import bdb
7import multiprocessing
8import asyncio
9from . import debuggerformatter
10
11
12class Debugger(bdb.Bdb):
13
14 def __init__(self, scripter, cmd):
15 bdb.Bdb.__init__(self)
16
17 self.quit = False
18 self.debugq = multiprocessing.Queue()
19 self.scripter = scripter
20 self.applicationq = multiprocessing.Queue()
21 self.filePath = self.scripter.documentcontroller.activeDocument.filePath
24 self.debugprocess = multiprocessing.Process(target=self._run, args=(self.filePath,))
25 self.currentLine = 0
26
27 bdb.Bdb.reset(self)
28
29 def _run(self, filename):
30 try:
31 self.mainpyfile = self.canonic(filename)
32 with open(filename, "rb") as fp:
33 statement = "exec(compile(%r, %r, 'exec'))" % \
34 (fp.read(), self.mainpyfile)
35 self.run(statement)
36 except Exception as e:
37 raise e
38
39 def user_call(self, frame, args):
40 name = frame.f_code.co_name or "<unknown>"
41
42 def user_line(self, frame):
43 """Handler that executes with every line of code"""
44 co = frame.f_code
45
46 if self.filePath != co.co_filename:
47 return
48
49 self.currentLine = frame.f_lineno
50 self.applicationq.put({"code": {"file": co.co_filename,
51 "name": co.co_name,
52 "lineNumber": str(frame.f_lineno)
53 },
54 "frame": {"firstLineNumber": co.co_firstlineno,
55 "locals": debuggerformatter.format_data(frame.f_locals),
56 "globals": debuggerformatter.format_data(frame.f_globals)
57 },
58 "trace": "line"
59 })
60
61 if self.quit:
62 return self.set_quit()
63 if self.currentLine == 0:
64 return
65 else:
66 cmd = self.debugq.get()
67
68 if cmd == "step":
69 return
70 if cmd == "stop":
71 return self.set_quit()
72
73 def user_return(self, frame, value):
74 name = frame.f_code.co_name or "<unknown>"
75
76 if name == '<module>':
77 self.applicationq.put({"quit": True})
78
79 def user_exception(self, frame, exception):
80 self.applicationq.put({"exception": str(exception[1])})
81
82 async def display(self):
83 """Coroutine for updating the UI"""
84
85 while True:
86 if self.applicationq.empty():
87 await asyncio.sleep(0.3)
88 else:
89 while not self.applicationq.empty():
90 self.application_data.update(self.applicationq.get())
91 self.scripter.uicontroller.repaintDebugArea()
92 return
93
94 async def start(self):
95 await self.display()
96
97 async def step(self):
98 self.debugq.put("step")
99 await self.display()
100
101 async def stop(self):
102 self.debugq.put("stop")
103 self.applicationq.put({"quit": True})
104 await self.display()
VertexDescriptor get(PredecessorMap const &m, VertexDescriptor v)
void put(PredecessorMap &m, VertexDescriptor key, VertexDescriptor value)
user_exception(self, frame, exception)
Definition debugger.py:79