Sat, 27 Jun 2020 12:08:12 +0200
Removed some more Python2 related code.
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

1  # * coding: utf8 * 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

2  
7360
9190402e4505
Updated copyright for 2020.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
6942
diff
changeset

3  # Copyright (c) 2014  2020 Detlev Offenbach <detlev@dieoffenbachs.de> 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

4  # 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

5  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

6  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

7  Module implementing an import hook patching thread modules to get debugged too. 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

8  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

9  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

10  import os.path 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

11  import sys 
5240
71c51aae2f4e
Use importlib instead __import__ because it returns the correct module directly.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5228
diff
changeset

12  import importlib 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

13  
7637
c878e8255972
Removed some more Python2 related code.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7635
diff
changeset

14  import _thread 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

15  import threading 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

16  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

17  from DebugBase import DebugBase 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

18  
5552
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

19  _qtThreadNumber = 1 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

20  
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

21  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

22  class ThreadExtension(object): 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

23  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

24  Class implementing the thread support for the debugger. 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

25  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

26  Provides methods for intercepting thread creation, retriving the running 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

27  threads and their name and state. 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

28  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

29  def __init__(self): 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

30  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

31  Constructor 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

32  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

33  self.threadNumber = 1 
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

34  self.enableImportHooks = True 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

35  self._original_start_new_thread = None 
5544
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

36  self.threadingAttached = False 
5552
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

37  self.qtThreadAttached = False 
5667
86554f131048
Avoid crashes on using greenlets.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5651
diff
changeset

38  self.greenlet = False 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

39  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

40  self.clientLock = threading.RLock() 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

41  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

42  # dictionary of all threads running {id: DebugBase} 
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

43  self.threads = {_thread.get_ident(): self} 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

44  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

45  # the "current" thread, basically for variables view 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

46  self.currentThread = self 
5221
960afd19c1b6
Give the next debugger command to the thread where we are stopped at the moment.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5209
diff
changeset

47  # the thread we are at a breakpoint continuing at next command 
960afd19c1b6
Give the next debugger command to the thread where we are stopped at the moment.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5209
diff
changeset

48  self.currentThreadExec = self 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

49  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

50  # special objects representing the main scripts thread and frame 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

51  self.mainThread = self 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

52  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

53  # reset already imported thread module to apply hooks at next import 
7637
c878e8255972
Removed some more Python2 related code.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7635
diff
changeset

54  del sys.modules['_thread'] 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

55  del sys.modules['threading'] 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

56  
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

57  sys.meta_path.insert(0, self) 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

58  
5651
982465f8389c
Fixed a few code style issues related to the usage of mutable types for default arguments.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5647
diff
changeset

59  def attachThread(self, target=None, args=None, kwargs=None, 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

60  mainThread=False): 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

61  """ 
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

62  Public method to setup a standard thread for DebugClient to debug. 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

63  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

64  If mainThread is True, then we are attaching to the already 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

65  started mainthread of the app and the rest of the args are ignored. 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

66  
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

67  @param target the start function of the target thread (i.e. the user 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

68  code) 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

69  @param args arguments to pass to target 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

70  @param kwargs keyword arguments to pass to target 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

71  @param mainThread True, if we are attaching to the already 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

72  started mainthread of the app 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

73  @return identifier of the created thread 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

74  """ 
5651
982465f8389c
Fixed a few code style issues related to the usage of mutable types for default arguments.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5647
diff
changeset

75  if kwargs is None: 
982465f8389c
Fixed a few code style issues related to the usage of mutable types for default arguments.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5647
diff
changeset

76  kwargs = {} 
982465f8389c
Fixed a few code style issues related to the usage of mutable types for default arguments.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5647
diff
changeset

77  
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

78  if mainThread: 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

79  ident = _thread.get_ident() 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

80  name = 'MainThread' 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

81  newThread = self.mainThread 
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

82  newThread.isMainThread = True 
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

83  if self.debugging: 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

84  sys.setprofile(newThread.profile) 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

85  
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

86  else: 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

87  newThread = DebugBase(self) 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

88  ident = self._original_start_new_thread( 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

89  newThread.bootstrap, (target, args, kwargs)) 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

90  name = 'Thread{0}'.format(self.threadNumber) 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

91  self.threadNumber += 1 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

92  
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

