source: trunk/essentials/dev-lang/python/Lib/bdb.py@ 3298

Last change on this file since 3298 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 19.7 KB
Line 
1"""Debugger basics"""
2
3import sys
4import os
5import types
6
7__all__ = ["BdbQuit","Bdb","Breakpoint"]
8
9class BdbQuit(Exception):
10 """Exception to give up completely"""
11
12
13class Bdb:
14
15 """Generic Python debugger base class.
16
17 This class takes care of details of the trace facility;
18 a derived class should implement user interaction.
19 The standard debugger class (pdb.Pdb) is an example.
20 """
21
22 def __init__(self):
23 self.breaks = {}
24 self.fncache = {}
25
26 def canonic(self, filename):
27 if filename == "<" + filename[1:-1] + ">":
28 return filename
29 canonic = self.fncache.get(filename)
30 if not canonic:
31 canonic = os.path.abspath(filename)
32 canonic = os.path.normcase(canonic)
33 self.fncache[filename] = canonic
34 return canonic
35
36 def reset(self):
37 import linecache
38 linecache.checkcache()
39 self.botframe = None
40 self.stopframe = None
41 self.returnframe = None
42 self.quitting = 0
43
44 def trace_dispatch(self, frame, event, arg):
45 if self.quitting:
46 return # None
47 if event == 'line':
48 return self.dispatch_line(frame)
49 if event == 'call':
50 return self.dispatch_call(frame, arg)
51 if event == 'return':
52 return self.dispatch_return(frame, arg)
53 if event == 'exception':
54 return self.dispatch_exception(frame, arg)
55 if event == 'c_call':
56 return self.trace_dispatch
57 if event == 'c_exception':
58 return self.trace_dispatch
59 if event == 'c_return':
60 return self.trace_dispatch
61 print 'bdb.Bdb.dispatch: unknown debugging event:', repr(event)
62 return self.trace_dispatch
63
64 def dispatch_line(self, frame):
65 if self.stop_here(frame) or self.break_here(frame):
66 self.user_line(frame)
67 if self.quitting: raise BdbQuit
68 return self.trace_dispatch
69
70 def dispatch_call(self, frame, arg):
71 # XXX 'arg' is no longer used
72 if self.botframe is None:
73 # First call of dispatch since reset()
74 self.botframe = frame.f_back # (CT) Note that this may also be None!
75 return self.trace_dispatch
76 if not (self.stop_here(frame) or self.break_anywhere(frame)):
77 # No need to trace this function
78 return # None
79 self.user_call(frame, arg)
80 if self.quitting: raise BdbQuit
81 return self.trace_dispatch
82
83 def dispatch_return(self, frame, arg):
84 if self.stop_here(frame) or frame == self.returnframe:
85 self.user_return(frame, arg)
86 if self.quitting: raise BdbQuit
87 return self.trace_dispatch
88
89 def dispatch_exception(self, frame, arg):
90 if self.stop_here(frame):
91 self.user_exception(frame, arg)
92 if self.quitting: raise BdbQuit
93 return self.trace_dispatch
94
95 # Normally derived classes don't override the following
96 # methods, but they may if they want to redefine the
97 # definition of stopping and breakpoints.
98
99 def stop_here(self, frame):
100 # (CT) stopframe may now also be None, see dispatch_call.
101 # (CT) the former test for None is therefore removed from here.
102 if frame is self.stopframe:
103 return True
104 while frame is not None and frame is not self.stopframe:
105 if frame is self.botframe:
106 return True
107 frame = frame.f_back
108 return False
109
110 def break_here(self, frame):
111 filename = self.canonic(frame.f_code.co_filename)
112 if not filename in self.breaks:
113 return False
114 lineno = frame.f_lineno
115 if not lineno in self.breaks[filename]:
116 # The line itself has no breakpoint, but maybe the line is the
117 # first line of a function with breakpoint set by function name.
118 lineno = frame.f_code.co_firstlineno
119 if not lineno in self.breaks[filename]:
120 return False
121
122 # flag says ok to delete temp. bp
123 (bp, flag) = effective(filename, lineno, frame)
124 if bp:
125 self.currentbp = bp.number
126 if (flag and bp.temporary):
127 self.do_clear(str(bp.number))
128 return True
129 else:
130 return False
131
132 def do_clear(self, arg):
133 raise NotImplementedError, "subclass of bdb must implement do_clear()"
134
135 def break_anywhere(self, frame):
136 return self.breaks.has_key(
137 self.canonic(frame.f_code.co_filename))
138
139 # Derived classes should override the user_* methods
140 # to gain control.
141
142 def user_call(self, frame, argument_list):
143 """This method is called when there is the remote possibility
144 that we ever need to stop in this function."""
145 pass
146
147 def user_line(self, frame):
148 """This method is called when we stop or break at this line."""
149 pass
150
151 def user_return(self, frame, return_value):
152 """This method is called when a return trap is set here."""
153 pass
154
155 def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
156 """This method is called if an exception occurs,
157 but only if we are to stop at or just below this level."""
158 pass
159
160 # Derived classes and clients can call the following methods
161 # to affect the stepping state.
162