DebugClients/Python/DebugThread.py

changeset 0
de9c2efb9d02
child 13
1af94a91f439
equal deleted inserted replaced
-1:000000000000 0:de9c2efb9d02
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2002 - 2009 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the debug thread.
8 """
9
10 import bdb
11 import os
12 import sys
13
14 from DebugBase import *
15
16 class DebugThread(DebugBase):
17 """
18 Class implementing a debug thread.
19
20 It represents a thread in the python interpreter that we are tracing.
21
22 Provides simple wrapper methods around bdb for the 'owning' client to
23 call to step etc.
24 """
25 def __init__(self, dbgClient, targ = None, args = None, kwargs = None,
26 mainThread = 0):
27 """
28 Constructor
29
30 @param dbgClient the owning client
31 @param targ the target method in the run thread
32 @param args arguments to be passed to the thread
33 @param kwargs arguments to be passed to the thread
34 @param mainThread 0 if this thread is not the mainscripts thread
35 """
36 DebugBase.__init__(self, dbgClient)
37
38 self._target = targ
39 self._args = args
40 self._kwargs = kwargs
41 self._mainThread = mainThread
42 # thread running tracks execution state of client code
43 # it will always be 0 for main thread as that is tracked
44 # by DebugClientThreads and Bdb...
45 self._threadRunning = 0
46
47 self.__ident = None # id of this thread.
48 self.__name = ""
49
50 def set_ident(self, id):
51 """
52 Public method to set the id for this thread.
53
54 @param id id for this thread (int)
55 """
56 self.__ident = id
57
58 def get_ident(self):
59 """
60 Public method to return the id of this thread.
61
62 @return the id of this thread (int)
63 """
64 return self.__ident
65
66 def get_name(self):
67 """
68 Public method to return the name of this thread.
69
70 @return name of this thread (string)
71 """
72 return self.__name
73
74 def traceThread(self):
75 """
76 Private method to setup tracing for this thread.
77 """
78 self.set_trace()
79 if not self._mainThread:
80 self.set_continue(0)
81
82 def bootstrap(self):
83 """
84 Private method to bootstrap the thread.
85
86 It wraps the call to the user function to enable tracing
87 before hand.
88 """
89 try:
90 try:
91 self._threadRunning = 1
92 self.traceThread()
93 self._target(*self._args, **self._kwargs)
94 except bdb.BdbQuit:
95 pass
96 finally:
97 self._threadRunning = 0
98 self.quitting = 1
99 self._dbgClient.threadTerminated(self)
100 sys.settrace(None)
101 sys.setprofile(None)
102
103 def trace_dispatch(self, frame, event, arg):
104 """
105 Private method wrapping the trace_dispatch of bdb.py.
106
107 It wraps the call to dispatch tracing into
108 bdb to make sure we have locked the client to prevent multiple
109 threads from entering the client event loop.
110
111 @param frame The current stack frame.
112 @param event The trace event (string)
113 @param arg The arguments
114 @return local trace function
115 """
116 try:
117 self._dbgClient.lockClient()
118 # if this thread came out of a lock, and we are quitting
119 # and we are still running, then get rid of tracing for this thread
120 if self.quitting and self._threadRunning:
121 sys.settrace(None)
122 sys.setprofile(None)
123 import threading
124 self.__name = threading.currentThread().getName()
125 retval = DebugBase.trace_dispatch(self, frame, event, arg)
126 finally:
127 self._dbgClient.unlockClient()
128
129 return retval

eric ide

mercurial