93  newThread.id = ident 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

94  newThread.name = name 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

95  
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

96  self.threads[ident] = newThread 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

97  
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

98  return ident 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

99  
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

100  def threadTerminated(self, threadId): 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

101  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

102  Public method called when a DebugThread has exited. 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

103  
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

104  @param threadId id of the DebugThread that has exited 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

105  @type int 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

106  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

107  self.lockClient() 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

108  try: 
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

109  del self.threads[threadId] 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

110  except KeyError: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

111  pass 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

112  finally: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

113  self.unlockClient() 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

114  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

115  def lockClient(self, blocking=True): 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

116  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

117  Public method to acquire the lock for this client. 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

118  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

119  @param blocking flag to indicating a blocking lock 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

120  @type bool 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

121  @return flag indicating successful locking 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

122  @rtype bool 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

123  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

124  if blocking: 
6891
93f82da09f22
Fixed some code style issues detected by the new 'return' checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
6645
diff
changeset

125  return self.clientLock.acquire() 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

126  else: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

127  return self.clientLock.acquire(blocking) 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

128  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

129  def unlockClient(self): 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

130  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

131  Public method to release the lock for this client. 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

132  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

133  try: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

134  self.clientLock.release() 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

135  except AssertionError: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

136  pass 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

137  
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5581
diff
changeset

138  def setCurrentThread(self, threadId): 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

139  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

140  Public method to set the current thread. 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

141  
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5581
diff
changeset

142  @param threadId the id the current thread should be set to. 
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

143  @type int 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

144  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

145  try: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

146  self.lockClient() 
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5581
diff
changeset

147  if threadId is None: 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

148  self.currentThread = None 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

149  else: 
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5581
diff
changeset

150  self.currentThread = self.threads.get(threadId) 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

151  finally: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

152  self.unlockClient() 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

153  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

154  def dumpThreadList(self): 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

155  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

156  Public method to send the list of threads. 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

157  """ 
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

158  self.updateThreadList() 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

159  threadList = [] 
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

160  if len(self.threads) > 1: 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

161  currentId = _thread.get_ident() 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

162  # update thread names set by user (threading.setName) 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

163  threadNames = {t.ident: t.getName() for t in threading.enumerate()} 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

164  
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5581
diff
changeset

165  for threadId, thd in self.threads.items(): 
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5581
diff
changeset

166  d = {"id": threadId} 
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

167  try: 
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5581
diff
changeset

168  d["name"] = threadNames.get(threadId, thd.name) 
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

169  d["broken"] = thd.isBroken 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

170  except Exception: 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

171  d["name"] = 'UnknownThread' 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

172  d["broken"] = False 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

173  
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

174  threadList.append(d) 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

175  else: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

176  currentId = 1 
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

177  d = {"id": 1} 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

178  d["name"] = "MainThread" 
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

179  d["broken"] = self.isBroken 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

180  threadList.append(d) 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

181  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

182  self.sendJsonCommand("ResponseThreadList", { 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

183  "currentID": currentId, 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

184  "threadList": threadList, 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

185  }) 
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

186  
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

187  def getExecutedFrame(self, frame): 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

188  """ 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

189  Public method to return the currently executed frame. 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

190  
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

191  @param frame the current frame 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

192  @type frame object 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

193  @return the frame which is excecuted (without debugger frames) 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

194  @rtype frame object 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

195  """ 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

196  # to get the currently executed frame, skip all frames belonging to the 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

197  # debugger 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

198  while frame is not None: 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

199  baseName = os.path.basename(frame.f_code.co_filename) 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

200  if not baseName.startswith( 
5581
f8abf5f741ef
Don't update currentFrame when in a breakpoint or exception. Otherwise unpredictable frames could be shown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5580
diff
changeset

201  ('DebugClientBase.py', 'DebugBase.py', 'AsyncFile.py', 
f8abf5f741ef
Don't update currentFrame when in a breakpoint or exception. Otherwise unpredictable frames could be shown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5580
diff
changeset

202  'ThreadExtension.py')): 
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

203  break 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

204  frame = frame.f_back 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

205  
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

206  return frame 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

207  
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

208  def updateThreadList(self): 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

209  """ 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

210  Public method to update the list of running threads. 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

