Sat, 26 Apr 2025 12:34:32 +0200
MicroPython
- Added a configuration option to disable the support for the no longer produced Pimoroni Pico Wireless Pack.
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
1 | # -*- coding: utf-8 -*- |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
2 | |
11090
f5f5f5803935
Updated copyright for 2025.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
3 | # Copyright (c) 2014 - 2025 Detlev Offenbach <detlev@die-offenbachs.de> |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
4 | # |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
5 | |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
6 | """ |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
7 | Module implementing an import hook patching thread modules to get debugged too. |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
8 | """ |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
9 | |
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
10 | import _thread |
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
11 | import contextlib |
7376
21df384d6150
Added some inspirations received from Tobias.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7364
diff
changeset
|
12 | import os |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
13 | import sys |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
14 | import threading |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
15 | |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
16 | from DebugBase import DebugBase |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
17 | |
5552
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset
|
18 | _qtThreadNumber = 1 |
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset
|
19 | |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
20 | |
8207
d359172d11be
Applied some more code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7986
diff
changeset
|
21 | class ThreadExtension: |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
22 | """ |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
23 | Class implementing the thread support for the debugger. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
24 | |
7390
052ce4cf06c6
Added a todo comment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7376
diff
changeset
|
25 | Provides methods for intercepting thread creation, retrieving the running |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
26 | threads and their name and state. |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
27 | """ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
28 | |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
29 | def __init__(self): |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
30 | """ |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
31 | Constructor |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
32 | """ |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
33 | self.threadNumber = 1 |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
34 | self._original_start_new_thread = None |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
35 | |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
36 | self.clientLock = threading.RLock() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
37 | |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
38 | # dictionary of all threads running {id: DebugBase} |
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset
|
39 | self.threads = {_thread.get_ident(): self} |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
40 | |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
41 | # the "current" thread, basically for variables view |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
42 | self.currentThread = self |
5221
960afd19c1b6
Give the next debugger command to the thread where we are stopped at the moment.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5209
diff
changeset
|
43 | # the thread we are at a breakpoint continuing at next command |
960afd19c1b6
Give the next debugger command to the thread where we are stopped at the moment.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5209
diff
changeset
|
44 | self.currentThreadExec = self |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
45 | |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
46 | # special objects representing the main scripts thread and frame |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
47 | self.mainThread = self |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
48 | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
49 | def attachThread(self, target=None, args=None, kwargs=None, mainThread=False): |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
50 | """ |
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset
|
51 | Public method to setup a standard thread for DebugClient to debug. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
52 | |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
53 | If mainThread is True, then we are attaching to the already |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
54 | started mainthread of the app and the rest of the args are ignored. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
55 | |
10417
c6011e501282
Modernized some code and converted Debug Client and Debugger source code documentation to the new style.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10293
diff
changeset
|
56 | @param target start function of the target thread (i.e. the user |
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset
|
57 | code) |
10417
c6011e501282
Modernized some code and converted Debug Client and Debugger source code documentation to the new style.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10293
diff
changeset
|
58 | @type function |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
59 | @param args arguments to pass to target |
10417
c6011e501282
Modernized some code and converted Debug Client and Debugger source code documentation to the new style.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10293
diff
changeset
|
60 | @type list of Any |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
61 | @param kwargs keyword arguments to pass to target |
10417
c6011e501282
Modernized some code and converted Debug Client and Debugger source code documentation to the new style.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10293
diff
changeset
|
62 | @type dict of Any |
c6011e501282
Modernized some code and converted Debug Client and Debugger source code documentation to the new style.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10293
diff
changeset
|
63 | @param mainThread True, if we are attaching to the already started |
c6011e501282
Modernized some code and converted Debug Client and Debugger source code documentation to the new style.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10293
diff
changeset
|
64 | mainthread of the app |
c6011e501282
Modernized some code and converted Debug Client and Debugger source code documentation to the new style.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10293
diff
changeset
|
65 | @type bool |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
66 | @return identifier of the created thread |
10417
c6011e501282
Modernized some code and converted Debug Client and Debugger source code documentation to the new style.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10293
diff
changeset
|
67 | @rtype int |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
68 | """ |
5651
982465f8389c
Fixed a few code style issues related to the usage of mutable types for default arguments.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
5647
diff
changeset
|
69 | if kwargs is None: |
982465f8389c
Fixed a few code style issues related to the usage of mutable types for default arguments.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
5647
diff
changeset
|
70 | kwargs = {} |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
71 | |
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset
|
72 | if mainThread: |
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset
|
73 | ident = _thread.get_ident() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
74 | name = "MainThread" |
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset
|
75 | newThread = self.mainThread |
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset
|
76 | newThread.isMainThread = True |
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset
|
77 | if self.debugging: |
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset
|
78 | sys.setprofile(newThread.profile) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
79 | |
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset
|
80 | else: |
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset
|
81 | newThread = DebugBase(self) |
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset
|
82 | ident = self._original_start_new_thread( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
83 | newThread.bootstrap, (target, args, kwargs) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
84 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
85 | name = "Thread-{0}".format(self.threadNumber) |
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset
|
86 | self.threadNumber += 1 |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
87 | |
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset
|
88 | newThread.id = ident |
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset
|
89 | newThread.name = name |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
90 | |
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset
|
91 | self.threads[ident] = newThread |
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset
|
92 | |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
93 | return ident |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
94 | |
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset
|
95 | def threadTerminated(self, threadId): |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
96 | """ |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
97 | Public method called when a DebugThread has exited. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
98 | |
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset
|
99 | @param threadId id of the DebugThread that has exited |
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset
|
100 | @type int |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
101 | """ |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
102 | self.lockClient() |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
103 | try: |
8240
93b8a353c4bf
Applied some more code simplifications suggested by the new Simplify checker (Y105: use contextlib.suppress) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8218
diff
changeset
|
104 | with contextlib.suppress(KeyError): |
93b8a353c4bf
Applied some more code simplifications suggested by the new Simplify checker (Y105: use contextlib.suppress) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8218
diff
changeset
|
105 | del self.threads[threadId] |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
106 | finally: |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
107 | self.unlockClient() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
108 | |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
109 | def lockClient(self, blocking=True): |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
110 | """ |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
111 | Public method to acquire the lock for this client. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
112 | |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
113 | @param blocking flag to indicating a blocking lock |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
114 | @type bool |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
115 | @return flag indicating successful locking |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
116 | @rtype bool |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
117 | """ |
7986
2971d5d19951
Fixed a few thread related issues in the debugger.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
118 | return self.clientLock.acquire(blocking) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
119 | |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
120 | def unlockClient(self): |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
121 | """ |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
122 | Public method to release the lock for this client. |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
123 | """ |
8240
93b8a353c4bf
Applied some more code simplifications suggested by the new Simplify checker (Y105: use contextlib.suppress) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8218
diff
changeset
|
124 | with contextlib.suppress(RuntimeError): |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
125 | self.clientLock.release() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
126 | |
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
5581
diff
changeset
|
127 | def setCurrentThread(self, threadId): |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
128 | """ |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
129 | Public method to set the current thread. |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
130 | |
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
5581
diff
changeset
|
131 | @param threadId the id the current thread should be set to. |
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
132 | @type int |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
133 | """ |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
134 | try: |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
135 | self.lockClient() |
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
5581
diff
changeset
|
136 | if threadId is None: |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
137 | self.currentThread = None |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
138 | else: |
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
5581
diff
changeset
|
139 | self.currentThread = self.threads.get(threadId) |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
140 | finally: |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
141 | self.unlockClient() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
142 | |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
143 | def dumpThreadList(self): |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
144 | """ |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
145 | Public method to send the list of threads. |
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
146 | """ |
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
147 | self.updateThreadList() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
148 | |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
149 | threadList = [] |
7417
9391a330f50f
Optimized the thread list dump.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7416
diff
changeset
|
150 | currentId = _thread.get_ident() |
9391a330f50f
Optimized the thread list dump.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7416
diff
changeset
|
151 | # update thread names set by user (threading.setName) |
10293
6c5585c3e543
Changed the use of the deprecated Thread.getName() to a direct access of the Thread.name property.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
152 | threadNames = {t.ident: t.name for t in threading.enumerate()} |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
153 | |
7417
9391a330f50f
Optimized the thread list dump.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7416
diff
changeset
|
154 | for threadId, thd in self.threads.items(): |
9391a330f50f
Optimized the thread list dump.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7416
diff
changeset
|
155 | d = {"id": threadId} |
9391a330f50f
Optimized the thread list dump.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7416
diff
changeset
|
156 | try: |
9391a330f50f
Optimized the thread list dump.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7416
diff
changeset
|
157 | d["name"] = threadNames.get(threadId, thd.name) |
9391a330f50f
Optimized the thread list dump.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7416
diff
changeset
|
158 | d["broken"] = thd.isBroken |
9391a330f50f
Optimized the thread list dump.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7416
diff
changeset
|
159 | d["except"] = thd.isException |
9391a330f50f
Optimized the thread list dump.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7416
diff
changeset
|
160 | except Exception: |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
161 | d["name"] = "UnknownThread" |
7417
9391a330f50f
Optimized the thread list dump.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7416
diff
changeset
|
162 | d["broken"] = False |
9391a330f50f
Optimized the thread list dump.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7416
diff
changeset
|
163 | d["except"] = False |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
164 | |
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
165 | threadList.append(d) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
166 | |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
167 | self.sendJsonCommand( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
168 | "ResponseThreadList", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
169 | { |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
170 | "currentID": currentId, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
171 | "threadList": threadList, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
172 | }, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
173 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
174 | |
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
175 | def getExecutedFrame(self, frame): |
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
176 | """ |
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
177 | Public method to return the currently executed frame. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
178 | |
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
179 | @param frame the current frame |
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
180 | @type frame object |
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
181 | @return the frame which is excecuted (without debugger frames) |
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
182 | @rtype frame object |
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
183 | """ |
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
184 | # to get the currently executed frame, skip all frames belonging to the |
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
185 | # debugger |
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
186 | while frame is not None: |
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
187 | baseName = os.path.basename(frame.f_code.co_filename) |
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
188 | if not baseName.startswith( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
189 | ( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
190 | "DebugClientBase.py", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
191 | "DebugBase.py", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
192 | "AsyncFile.py", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
193 | "ThreadExtension.py", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
194 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
195 | ): |
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
196 | break |
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
197 | frame = frame.f_back |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
198 | |
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
199 | return frame |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
200 | |
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
201 | def updateThreadList(self): |
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
202 | """ |
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
203 | Public method to update the list of running threads. |
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
204 | """ |
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
205 | frames = sys._current_frames() |
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
5581
diff
changeset
|
206 | for threadId, frame in frames.items(): |
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
207 | # skip our own timer thread |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
208 | if frame.f_code.co_name == "__eventPollTimer": |
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
209 | continue |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
210 | |
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
211 | # Unknown thread |
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
5581
diff
changeset
|
212 | if threadId not in self.threads: |
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
213 | newThread = DebugBase(self) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
214 | name = "Thread-{0}".format(self.threadNumber) |
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
215 | self.threadNumber += 1 |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
216 | |
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
5581
diff
changeset
|
217 | newThread.id = threadId |
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
218 | newThread.name = name |
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
5581
diff
changeset
|
219 | self.threads[threadId] = newThread |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
220 | |
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
221 | # adjust current frame |
5269
0e96e1557c45
Fix for PyPy showing no local variables and suppress exception on old frames.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5240
diff
changeset
|
222 | if "__pypy__" not in sys.builtin_module_names: |
5561
5fffb5cc1a88
When in unhandled exception, current frame lies in debugger files always. In breakpoints,
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5552
diff
changeset
|
223 | # Don't update with None |
5fffb5cc1a88
When in unhandled exception, current frame lies in debugger files always. In breakpoints,
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5552
diff
changeset
|
224 | currentFrame = self.getExecutedFrame(frame) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
225 | if ( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
226 | currentFrame is not None |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
227 | and self.threads[threadId].isBroken is False |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
228 | ): |
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
5581
diff
changeset
|
229 | self.threads[threadId].currentFrame = currentFrame |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
230 | |
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset
|
231 | # Clean up obsolet because terminated threads |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
232 | self.threads = { |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
233 | id_: thrd for id_, thrd in self.threads.items() if id_ in frames |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
234 | } |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
235 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
236 | ####################################################################### |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
237 | ## Methods below deal with patching various modules to support |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
238 | ## debugging of threads. |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
239 | ####################################################################### |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
240 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
241 | def patchPyThread(self, module): |
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset
|
242 | """ |
8334
c113428ecff3
Removed some obsolete code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
243 | Public method to patch Python _thread module. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
244 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
245 | @param module reference to the imported module to be patched |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
246 | @type module |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
247 | """ |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
248 | # make thread hooks available to system |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
249 | self._original_start_new_thread = module.start_new_thread |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
250 | module.start_new_thread = self.attachThread |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
251 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
252 | def patchGreenlet(self, module): |
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset
|
253 | """ |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
254 | Public method to patch the 'greenlet' module. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
255 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
256 | @param module reference to the imported module to be patched |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
257 | @type module |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
258 | @return flag indicating that the module was processed |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
259 | @rtype bool |
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset
|
260 | """ |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
261 | # Check for greenlet.settrace |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
262 | if hasattr(module, "settrace"): |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
263 | DebugBase.pollTimerEnabled = False |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
264 | return True |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
265 | return False |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
266 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
267 | def patchPyThreading(self, module): |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
268 | """ |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
269 | Public method to patch the Python threading module. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
270 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
271 | @param module reference to the imported module to be patched |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
272 | @type module |
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset
|
273 | """ |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
274 | # _debugClient as a class attribute can't be accessed in following |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
275 | # class. Therefore we need a global variable. |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
276 | _debugClient = self |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
277 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
278 | def _bootstrap(self, run): |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
279 | """ |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
280 | Bootstrap for threading, which reports exceptions correctly. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
281 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
282 | @param run the run method of threading.Thread |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
283 | @type method pointer |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
284 | """ |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
285 | newThread = DebugBase(_debugClient) |
7416
6f899e82f9a2
Ironed out some issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7415
diff
changeset
|
286 | newThread.name = self.name |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
287 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
288 | _debugClient.threads[self.ident] = newThread |
7416
6f899e82f9a2
Ironed out some issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7415
diff
changeset
|
289 | _debugClient.dumpThreadList() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
290 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
291 | # see DebugBase.bootstrap |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
292 | sys.settrace(newThread.trace_dispatch) |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
293 | try: |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
294 | run() |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
295 | except Exception: |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
296 | excinfo = sys.exc_info() |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
297 | newThread.user_exception(excinfo, True) |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
298 | finally: |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
299 | sys.settrace(None) |
7416
6f899e82f9a2
Ironed out some issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7415
diff
changeset
|
300 | _debugClient.dumpThreadList() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
301 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
302 | class ThreadWrapper(module.Thread): |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
303 | """ |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
304 | Wrapper class for threading.Thread. |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
305 | """ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
306 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
307 | def __init__(self, *args, **kwargs): |
5552
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset
|
308 | """ |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
309 | Constructor |
6196
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6195
diff
changeset
|
310 | """ |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
311 | # Overwrite the provided run method with our own, to |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
312 | # intercept the thread creation by threading.Thread |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
313 | self.run = lambda s=self, run=self.run: _bootstrap(s, run) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
314 | |
8218
7c09585bd960
Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8207
diff
changeset
|
315 | super().__init__(*args, **kwargs) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
316 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
317 | module.Thread = ThreadWrapper |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
318 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
319 | # Special handling of threading.(_)Timer |
7646
39e3db2b4936
Merged with default to track recent development.
Detlev Offenbach <detlev@die-offenbachs.de>
diff
changeset
|
320 | timer = module.Timer |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
321 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
322 | class TimerWrapper(timer, ThreadWrapper): |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
323 | """ |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
324 | Wrapper class for threading.(_)Timer. |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
325 | """ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
326 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
327 | def __init__(self, interval, function, *args, **kwargs): |
6196
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6195
diff
changeset
|
328 | """ |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
329 | Constructor |
6196
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6195
diff
changeset
|
330 | """ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
331 | super().__init__(interval, function, *args, **kwargs) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
332 | |
7646
39e3db2b4936
Merged with default to track recent development.
Detlev Offenbach <detlev@die-offenbachs.de>
diff
changeset
|
333 | module.Timer = TimerWrapper |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
334 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
335 | # Special handling of threading._DummyThread |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
336 | class DummyThreadWrapper(module._DummyThread, ThreadWrapper): |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
337 | """ |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
338 | Wrapper class for threading._DummyThread. |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
339 | """ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
340 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
341 | def __init__(self, *args, **kwargs): |
7364
bcf6b40e7790
Fixed thread creation on uninitialized threads (found in git-cola project).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
7360
diff
changeset
|
342 | """ |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
343 | Constructor |
7364
bcf6b40e7790
Fixed thread creation on uninitialized threads (found in git-cola project).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
7360
diff
changeset
|
344 | """ |
8218
7c09585bd960
Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8207
diff
changeset
|
345 | super().__init__(*args, **kwargs) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
346 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
347 | module._DummyThread = DummyThreadWrapper |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
348 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
349 | def patchQThread(self, module): |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
350 | """ |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
351 | Public method to patch the QtCore module's QThread. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
352 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
353 | @param module reference to the imported module to be patched |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
354 | @type module |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
355 | """ |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
356 | # _debugClient as a class attribute can't be accessed in following |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
357 | # class. Therefore we need a global variable. |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
358 | _debugClient = self |
5552
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset
|
359 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
360 | def _bootstrapQThread(self, run): |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
361 | """ |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
362 | Bootstrap for QThread, which reports exceptions correctly. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
363 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
364 | @param run the run method of *.QThread |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
365 | @type method pointer |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
366 | """ |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
367 | global _qtThreadNumber |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
368 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
369 | newThread = DebugBase(_debugClient) |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
370 | ident = _thread.get_ident() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
371 | name = "QtThread-{0}".format(_qtThreadNumber) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
372 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
373 | _qtThreadNumber += 1 |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
374 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
375 | newThread.id = ident |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
376 | newThread.name = name |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
377 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
378 | _debugClient.threads[ident] = newThread |
7416
6f899e82f9a2
Ironed out some issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7415
diff
changeset
|
379 | _debugClient.dumpThreadList() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
380 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
381 | # see DebugBase.bootstrap |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
382 | sys.settrace(newThread.trace_dispatch) |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
383 | try: |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
384 | run() |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
385 | except SystemExit: |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
386 | # *.QThreads doesn't like SystemExit |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
387 | pass |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
388 | except Exception: |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
389 | excinfo = sys.exc_info() |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
390 | newThread.user_exception(excinfo, True) |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
391 | finally: |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
392 | sys.settrace(None) |
7416
6f899e82f9a2
Ironed out some issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7415
diff
changeset
|
393 | _debugClient.dumpThreadList() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
394 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
395 | class QThreadWrapper(module.QThread): |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
396 | """ |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
397 | Wrapper class for *.QThread. |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
398 | """ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
399 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
400 | def __init__(self, *args, **kwargs): |
5552
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset
|
401 | """ |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
402 | Constructor |
5552
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset
|
403 | """ |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
404 | # Overwrite the provided run method with our own, to |
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
405 | # intercept the thread creation by Qt |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
406 | self.run = lambda s=self, run=self.run: (_bootstrapQThread(s, run)) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
407 | |
8218
7c09585bd960
Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8207
diff
changeset
|
408 | super().__init__(*args, **kwargs) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
409 | |
7415 | 410 | class QRunnableWrapper(module.QRunnable): |
411 | """ | |
412 | Wrapper class for *.QRunnable. | |
413 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
414 | |
7415 | 415 | def __init__(self, *args, **kwargs): |
6196
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6195
diff
changeset
|
416 | """ |
7415 | 417 | Constructor |
6196
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
6195
diff
changeset
|
418 | """ |
7415 | 419 | # Overwrite the provided run method with our own, to |
420 | # intercept the thread creation by Qt | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
421 | self.run = lambda s=self, run=self.run: (_bootstrapQThread(s, run)) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
422 | |
8218
7c09585bd960
Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8207
diff
changeset
|
423 | super().__init__(*args, **kwargs) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
424 | |
7404
663f1c3d6f53
Placed the module loader and patching logic into a separate module of the debug client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7390
diff
changeset
|
425 | module.QThread = QThreadWrapper |
7415 | 426 | module.QRunnable = QRunnableWrapper |