diff -r 4e2ab5215bcf -r 5a90f78a73c9 DebugClients/Python/ThreadExtension.py --- a/DebugClients/Python/ThreadExtension.py Tue Feb 21 22:01:20 2017 +0100 +++ b/DebugClients/Python/ThreadExtension.py Tue Feb 21 22:08:17 2017 +0100 @@ -35,6 +35,7 @@ self.threadNumber = 1 self.enableImportHooks = True self._original_start_new_thread = None + self.threadingAttached = False self._qtThread = None self.clientLock = threading.RLock() @@ -250,7 +251,8 @@ return None if fullname in [self.threadModName, 'PyQt4.QtCore', 'PyQt5.QtCore', - 'PySide.QtCore'] and self.enableImportHooks: + 'PySide.QtCore', 'PySide2.QtCore', + 'threading'] and self.enableImportHooks: # Disable hook to be able to import original module self.enableImportHooks = False return self @@ -273,8 +275,40 @@ # make thread hooks available to system self._original_start_new_thread = module.start_new_thread module.start_new_thread = self.attachThread - elif fullname in ['PyQt4.QtCore', 'PyQt5.QtCore', - 'PySide.QtCore'] and self._qtThread is None: + + # Add hook for threading.run() + elif (fullname == "threading" and self.threadingAttached is False): + self.threadingAttached = True + + # _debugClient as a class attribute can't be accessed in following + # class. Therefore we need a global variable. + _debugClient = self + + def _bootstrap(self, run): + newThread = _debugClient.threads[self.ident] + newThread.name = self.name + # see DebugBase.bootstrap + sys.settrace(newThread.trace_dispatch) + try: + run() + except Exception: + excinfo = sys.exc_info() + newThread.user_exception(excinfo, True) + + class ThreadWrapper(module.Thread): + + def __init__(self, *args, **kwargs): + # Overwrite the provided run method with our own, to + # intercept the thread creation by threading.Thread + self.run = lambda s=self, run=self.run: _bootstrap(s, run) + + super(ThreadWrapper, self).__init__(*args, **kwargs) + + module.Thread = ThreadWrapper + + elif (fullname in ['PyQt4.QtCore', 'PyQt5.QtCore', + 'PySide.QtCore', 'PySide2.QtCore'] and + self._qtThread is None): self._qtThread = module.QThread # _debugClient as a class attribute can't be accessed in following # class. Therefore we need a global variable.