52 self.currentThreadExec = self |
48 self.currentThreadExec = self |
53 |
49 |
54 # special objects representing the main scripts thread and frame |
50 # special objects representing the main scripts thread and frame |
55 self.mainThread = self |
51 self.mainThread = self |
56 |
52 |
57 if sys.version_info[0] == 2: |
|
58 self.threadModName = 'thread' |
|
59 else: |
|
60 self.threadModName = '_thread' |
|
61 |
|
62 # reset already imported thread module to apply hooks at next import |
53 # reset already imported thread module to apply hooks at next import |
63 del sys.modules[self.threadModName] |
54 del sys.modules['_thread'] |
64 del sys.modules['threading'] |
55 del sys.modules['threading'] |
65 |
56 |
66 sys.meta_path.insert(0, self) |
57 sys.meta_path.insert(0, self) |
67 |
58 |
68 def attachThread(self, target=None, args=None, kwargs=None, |
59 def attachThread(self, target=None, args=None, kwargs=None, |
258 @rtype object |
249 @rtype object |
259 """ |
250 """ |
260 if fullname in sys.modules or not self.debugging: |
251 if fullname in sys.modules or not self.debugging: |
261 return None |
252 return None |
262 |
253 |
263 if fullname in [self.threadModName, 'PyQt5.QtCore', 'PySide2.QtCore', |
254 if fullname in ['_thread', 'PyQt5.QtCore', 'PySide2.QtCore', |
264 'greenlet', 'threading' |
255 'greenlet', 'threading' |
265 ] and self.enableImportHooks: |
256 ] and self.enableImportHooks: |
266 # Disable hook to be able to import original module |
257 # Disable hook to be able to import original module |
267 self.enableImportHooks = False |
258 self.enableImportHooks = False |
268 return self |
259 return self |
278 @return reference to the loaded module |
269 @return reference to the loaded module |
279 @rtype module |
270 @rtype module |
280 """ |
271 """ |
281 module = importlib.import_module(fullname) |
272 module = importlib.import_module(fullname) |
282 sys.modules[fullname] = module |
273 sys.modules[fullname] = module |
283 if (fullname == self.threadModName and |
274 if (fullname == '_thread' and |
284 self._original_start_new_thread is None): |
275 self._original_start_new_thread is None): |
285 # make thread hooks available to system |
276 # make thread hooks available to system |
286 self._original_start_new_thread = module.start_new_thread |
277 self._original_start_new_thread = module.start_new_thread |
287 module.start_new_thread = self.attachThread |
278 module.start_new_thread = self.attachThread |
288 |
279 |
335 super(ThreadWrapper, self).__init__(*args, **kwargs) |
326 super(ThreadWrapper, self).__init__(*args, **kwargs) |
336 |
327 |
337 module.Thread = ThreadWrapper |
328 module.Thread = ThreadWrapper |
338 |
329 |
339 # Special handling of threading.(_)Timer |
330 # Special handling of threading.(_)Timer |
340 if sys.version_info[0] == 2: |
331 timer = module.Timer |
341 timer = module._Timer |
|
342 else: |
|
343 timer = module.Timer |
|
344 |
332 |
345 class TimerWrapper(timer, ThreadWrapper): |
333 class TimerWrapper(timer, ThreadWrapper): |
346 """ |
334 """ |
347 Wrapper class for threading.(_)Timer. |
335 Wrapper class for threading.(_)Timer. |
348 """ |
336 """ |
351 Constructor |
339 Constructor |
352 """ |
340 """ |
353 super(TimerWrapper, self).__init__( |
341 super(TimerWrapper, self).__init__( |
354 interval, function, *args, **kwargs) |
342 interval, function, *args, **kwargs) |
355 |
343 |
356 if sys.version_info[0] == 2: |
344 module.Timer = TimerWrapper |
357 module._Timer = TimerWrapper |
|
358 else: |
|
359 module.Timer = TimerWrapper |
|
360 |
345 |
361 # Special handling of threading._DummyThread |
346 # Special handling of threading._DummyThread |
362 class DummyThreadWrapper(module._DummyThread, ThreadWrapper): |
347 class DummyThreadWrapper(module._DummyThread, ThreadWrapper): |
363 """ |
348 """ |
364 Wrapper class for threading._DummyThread. |
349 Wrapper class for threading._DummyThread. |