211  """ 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

212  frames = sys._current_frames() 
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5581
diff
changeset

213  for threadId, frame in frames.items(): 
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

214  # skip our own timer thread 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

215  if frame.f_code.co_name == '__eventPollTimer': 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

216  continue 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

217  
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

218  # Unknown thread 
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5581
diff
changeset

219  if threadId not in self.threads: 
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

220  newThread = DebugBase(self) 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

221  name = 'Thread{0}'.format(self.threadNumber) 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

222  self.threadNumber += 1 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

223  
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5581
diff
changeset

224  newThread.id = threadId 
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

225  newThread.name = name 
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5581
diff
changeset

226  self.threads[threadId] = newThread 
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

227  
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

228  # adjust current frame 
5269
0e96e1557c45
Fix for PyPy showing no local variables and suppress exception on old frames.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5240
diff
changeset

229  if "__pypy__" not in sys.builtin_module_names: 
5561
5fffb5cc1a88
When in unhandled exception, current frame lies in debugger files always. In breakpoints,
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5552
diff
changeset

230  # Don't update with None 
5fffb5cc1a88
When in unhandled exception, current frame lies in debugger files always. In breakpoints,
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5552
diff
changeset

231  currentFrame = self.getExecutedFrame(frame) 
5581
f8abf5f741ef
Don't update currentFrame when in a breakpoint or exception. Otherwise unpredictable frames could be shown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5580
diff
changeset

232  if (currentFrame is not None and 
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5581
diff
changeset

233  self.threads[threadId].isBroken is False): 
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5581
diff
changeset

234  self.threads[threadId].currentFrame = currentFrame 
5222
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

235  
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

236  # Clean up obsolet because terminated threads 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

237  self.threads = {id_: thrd for id_, thrd in self.threads.items() 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

238  if id_ in frames} 
6548dc1bfd48
Collect all running threads, even if they were not started by the debug client.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5221
diff
changeset

239  
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

240  def find_module(self, fullname, path=None): 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

241  """ 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

242  Public method returning the module loader. 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

243  
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

244  @param fullname name of the module to be loaded 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

245  @type str 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

246  @param path path to resolve the module name 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

247  @type str 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

248  @return module loader object 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

249  @rtype object 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

250  """ 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

251  if fullname in sys.modules or not self.debugging: 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

252  return None 
5228
4332a6751b14
Support for debugging of QThread processes added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5222
diff
changeset

253  
7637
c878e8255972
Removed some more Python2 related code.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7635
diff
changeset

254  if fullname in ['_thread', 'PyQt5.QtCore', 'PySide2.QtCore', 
7635
0cdead130a81
Removed support for Python2 and removed support for Qt4 (PyQt4 and pyside).
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7413
diff
changeset

255  'greenlet', 'threading' 
0cdead130a81
Removed support for Python2 and removed support for Qt4 (PyQt4 and pyside).
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7413
diff
changeset

256  ] and self.enableImportHooks: 
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

257  # Disable hook to be able to import original module 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

258  self.enableImportHooks = False 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

259  return self 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

260  
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

261  return None 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

262  
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

263  def load_module(self, fullname): 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

264  """ 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

265  Public method to load a module. 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

266  
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

267  @param fullname name of the module to be loaded 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

268  @type str 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

269  @return reference to the loaded module 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

270  @rtype module 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

271  """ 
5240
71c51aae2f4e
Use importlib instead __import__ because it returns the correct module directly.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5228
diff
changeset

272  module = importlib.import_module(fullname) 
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

273  sys.modules[fullname] = module 
7637
c878e8255972
Removed some more Python2 related code.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7635
diff
changeset

274  if (fullname == '_thread' and 
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

275  self._original_start_new_thread is None): 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

276  # make thread hooks available to system 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

277  self._original_start_new_thread = module.start_new_thread 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

278  module.start_new_thread = self.attachThread 
5544
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

279  
5667
86554f131048
Avoid crashes on using greenlets.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5651
diff
changeset

280  elif (fullname == 'greenlet' and self.greenlet is False): 
86554f131048
Avoid crashes on using greenlets.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5651
diff
changeset

281  # Check for greenlet.settrace 
86554f131048
Avoid crashes on using greenlets.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5651
diff
changeset

282  if hasattr(module, 'settrace'): 
86554f131048
Avoid crashes on using greenlets.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5651
diff
changeset

283  self.greenlet = True 
86554f131048
Avoid crashes on using greenlets.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5651
diff
changeset

284  DebugBase.pollTimerEnabled = False 
86554f131048
Avoid crashes on using greenlets.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5651
diff
changeset

285  
5544
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

286  # Add hook for threading.run() 
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

287  elif (fullname == "threading" and self.threadingAttached is False): 
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

288  self.threadingAttached = True 
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

289  
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

290  # _debugClient as a class attribute can't be accessed in following 
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

291  # class. Therefore we need a global variable. 
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

292  _debugClient = self 
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

293  
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

294  def _bootstrap(self, run): 
5552
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

295  """ 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

