7 Module implementing an import hook patching thread modules to get debugged too. |
7 Module implementing an import hook patching thread modules to get debugged too. |
8 """ |
8 """ |
9 |
9 |
10 import os |
10 import os |
11 import sys |
11 import sys |
|
12 import contextlib |
12 |
13 |
13 import _thread |
14 import _thread |
14 import threading |
15 import threading |
15 |
16 |
16 from DebugBase import DebugBase |
17 from DebugBase import DebugBase |
17 |
18 |
18 _qtThreadNumber = 1 |
19 _qtThreadNumber = 1 |
19 |
20 |
20 |
21 |
21 class ThreadExtension(object): |
22 class ThreadExtension: |
22 """ |
23 """ |
23 Class implementing the thread support for the debugger. |
24 Class implementing the thread support for the debugger. |
24 |
25 |
25 Provides methods for intercepting thread creation, retrieving the running |
26 Provides methods for intercepting thread creation, retrieving the running |
26 threads and their name and state. |
27 threads and their name and state. |
114 |
114 |
115 def unlockClient(self): |
115 def unlockClient(self): |
116 """ |
116 """ |
117 Public method to release the lock for this client. |
117 Public method to release the lock for this client. |
118 """ |
118 """ |
119 try: |
119 with contextlib.suppress(RuntimeError): |
120 self.clientLock.release() |
120 self.clientLock.release() |
121 except RuntimeError: |
|
122 pass |
|
123 |
121 |
124 def setCurrentThread(self, threadId): |
122 def setCurrentThread(self, threadId): |
125 """ |
123 """ |
126 Public method to set the current thread. |
124 Public method to set the current thread. |
127 |
125 |
296 """ |
294 """ |
297 # Overwrite the provided run method with our own, to |
295 # Overwrite the provided run method with our own, to |
298 # intercept the thread creation by threading.Thread |
296 # intercept the thread creation by threading.Thread |
299 self.run = lambda s=self, run=self.run: _bootstrap(s, run) |
297 self.run = lambda s=self, run=self.run: _bootstrap(s, run) |
300 |
298 |
301 super(ThreadWrapper, self).__init__(*args, **kwargs) |
299 super().__init__(*args, **kwargs) |
302 |
300 |
303 module.Thread = ThreadWrapper |
301 module.Thread = ThreadWrapper |
304 |
302 |
305 # Special handling of threading.(_)Timer |
303 # Special handling of threading.(_)Timer |
306 timer = module.Timer |
304 timer = module.Timer |
311 """ |
309 """ |
312 def __init__(self, interval, function, *args, **kwargs): |
310 def __init__(self, interval, function, *args, **kwargs): |
313 """ |
311 """ |
314 Constructor |
312 Constructor |
315 """ |
313 """ |
316 super(TimerWrapper, self).__init__( |
314 super().__init__( |
317 interval, function, *args, **kwargs) |
315 interval, function, *args, **kwargs) |
318 |
316 |
319 module.Timer = TimerWrapper |
317 module.Timer = TimerWrapper |
320 |
318 |
321 # Special handling of threading._DummyThread |
319 # Special handling of threading._DummyThread |
325 """ |
323 """ |
326 def __init__(self, *args, **kwargs): |
324 def __init__(self, *args, **kwargs): |
327 """ |
325 """ |
328 Constructor |
326 Constructor |
329 """ |
327 """ |
330 super(DummyThreadWrapper, self).__init__(*args, **kwargs) |
328 super().__init__(*args, **kwargs) |
331 |
329 |
332 module._DummyThread = DummyThreadWrapper |
330 module._DummyThread = DummyThreadWrapper |
333 |
331 |
334 def patchQThread(self, module): |
332 def patchQThread(self, module): |
335 """ |
333 """ |
388 # Overwrite the provided run method with our own, to |
386 # Overwrite the provided run method with our own, to |
389 # intercept the thread creation by Qt |
387 # intercept the thread creation by Qt |
390 self.run = lambda s=self, run=self.run: ( |
388 self.run = lambda s=self, run=self.run: ( |
391 _bootstrapQThread(s, run)) |
389 _bootstrapQThread(s, run)) |
392 |
390 |
393 super(QThreadWrapper, self).__init__(*args, **kwargs) |
391 super().__init__(*args, **kwargs) |
394 |
392 |
395 class QRunnableWrapper(module.QRunnable): |
393 class QRunnableWrapper(module.QRunnable): |
396 """ |
394 """ |
397 Wrapper class for *.QRunnable. |
395 Wrapper class for *.QRunnable. |
398 """ |
396 """ |
403 # Overwrite the provided run method with our own, to |
401 # Overwrite the provided run method with our own, to |
404 # intercept the thread creation by Qt |
402 # intercept the thread creation by Qt |
405 self.run = lambda s=self, run=self.run: ( |
403 self.run = lambda s=self, run=self.run: ( |
406 _bootstrapQThread(s, run)) |
404 _bootstrapQThread(s, run)) |
407 |
405 |
408 super(QRunnableWrapper, self).__init__(*args, **kwargs) |
406 super().__init__(*args, **kwargs) |
409 |
407 |
410 module.QThread = QThreadWrapper |
408 module.QThread = QThreadWrapper |
411 module.QRunnable = QRunnableWrapper |
409 module.QRunnable = QRunnableWrapper |