DebugClients/Python/ThreadExtension.py

branch
debugger speed
changeset 5228
4332a6751b14
parent 5222
6548dc1bfd48
child 5240
71c51aae2f4e
--- a/DebugClients/Python/ThreadExtension.py	Sun Oct 09 22:22:36 2016 +0200
+++ b/DebugClients/Python/ThreadExtension.py	Tue Oct 11 21:55:03 2016 +0200
@@ -34,6 +34,7 @@
         self.threadNumber = 1
         self.enableImportHooks = True
         self._original_start_new_thread = None
+        self._qtThread = None
         
         self.clientLock = threading.RLock()
         
@@ -250,7 +251,9 @@
         """
         if fullname in sys.modules or not self.debugging:
             return None
-        if fullname == self.threadModName and self.enableImportHooks:
+        
+        if fullname in [self.threadModName, 'PyQt4.QtCore', 'PyQt5.QtCore',
+                        'PySide.QtCore'] and self.enableImportHooks:
             # Disable hook to be able to import original module
             self.enableImportHooks = False
             return self
@@ -273,6 +276,45 @@
             # 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:
+            self._qtThread = module.QtCore.QThread
+            # _debugClient as a class attribute can't be accessed in following
+            # class. Therefore we need a global variable.
+            _debugClient = self
+            
+            class QThreadWrapper(module.QtCore.QThread):
+                __qtThreadNumber = 1
+                
+                def __init__(self, *args, **kwargs):
+                    # Overwrite the provided run method with our own, to
+                    # intercept the thread creation by Qt
+                    self._ApplicationRun = self.run
+                    self.run = self.__bootstrapQThread
+                    
+                    super(QThreadWrapper, self).__init__(*args, **kwargs)
+                
+                def __bootstrapQThread(self):
+                    newThread = DebugBase(_debugClient)
+                    ident = _thread.get_ident()
+                    name = 'QtThread-{0}'.format(self.__qtThreadNumber)
+                    self.__qtThreadNumber += 1
+                
+                    newThread.id = ident
+                    newThread.name = name
+                    
+                    _debugClient.threads[ident] = newThread
+                    
+                    frame = sys._getframe()
+                    newThread.botframe = frame
+                    frame.f_trace = newThread.trace_dispatch
+                    # see DebugBase.bootstrap
+                    sys.settrace(
+                        lambda frame, event, arg: newThread.trace_dispatch)
+                    
+                    return self._ApplicationRun()
+            
+            module.QThread = QThreadWrapper
         
         self.enableImportHooks = True
         return module

eric ide

mercurial