上一话题

审计事件表

下一话题
就业培训     下载中心     Wiki     联络
登录   注册

Log
  1. 首页
  2. Python 3.12.4
  3. 索引
  4. 模块
  5. 下一
  6. 上一
  7. Python 标准库
  8. 调试和剖分析
  9. bdb — 调试器框架

bdb — 调试器框架 ¶

源代码: Lib/bdb.py


The bdb 模块处理基本调试器功能,像:设置断点 (或凭借调试器管理执行)。

定义了以下异常:

exception bdb. BdbQuit ¶

异常被引发通过 Bdb 类为离开调试器。

The bdb 模块还定义了 2 个类:

class bdb. Breakpoint ( self , file , line , temporary = False , cond = None , funcname = None ) ¶

此类实现临时断点、忽略计数、禁用和 (重新) 启用及条件。

是通过数字索引断点透过列表称为 bpbynumber 和通过 (file, line) 对透过 bplist 。前者指向单实例化的类 Breakpoint 。后者指向这种实例的列表,由于每行可能有多个断点。

当创建断点时,关联 file name 应该是典型形式。若 funcname 有定义,断点 hit 会计数当执行该函数的第一行时。 conditional 断点始终计数 hit .

Breakpoint 实例具有下列方法:

deleteMe ( ) ¶

从文件/行关联列表删除断点。若它是该位置的最后一个断点,还会删除文件/行条目。

enable ( ) ¶

将断点标记为启用。

disable ( ) ¶

将断点标记为禁用。

bpformat ( ) ¶

返回具有断点所有有关信息的字符串,好的格式:

  • 断点编号。

  • 临时状态 (删除或保持)。

  • 文件/行位置。

  • 中断条件。

  • 忽略次数。

  • 命中次数。

Added in version 3.2.

bpprint ( out = None ) ¶

打印输出的 bpformat() 到文件 out ,或若它为 None ,到标准输出。

Breakpoint 实例拥有下列属性:

file ¶

文件名对于 Breakpoint .

line ¶

行号对于 Breakpoint 在 file .

temporary ¶

True 若 Breakpoint 在 (file, line) 是临时的。

cond ¶

条件为估算 Breakpoint 在 (file, line)。

funcname ¶

函数名称定义是否 Breakpoint 被命中当进入函数时。

enabled ¶

True if Breakpoint 被启用。

bpbynumber ¶

数字索引对于单实例 Breakpoint .

bplist ¶

字典对于 Breakpoint 实例索引通过 ( file , line ) 元组。

ignore ¶

次数为忽略 Breakpoint .

hits ¶

Count of the number of times a Breakpoint has been hit.

class bdb. Bdb ( skip = None ) ¶

The Bdb 类充当一般 Python 调试器基类。

此类负责跟踪设施细节;派生类应实现用户交互。标准调试器类 ( pdb.Pdb ) 是范例。

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.

3.1 版改变: 添加 skip 参数。

下列方法对于 Bdb 通常不需要覆写。

canonic ( filename ) ¶

返回典型形式的 filename .

For real file names, the canonical form is an operating-system-dependent, case-normalized absolute path 。 filename with angle brackets, such as "<stdin>" generated in interactive mode, is returned unchanged.

reset ( ) ¶

设置 botframe , stopframe , returnframe and quitting 属性采用准备开始调试的值。

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 参数从属先前事件。

见文档编制为 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() 方法 (应在子类中覆写)。引发 BdbQuit 异常若 quitting flag is set (which can be set from user_line() ). Return a reference to the trace_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() 方法 (应在子类中覆写)。引发 BdbQuit 异常若 quitting flag is set (which can be set from user_call() ). Return a reference to the trace_dispatch() method for further tracing in that scope.

dispatch_return ( frame , arg ) ¶

若调试器应在此函数返回时停止,援引 user_return() 方法 (应在子类中覆写)。引发 BdbQuit 异常若 quitting flag is set (which can be set from user_return() ). Return a reference to the trace_dispatch() method for further tracing in that scope.

dispatch_exception ( frame , arg ) ¶

若调试器应在此异常处停止,援引 user_exception() 方法 (应在子类中覆写)。引发 BdbQuit 异常若 quitting flag is set (which can be set from user_exception() ). Return a reference to the trace_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_line ( module_name ) ¶

返回 True if module_name 匹配任何跳过模式。

stop_here ( frame ) ¶

返回 True if frame is below the starting frame in the stack.

break_here ( frame ) ¶

返回 True if 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 ) ¶

返回 True if any breakpoint exists for frame ’s filename.

派生类应该覆写这些方法,以获得对调试器操作的控制。

user_call ( frame , argument_list ) ¶

被调用从 dispatch_call() 若中断可能在调用函数内停止。

argument_list is not used anymore and will always be None . The argument is kept for backwards compatibility.

user_line ( frame ) ¶

被调用从 dispatch_line() 当 stop_here() or break_here() 返回 True .

user_return ( frame , return_value ) ¶