296  Bootstrap for threading, which reports exceptions correctly. 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

297  
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

298  @param run the run method of threading.Thread 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

299  @type method pointer 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

300  """ 
5647
f40b614df103
Was only valid, if thread module has been explicitly imported before threading.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5587
diff
changeset

301  newThread = DebugBase(_debugClient) 
f40b614df103
Was only valid, if thread module has been explicitly imported before threading.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5587
diff
changeset

302  _debugClient.threads[self.ident] = newThread 
5544
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

303  newThread.name = self.name 
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

304  # see DebugBase.bootstrap 
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

305  sys.settrace(newThread.trace_dispatch) 
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

306  try: 
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

307  run() 
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

308  except Exception: 
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

309  excinfo = sys.exc_info() 
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

310  newThread.user_exception(excinfo, True) 
5580
0f5d29acc8ea
Don't step into libraries after a QThread or threading.Thread has finished.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5561
diff
changeset

311  finally: 
0f5d29acc8ea
Don't step into libraries after a QThread or threading.Thread has finished.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5561
diff
changeset

312  sys.settrace(None) 
5544
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

313  
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

314  class ThreadWrapper(module.Thread): 
6196
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
6195
diff
changeset

315  """ 
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
6195
diff
changeset

316  Wrapper class for threading.Thread. 
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
6195
diff
changeset

317  """ 
5544
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

318  def __init__(self, *args, **kwargs): 
6196
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
6195
diff
changeset

319  """ 
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
6195
diff
changeset

320  Constructor 
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
6195
diff
changeset

321  """ 
5544
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

322  # Overwrite the provided run method with our own, to 
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

323  # intercept the thread creation by threading.Thread 
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

324  self.run = lambda s=self, run=self.run: _bootstrap(s, run) 
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

325  
5965
b8d53f4149c8
Reverted changeset d89cd224dd1b because the 'fix' cause more trouble than it fixes.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5918
diff
changeset

326  super(ThreadWrapper, self).__init__(*args, **kwargs) 
5544
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

327  
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

328  module.Thread = ThreadWrapper 
6195
186cbd6b15b1
Make threading.Timer() usable while debugging (fixes issue #262).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
6048
diff
changeset

329  
186cbd6b15b1
Make threading.Timer() usable while debugging (fixes issue #262).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
6048
diff
changeset

330  # Special handling of threading.(_)Timer 
7637
c878e8255972
Removed some more Python2 related code.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7635
diff
changeset

331  timer = module.Timer 
6195
186cbd6b15b1
Make threading.Timer() usable while debugging (fixes issue #262).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
6048
diff
changeset

332  
186cbd6b15b1
Make threading.Timer() usable while debugging (fixes issue #262).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
6048
diff
changeset

333  class TimerWrapper(timer, ThreadWrapper): 
6196
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
6195
diff
changeset

334  """ 
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
6195
diff
changeset

335  Wrapper class for threading.(_)Timer. 
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
6195
diff
changeset

336  """ 
6195
186cbd6b15b1
Make threading.Timer() usable while debugging (fixes issue #262).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
6048
diff
changeset

337  def __init__(self, interval, function, *args, **kwargs): 
6196
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
6195
diff
changeset

338  """ 
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
6195
diff
changeset

339  Constructor 
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
6195
diff
changeset

