DebugClients/Python/ThreadExtension.py

branch
debugger speed
changeset 5209
cd058aa6af37
parent 5208
aa8045780ce4
child 5221
960afd19c1b6
equal deleted inserted replaced
5208:aa8045780ce4 5209:cd058aa6af37
30 def __init__(self): 30 def __init__(self):
31 """ 31 """
32 Constructor 32 Constructor
33 """ 33 """
34 self.threadNumber = 1 34 self.threadNumber = 1
35 self.enableImportHooks = True
35 self._original_start_new_thread = None 36 self._original_start_new_thread = None
36 37
37 self.clientLock = threading.RLock() 38 self.clientLock = threading.RLock()
38 39
39 # dictionary of all threads running {id: DebugBase} 40 # dictionary of all threads running {id: DebugBase}
51 self.threadModName = '_thread' 52 self.threadModName = '_thread'
52 53
53 # reset already imported thread module to apply hooks at next import 54 # reset already imported thread module to apply hooks at next import
54 del sys.modules[self.threadModName] 55 del sys.modules[self.threadModName]
55 del sys.modules['threading'] 56 del sys.modules['threading']
57
58 sys.meta_path.insert(0, self)
56 59
57 # provide a hook to perform a hard breakpoint 60 # provide a hook to perform a hard breakpoint
58 # Use it like this: 61 # Use it like this:
59 # if hasattr(sys, 'breakpoint): sys.breakpoint() 62 # if hasattr(sys, 'breakpoint): sys.breakpoint()
60 sys.breakpoint = self.set_trace 63 sys.breakpoint = self.set_trace
77 """ 80 """
78 if mainThread: 81 if mainThread:
79 ident = _thread.get_ident() 82 ident = _thread.get_ident()
80 name = 'MainThread' 83 name = 'MainThread'
81 newThread = self.mainThread 84 newThread = self.mainThread
82 newThread._mainThread = True 85 newThread.isMainThread = True
83 if self.debugging: 86 if self.debugging:
84 sys.setprofile(newThread.profile) 87 sys.setprofile(newThread.profile)
85 88
86 else: 89 else:
87 newThread = DebugBase(self) 90 newThread = DebugBase(self)
95 98
96 self.threads[ident] = newThread 99 self.threads[ident] = newThread
97 100
98 return ident 101 return ident
99 102
100 def threadTerminated(self, dbgThread): 103 def threadTerminated(self, threadId):
101 """ 104 """
102 Public method called when a DebugThread has exited. 105 Public method called when a DebugThread has exited.
103 106
104 @param dbgThread the DebugThread that has exited 107 @param threadId id of the DebugThread that has exited
108 @type int
105 """ 109 """
106 self.lockClient() 110 self.lockClient()
107 try: 111 try:
108 del self.threads[dbgThread.get_ident()] 112 del self.threads[threadId]
109 except KeyError: 113 except KeyError:
110 pass 114 pass
111 finally: 115 finally:
112 self.unlockClient() 116 self.unlockClient()
113 117
140 144
141 @param id the id the current thread should be set to. 145 @param id the id the current thread should be set to.
142 """ 146 """
143 try: 147 try:
144 self.lockClient() 148 self.lockClient()
145 if id is None or id not in self.threads: 149 if id is None:
146 self.currentThread = None 150 self.currentThread = None
147 else: 151 else:
148 self.currentThread = self.threads[id] 152 self.currentThread = self.threads.get(id)
149 finally: 153 finally:
150 self.unlockClient() 154 self.unlockClient()
151 155
152 def dumpThreadList(self): 156 def dumpThreadList(self):
153 """ 157 """
178 182
179 self.sendJsonCommand("ResponseThreadList", { 183 self.sendJsonCommand("ResponseThreadList", {
180 "currentID": currentId, 184 "currentID": currentId,
181 "threadList": threadList, 185 "threadList": threadList,
182 }) 186 })
187
188 def find_module(self, fullname, path=None):
189 """
190 Public method returning the module loader.
191
192 @param fullname name of the module to be loaded
193 @type str
194 @param path path to resolve the module name
195 @type str
196 @return module loader object
197 @rtype object
198 """
199 if fullname in sys.modules or not self.debugging:
200 return None
201 if fullname == self.threadModName and self.enableImportHooks:
202 # Disable hook to be able to import original module
203 self.enableImportHooks = False
204 return self
205
206 return None
207
208 def load_module(self, fullname):
209 """
210 Public method to load a module.
211
212 @param fullname name of the module to be loaded
213 @type str
214 @return reference to the loaded module
215 @rtype module
216 """
217 module = __import__(fullname)
218 sys.modules[fullname] = module
219 if (fullname == self.threadModName and
220 self._original_start_new_thread is None):
221 # make thread hooks available to system
222 self._original_start_new_thread = module.start_new_thread
223 module.start_new_thread = self.attachThread
224
225 self.enableImportHooks = True
226 return module
183 227
184 228
185 # 229 #
186 # eflag: noqa = M702 230 # eflag: noqa = M702

eric ide

mercurial