被调用从 dispatch_return() 当 stop_here() 返回 True .

user_exception ( frame , exc_info ) ¶

被调用从 dispatch_exception() 当 stop_here() 返回 True .

do_clear ( arg ) ¶

Handle how a breakpoint must be removed when it is a temporary one.

此方法必须由派生类实现。

Derived classes and clients can call the following methods to affect the stepping state.

set_step ( ) ¶

停止在 1 行代码后。

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 , lineno = None ) ¶

Stop when the line with the lineno greater than the current one is reached or when returning from current frame.

set_trace ( [ frame ] ) ¶

Start debugging from frame 。若 frame is not specified, debugging starts from caller’s frame.

set_continue ( ) ¶

Stop only at breakpoints or when finished. If there are no breakpoints, set the system trace function to None .

set_quit ( ) ¶

设置 quitting 属性为 True 。这引发 BdbQuit in the next call to one of the dispatch_*() 方法。

Derived classes and clients can call the following methods to manipulate breakpoints. These methods return a string containing an error message if something went wrong, or None if all is well.

set_break ( filename , lineno , temporary = False , cond = None , funcname = None ) ¶

设置新断点。若 lineno line doesn’t exist for the filename passed as argument, return an error message. The filename should be in canonical form, as described in the canonic() 方法。

clear_break ( filename , lineno ) ¶

Delete the breakpoints in filename and lineno 。若设为 None,返回错误消息。

clear_bpbynumber ( arg ) ¶

Delete the breakpoint which has the index arg 在 Breakpoint.bpbynumber 。若 arg is not numeric or out of range, return an error message.

clear_all_file_breaks ( filename ) ¶

删除所有断点在 filename 。若设为 None,返回错误消息。

clear_all_breaks ( ) ¶

删除所有现有断点。若设为 None,返回错误消息。

get_bpbynumber ( arg ) ¶

Return a breakpoint specified by the given number. If arg is a string, it will be converted to a number. If arg is a non-numeric string, if the given breakpoint never existed or has been deleted, a ValueError 被引发。

Added in version 3.2.

get_break ( filename , lineno ) ¶

返回 True if there is a breakpoint for lineno in filename .

get_breaks ( filename , lineno ) ¶

Return all breakpoints for lineno in filename , or an empty list if none are set.

get_file_breaks ( filename ) ¶

Return all breakpoints in filename , or an empty list if none are set.

get_all_breaks ( ) ¶

返回设置的所有断点。

Derived classes and clients can call the following methods to get a data structure representing a stack trace.

get_stack ( f , t ) ¶

Return a list of (frame, lineno) tuples in a stack trace, and a size.

The most recently called frame is last in the list. The size is the number of frames below the frame where the debugger was invoked.

format_stack_entry ( frame_lineno , lprefix = ': ' ) ¶

Return a string with information about a stack entry, which is a (frame, lineno) tuple. The return string contains:

  • 包含帧的典型文件名。

  • 函数名称或 "<lambda>" .

  • 输入自变量。

  • 返回值。

  • 代码行 (若它存在)。

The following two methods can be called by clients to use a debugger to debug a 语句 , given as a string.

run ( cmd , globals = None , locals = None ) ¶

调试执行语句凭借 exec() 函数。 globals 默认为 __main__.__dict__ , locals 默认为 globals .

runeval ( expr , globals = None , locals = None ) ¶

Debug an expression executed via the eval() 函数。 globals and locals 拥有相同含义如在 run() .

runctx ( cmd , globals , locals ) ¶

为向后兼容。调用 run() 方法。

runcall ( func , / , * args , ** kwds ) ¶

Debug a single function call, and return its result.

Finally, the module defines the following functions:

bdb. checkfuncname ( b , frame ) ¶

返回 True if we should break here, depending on the way the Breakpoint b was set.

If it was set via line number, it checks if b.line is the same as the one in frame . If the breakpoint was set via function name , we have to check we are in the right frame (the right function) and if we are on its first executable line.

bdb. effective ( file , line , frame ) ¶

返回 (active breakpoint, delete temporary flag) or (None, None) as the breakpoint to act upon.

The active breakpoint is the first entry in bplist for the ( file , line ) (which must exist) that is enabled , for which checkfuncname() is true, and that has neither a false condition nor positive ignore count. The flag , meaning that a temporary breakpoint should be deleted, is False only when the cond cannot be evaluated (in which case, ignore count is ignored).

If no such entry exists, then (None, None) 被返回。

bdb. set_trace ( ) ¶

Start debugging with a Bdb instance from caller’s frame.

上一话题

审计事件表

下一话题

faulthandler — 转储 Python 回溯

本页

  • 报告 Bug
  • 展示源

快速搜索

键入搜索术语或模块、类、函数名称。

  1. 首页
  2. Python 3.12.4
  3. 索引
  4. 模块
  5. 下一
  6. 上一
  7. Python 标准库
  8. 调试和剖分析
  9. bdb — 调试器框架

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1