340  """ 
6195
186cbd6b15b1
Make threading.Timer() usable while debugging (fixes issue #262).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
6048
diff
changeset

341  super(TimerWrapper, self).__init__( 
186cbd6b15b1
Make threading.Timer() usable while debugging (fixes issue #262).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
6048
diff
changeset

342  interval, function, *args, **kwargs) 
186cbd6b15b1
Make threading.Timer() usable while debugging (fixes issue #262).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
6048
diff
changeset

343  
7637
c878e8255972
Removed some more Python2 related code.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7635
diff
changeset

344  module.Timer = TimerWrapper 
5544
5a90f78a73c9
Handling of unhandled exceptions for threading without polluting its namespace.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5389
diff
changeset

345  
7364
bcf6b40e7790
Fixed thread creation on uninitialized threads (found in gitcola project).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
7360
diff
changeset

346  # Special handling of threading._DummyThread 
bcf6b40e7790
Fixed thread creation on uninitialized threads (found in gitcola project).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
7360
diff
changeset

347  class DummyThreadWrapper(module._DummyThread, ThreadWrapper): 
bcf6b40e7790
Fixed thread creation on uninitialized threads (found in gitcola project).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
7360
diff
changeset

348  """ 
bcf6b40e7790
Fixed thread creation on uninitialized threads (found in gitcola project).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
7360
diff
changeset

349  Wrapper class for threading._DummyThread. 
bcf6b40e7790
Fixed thread creation on uninitialized threads (found in gitcola project).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
7360
diff
changeset

350  """ 
bcf6b40e7790
Fixed thread creation on uninitialized threads (found in gitcola project).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
7360
diff
changeset

351  def __init__(self, *args, **kwargs): 
bcf6b40e7790
Fixed thread creation on uninitialized threads (found in gitcola project).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
7360
diff
changeset

352  """ 
bcf6b40e7790
Fixed thread creation on uninitialized threads (found in gitcola project).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
7360
diff
changeset

353  Constructor 
bcf6b40e7790
Fixed thread creation on uninitialized threads (found in gitcola project).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
7360
diff
changeset

354  """ 
bcf6b40e7790
Fixed thread creation on uninitialized threads (found in gitcola project).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
7360
diff
changeset

355  super(DummyThreadWrapper, self).__init__(*args, **kwargs) 
bcf6b40e7790
Fixed thread creation on uninitialized threads (found in gitcola project).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
7360
diff
changeset

356  
bcf6b40e7790
Fixed thread creation on uninitialized threads (found in gitcola project).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
7360
diff
changeset

357  module._DummyThread = DummyThreadWrapper 
bcf6b40e7790
Fixed thread creation on uninitialized threads (found in gitcola project).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
7360
diff
changeset

358  
5552
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

359  # Add hook for *.QThread 
7635
0cdead130a81
Removed support for Python2 and removed support for Qt4 (PyQt4 and pyside).
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7413
diff
changeset

360  elif (fullname in ['PyQt5.QtCore', 'PySide2.QtCore'] and 
0cdead130a81
Removed support for Python2 and removed support for Qt4 (PyQt4 and pyside).
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7413
diff
changeset

361  self.qtThreadAttached is False): 
5552
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

362  self.qtThreadAttached = True 
5228
4332a6751b14
Support for debugging of QThread processes added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5222
diff
changeset

363  # _debugClient as a class attribute can't be accessed in following 
4332a6751b14
Support for debugging of QThread processes added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5222
diff
changeset

364  # class. Therefore we need a global variable. 
4332a6751b14
Support for debugging of QThread processes added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5222
diff
changeset

365  _debugClient = self 
5552
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

366  
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

367  def _bootstrapQThread(self, run): 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

368  """ 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

369  Bootstrap for QThread, which reports exceptions correctly. 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

370  
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

371  @param run the run method of *.QThread 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

372  @type method pointer 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

373  """ 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

374  global _qtThreadNumber 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

375  
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

376  newThread = DebugBase(_debugClient) 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

377  ident = _thread.get_ident() 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

378  name = 'QtThread{0}'.format(_qtThreadNumber) 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

379  
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

380  _qtThreadNumber += 1 
5228
4332a6751b14
Support for debugging of QThread processes added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5222
diff
changeset

381  
5552
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

382  newThread.id = ident 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

383  newThread.name = name 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

384  
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

385  _debugClient.threads[ident] = newThread 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

386  
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

387  # see DebugBase.bootstrap 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

388  sys.settrace(newThread.trace_dispatch) 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

389  try: 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

390  run() 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

391  except SystemExit: 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

392  # *.QThreads doesn't like SystemExit 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

393  pass 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

394  except Exception: 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

395  excinfo = sys.exc_info() 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

396  newThread.user_exception(excinfo, True) 
5580
0f5d29acc8ea
Don't step into libraries after a QThread or threading.Thread has finished.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5561
diff
changeset

397  finally: 
0f5d29acc8ea
Don't step into libraries after a QThread or threading.Thread has finished.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5561
diff
changeset

398  sys.settrace(None) 
5552
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

399  
5240
71c51aae2f4e
Use importlib instead __import__ because it returns the correct module directly.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5228
diff
changeset

400  class QThreadWrapper(module.QThread): 
6196
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
6195
diff
changeset

401  """ 
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
6195
diff
changeset

