bdb --- 偵錯器框架¶
原始碼:Lib/bdb.py
The bdb module handles basic debugger functions, like setting breakpoints
or managing execution via the debugger.
有定義以下例外:
bdb 模組也定義了兩個類別:
- class bdb.Breakpoint(self, file, line, temporary=False, cond=None, funcname=None)¶
This class implements temporary breakpoints, ignore counts, disabling and (re-)enabling, and conditionals.
Breakpoints are indexed by number through a list called
bpbynumberand by(file, line)pairs throughbplist. The former points to a single instance of classBreakpoint. The latter points to a list of such instances since there may be more than one breakpoint per line.When creating a breakpoint, its associated
file nameshould be in canonical form. If afuncnameis defined, a breakpointhitwill be counted when the first line of that function is executed. Aconditionalbreakpoint always counts ahit.Breakpoint實例有以下方法:- deleteMe()¶
Delete the breakpoint from the list associated to a file/line. If it is the last breakpoint in that position, it also deletes the entry for the file/line.
- enable()¶
Mark the breakpoint as enabled.
- disable()¶
Mark the breakpoint as disabled.
- bpformat()¶
Return a string with all the information about the breakpoint, nicely formatted:
Breakpoint number.
Temporary status (del or keep).
File/line position.
Break condition.
Number of times to ignore.
Number of times hit.
在 3.2 版被加入.
- bpprint(out=None)¶
Print the output of
bpformat()to the file out, or if it isNone, to standard output.
Breakpoint實例有以下屬性:- file¶
Breakpoint的檔案名稱。
- line¶
Line number of the
Breakpointwithinfile.
- temporary¶
Trueif aBreakpointat (file, line) is temporary.
- cond¶
Condition for evaluating a
Breakpointat (file, line).
- funcname¶
Function name that defines whether a
Breakpointis hit upon entering the function.
- enabled¶
如
Breakpoint有被啟用則為True。
- bpbynumber¶
Numeric index for a single instance of a
Breakpoint.
- bplist¶
Dictionary of
Breakpointinstances indexed by (file,line) tuples.
- ignore¶
Number of times to ignore a
Breakpoint.
- hits¶
Count of the number of times a
Breakpointhas been hit.
- class bdb.Bdb(skip=None, backend='settrace')¶
The
Bdbclass acts as a generic Python debugger base class.This class takes care of the details of the trace facility; a derived class should implement user interaction. The standard debugger class (
pdb.Pdb) is an example.The skip argument, if given, must be an iterable of glob-style module name patterns. The debugger will not step into frames that originate in a module that matches one of these patterns. Whether a frame is considered to originate in a certain module is determined by the
__name__in the frame globals.The backend argument specifies the backend to use for
Bdb. It can be either'settrace'or'monitoring'.'settrace'usessys.settrace()which has the best backward compatibility. The'monitoring'backend uses the newsys.monitoringthat was introduced in Python 3.12, which can be much more efficient because it can disable unused events. We are trying to keep the exact interfaces for both backends, but there are some differences. The debugger developers are encouraged to use the'monitoring'backend to achieve better performance.在 3.1 版的變更: 新增 skip 引數。
在 3.14 版的變更: 新增 backend 參數。
The following methods of
Bdbnormally don't need to be overridden.- canonic(filename)¶
Return canonical form of filename.
For real file names, the canonical form is an operating-system-dependent,
case-normalizedabsolute path. A filename with angle brackets, such as"<stdin>"generated in interactive mode, is returned unchanged.
- start_trace(self)¶
Start tracing. For
'settrace'backend, this method is equivalent tosys.settrace(self.trace_dispatch)在 3.14 版被加入.
- stop_trace(self)¶
Stop tracing. For
'settrace'backend, this method is equivalent tosys.settrace(None)在 3.14 版被加入.
- reset()¶
Set the
botframe,stopframe,returnframeandquittingattributes with values ready to start debugging.
- trace_dispatch(frame, event, arg)¶
This function is installed as the trace function of debugged frames. Its return value is the new trace function (in most cases, that is, itself).
The default implementation decides how to dispatch a frame, depending on the type of event (passed as a string) that is about to be executed. event can be one of the following:
"line": A new line of code is going to be executed."call": A function is about to be called, or another code block entered."return": A function or other code block is about to return."exception": An exception has occurred."c_call": A C function is about to be called."c_return": A C function has returned."c_exception": A C function has raised an exception.
For the Python events, specialized functions (see below) are called. For the C events, no action is taken.
The arg parameter depends on the previous event.
See the documentation for
sys.settrace()for more information on the trace function. For more information on code and frame objects, refer to 標準型別階層.
- dispatch_line(frame)¶
If the debugger should stop on the current line, invoke the
user_line()method (which should be overridden in subclasses). Raise aBdbQuitexception if thequittingflag is set (which can be set fromuser_line()). Return a reference to thetrace_dispatch()method for further tracing in that scope.
- dispatch_call(frame, arg)¶
If the debugger should stop on this function call, invoke the
user_call()method (which should be overridden in subclasses). Raise aBdbQuitexception if thequittingflag is set (which can be set fromuser_call()). Return a reference to thetrace_dispatch()method for further tracing in that scope.
- dispatch_return(frame, arg)¶
If the debugger should stop on this function return, invoke the
user_return()method (which should be overridden in subclasses). Raise aBdbQuitexception if thequittingflag is set (which can be set fromuser_return()). Return a reference to thetrace_dispatch()method for further tracing in that scope.
- dispatch_exception(frame, arg)¶
If the debugger should stop at this exception, invokes the
user_exception()method (which should be overridden in subclasses). Raise aBdbQuitexception if thequittingflag is set (which can be set fromuser_exception()). Return a reference to thetrace_dispatch()method for further tracing in that scope.
Normally derived classes don't override the following methods, but they may if they want to redefine the definition of stopping and breakpoints.
- is_skipped_module(module_name)¶
Return
Trueif module_name matches any skip pattern.
- stop_here(frame)¶
Return
Trueif frame is below the starting frame in the stack.
- break_here(frame)¶
Return
Trueif there is an effective breakpoint for this line.Check whether a line or function breakpoint exists and is in effect. Delete temporary breakpoints based on information from
effective().
- break_anywhere(frame)¶
Return
Trueif any breakpoint exists for frame's filename.
Derived classes should override these methods to gain control over debugger operation.
- user_call(frame, argument_list)¶
Called from
dispatch_call()if a break might stop inside the called function.argument_list is not used anymore and will always be
None. The argument is kept for backwards compatibility.
- user_line(frame)¶
Called from
dispatch_line()when eitherstop_here()orbreak_here()returnsTrue.
- user_return(frame, return_value)¶
Called from
dispatch_return()whenstop_here()returnsTrue.
- user_exception(frame, exc_info)¶
Called from
dispatch_exception()whenstop_here()returnsTrue.
- do_clear(arg)¶
Handle how a breakpoint must be removed when it is a temporary one.
This method must be implemented by derived classes.
Derived classes and clients can call the following methods to affect the stepping state.
- set_step()¶
Stop after one line of code.
- set_next(frame)¶
Stop on the next line in or below the given frame.
- set_return(frame)¶
Stop when returning from the given frame.
- set_until(frame,