eric6/DebugClients/Python/ThreadExtension.py

branch
maintenance
changeset 7642
72721823d453
parent 7437
1148ca40ea36
parent 7639
422fd05e9c91
child 7924
8a96736d465e
equal deleted inserted replaced
7608:f7cb83647621 7642:72721823d453
9 9
10 import os.path 10 import os.path
11 import sys 11 import sys
12 import importlib 12 import importlib
13 13
14 if sys.version_info[0] == 2: 14 import _thread
15 import thread as _thread
16 else:
17 import _thread
18
19 import threading 15 import threading
20 16
21 from DebugBase import DebugBase 17 from DebugBase import DebugBase
22 18
23 _qtThreadNumber = 1 19 _qtThreadNumber = 1
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, 'PyQt4.QtCore', 'PyQt5.QtCore', 254 if fullname in ['_thread', 'PyQt5.QtCore', 'PySide2.QtCore',
264 'PySide.QtCore', 'PySide2.QtCore', 'greenlet', 255 'greenlet', 'threading'
265 'threading'] 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
269 260
270 return None 261 return None
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.
370 super(DummyThreadWrapper, self).__init__(*args, **kwargs) 355 super(DummyThreadWrapper, self).__init__(*args, **kwargs)
371 356
372 module._DummyThread = DummyThreadWrapper 357 module._DummyThread = DummyThreadWrapper
373 358
374 # Add hook for *.QThread 359 # Add hook for *.QThread
375 elif (fullname in ['PyQt4.QtCore', 'PyQt5.QtCore', 360 elif (fullname in ['PyQt5.QtCore', 'PySide2.QtCore'] and
376 'PySide.QtCore', 'PySide2.QtCore'] and 361 self.qtThreadAttached is False):
377 self.qtThreadAttached is False):
378 self.qtThreadAttached = True 362 self.qtThreadAttached = True
379 # _debugClient as a class attribute can't be accessed in following 363 # _debugClient as a class attribute can't be accessed in following
380 # class. Therefore we need a global variable. 364 # class. Therefore we need a global variable.
381 _debugClient = self 365 _debugClient = self
382 366
446 module.QThread = QThreadWrapper 430 module.QThread = QThreadWrapper
447 module.QRunnable = QRunnableWrapper 431 module.QRunnable = QRunnableWrapper
448 432
449 self.enableImportHooks = True 433 self.enableImportHooks = True
450 return module 434 return module
451
452 #
453 # eflag: noqa = M702

eric ide

mercurial