402  Wrapper class for *.QThread. 
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
6195
diff
changeset

403  """ 
5228
4332a6751b14
Support for debugging of QThread processes added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5222
diff
changeset

404  def __init__(self, *args, **kwargs): 
6196
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
6195
diff
changeset

405  """ 
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
6195
diff
changeset

406  Constructor 
7135a692e43e
Some code style changes and regeneration of source docu.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
6195
diff
changeset

407  """ 
5228
4332a6751b14
Support for debugging of QThread processes added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5222
diff
changeset

408  # Overwrite the provided run method with our own, to 
4332a6751b14
Support for debugging of QThread processes added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5222
diff
changeset

409  # intercept the thread creation by Qt 
5552
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

410  self.run = lambda s=self, run=self.run: ( 
313a91a38aed
Handling of unhandled exceptions for *.QThread. Namespace is clean now.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5544
diff
changeset

411  _bootstrapQThread(s, run)) 
5228
4332a6751b14
Support for debugging of QThread processes added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5222
diff
changeset

412  
4332a6751b14
Support for debugging of QThread processes added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5222
diff
changeset

413  super(QThreadWrapper, self).__init__(*args, **kwargs) 
4332a6751b14
Support for debugging of QThread processes added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5222
diff
changeset

414  
7413
424f88fdee04
ThreadExtension: added debugging support for QRunable.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7364
diff
changeset

415  class QRunnableWrapper(module.QRunnable): 
424f88fdee04
ThreadExtension: added debugging support for QRunable.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7364
diff
changeset

416  """ 
424f88fdee04
ThreadExtension: added debugging support for QRunable.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7364
diff
changeset

417  Wrapper class for *.QRunnable. 
424f88fdee04
ThreadExtension: added debugging support for QRunable.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7364
diff
changeset

418  """ 
424f88fdee04
ThreadExtension: added debugging support for QRunable.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7364
diff
changeset

419  def __init__(self, *args, **kwargs): 
424f88fdee04
ThreadExtension: added debugging support for QRunable.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7364
diff
changeset

420  """ 
424f88fdee04
ThreadExtension: added debugging support for QRunable.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7364
diff
changeset

421  Constructor 
424f88fdee04
ThreadExtension: added debugging support for QRunable.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7364
diff
changeset

422  """ 
424f88fdee04
ThreadExtension: added debugging support for QRunable.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7364
diff
changeset

423  # Overwrite the provided run method with our own, to 
424f88fdee04
ThreadExtension: added debugging support for QRunable.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7364
diff
changeset

424  # intercept the thread creation by Qt 
424f88fdee04
ThreadExtension: added debugging support for QRunable.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7364
diff
changeset

425  self.run = lambda s=self, run=self.run: ( 
424f88fdee04
ThreadExtension: added debugging support for QRunable.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7364
diff
changeset

426  _bootstrapQThread(s, run)) 
424f88fdee04
ThreadExtension: added debugging support for QRunable.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7364
diff
changeset

427  
424f88fdee04
ThreadExtension: added debugging support for QRunable.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7364
diff
changeset

428  super(QRunnableWrapper, self).__init__(*args, **kwargs) 
424f88fdee04
ThreadExtension: added debugging support for QRunable.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7364
diff
changeset

429  
5228
4332a6751b14
Support for debugging of QThread processes added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5222
diff
changeset

430  module.QThread = QThreadWrapper 
7413
424f88fdee04
ThreadExtension: added debugging support for QRunable.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
7364
diff
changeset

431  module.QRunnable = QRunnableWrapper 
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

432  
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

433  self.enableImportHooks = True 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

434  return module 