26.2. pdbThe Python Debugger

模块pdb为Python 程序定义了一个交互式的源代码调试器。它支持在任何堆栈帧的上下文中设置 (条件) 断点和单步执行源代码行级别的检查堆栈帧、 源代码清单,和任意的 Python 代码的评价。它还支持验尸调试,并可以调用在程序控制下。

调试器是可扩展的 — — 它实际上被定义为类Pdb这是目前没有文档,但容易通过读取源文件来理解。扩展接口使用模块bdbcmd

调试器的提示符是(Pdb)若要运行程序在调试器的控制下的典型用法是:

>>> import pdb
>>> import mymodule
>>> pdb.run('mymodule.test()')
> <string>(0)?()
(Pdb) continue
> <string>(1)?()
(Pdb) continue
NameError: 'spam'
> <string>(1)?()
(Pdb)

pdb.py也可以作为一个脚本来调试其他脚本调用。举个例子:

python -m pdb myscript.py

当调用作为脚本,pdb 将自动进入验尸调试如果正在调试的程序异常退出。在验尸调试 (或之后正常退出程序的),pdb 将重新启动该程序。自动重新启动保留 pdb 的状态 (如断点),并在大多数情况下是比退出调试器在程序退出时更有用。

新在 2.4 版本:重新启动的验尸行为补充说。

典型的用法来中断并进入调试器从正在运行的程序是要插入

import pdb; pdb.set_trace()

在的位置,您想要中断并进入调试器。可以单步执行该语句后面的语句的代码,然后可以不使用调试器使用c命令继续运行。

典型的用法来检查一个坠毁的程序是:

>>> import pdb
>>> import mymodule
>>> mymodule.test()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "./mymodule.py", line 4, in test
    test2()
  File "./mymodule.py", line 3, in test2
    print spam
NameError: spam
>>> pdb.pm()
> ./mymodule.py(3)test2()
-> print spam
(Pdb)

模块定义了以下的功能 ;每一个进入调试器的方式略有不同:

pdb.run(statement[, globals[, locals]])

执行该语句(以字符串形式给出) 在调试器的控制下。调试器提示出现之前执行任何代码 ;您可以设置断点并键入continue,或者你可以逐句通过语句,并且使用stepnext(所有这些命令解释见下文)。全局变量和局部变量的可选参数指定的环境中执行的代码 ;默认情况下的模块__main__字典使用。(见解释的exec语句或eval ()内置函数)。

pdb.runeval(expression[, globals[, locals]])

评估给定的表达式(作为字符串) 在调试器的控制下。 Runeval()返回时,它返回表达式的值。否则此函数是类似于run ()

pdb.runcall(function[, argument, ...])

调用函数(一个函数或方法的对象,不是字符串) 与给定的参数。 Runcall()返回时,它将返回任何函数调用返回。只要输入函数,就会显示该调试器提示。

pdb.set_trace()

进入调试器在调用的堆栈帧。硬编码断点在某一给定的程序中很有用, 即使代码没有另外被调试 (例如当断言失败时)。

pdb.post_mortem([traceback])

输入给定的追踪对象的事后调试。如果给出了无回溯,它将使用当前正在处理的异常之一 (必须被处理异常如果默认设置是使用)。

pdb.pm()

输入事后调试的在sys.last_traceback中找到的追踪。

Set_trace()运行 *职能是名称的Pdb类实例化和调用的方法相同的别名。如果你想要获得进一步的功能,你必须做这件事:

class pdb.Pdb(completekey='tab', stdin=None, stdout=None, skip=None)

Pdb是调试器类。

completekey标准输入标准输出的参数传递到底层的cmd.Cmd类 ;看到那里的描述。

跳过的论点,如果给定,必须是可迭代的 glob 样式模块名称。调试器不会进入起源于一个模块,其中一种模式匹配的帧。[] 1

启用跟踪与跳过的调用示例:

import pdb; pdb.Pdb(skip=['django.*']).set_trace()

新在 2.7 版本:跳过的论点。

run(statement[, globals[, locals]])
runeval(expression[, globals[, locals]])
runcall(function[, argument, ...])
set_trace()

请参阅上文的功能的文档。

26.3. 调试命令

调试器能够识别以下命令。大多数命令可以简化为一个或两个字母 ;例如h(elp)意味着可以用hhelp,来输入帮助命令 (但不是hehel,也不是HHELP )。命令的参数必须用空白 (空格或制表符) 隔开。可选参数括在方括号 ([]) 中的命令的语法 ;不,必须键入方括号。在该命令的语法的替代品被隔开的垂直栏 (|)。

输入一个空行重复输入的最后一个命令。例外: 如果上次执行的命令是列表命令,列出下一步 11 行。

调试器不能识别的命令被假定为 Python 语句和正在调试的程序的上下文中执行。Python 语句也可以有一个带惊叹号 (!) 作为前缀。这是强大的方式来检查程序正在调试 ;它是甚至可以改变一个变量或调用一个函数。当在此类声明中出现异常时,异常名称打印,但调试器的状态不会更改。

可能在单个行上,以分隔输入多个命令; ;(单个;不是使用原样传递到 Python 语法分析器的行中的多个命令分隔符。没有智能应用于分离命令 ;输入拆分第一; ;配对,即使它是在带引号的字符串。

调试器支持别名。别名可以有参数,它允许一个一定水平的背景下考试的适应性。

如果.pdbrc文件存在于用户的主目录或当前目录中,它是在读,因为如果它已被类型在调试器提示符下执行。这是特别有用的别名。如果这两个文件存在,主目录中的一个先读,那里定义别名可以通过本地文件重写。

h(elp) [command]
Without argument, print the list of available commands. With a command as argument, print help about that command. help pdb displays the full documentation file; if the environment variable PAGER is defined, the file is piped through that command instead. Since the command argument must be an identifier, help exec must be entered to get help on the ! command.
w(here)
Print a stack trace, with the most recent frame at the bottom. An arrow indicates the current frame, which determines the context of most commands.
d(own)
Move the current frame one level down in the stack trace (to a newer frame).
u(p)
Move the current frame one level up in the stack trace (to an older frame).
b(reak) [[filename:]lineno | function[, condition]]

空格符参数,当前文件中设置那里休息。函数的参数,在该函数中的第一个可执行语句设置休息。行号可能使用一个文件名和一个冒号,在另一个文件 (可能是尚未加载) 中指定的断点作为前缀。sys.path上搜索文件。请注意每个断点被分配到的所有其他断点命令引用一个编号。

如果存在第二个参数,则它是一个表达式的计算结果必须为 true 之前断点很荣幸。

如果没有参数,列出的所有分页符,包括每个断点的次数该断点已命中、 当前忽略计数和关联的条件如果任何。

tbreak [[filename:]lineno | function[, condition]]
Temporary breakpoint, which is removed automatically when it is first hit. The arguments are the same as break.
cl(ear) [filename:lineno | bpnumber [bpnumber ...]]
With a filename:lineno argument, clear all the breakpoints at this line. With a space separated list of breakpoint numbers, clear those breakpoints. Without argument, clear all breaks (but first ask confirmation).
disable [bpnumber [bpnumber ...]]
Disables the breakpoints given as a space separated list of breakpoint numbers. Disabling a breakpoint means it cannot cause the program to stop execution, but unlike clearing a breakpoint, it remains in the list of breakpoints and can be (re-)enabled.
enable [bpnumber [bpnumber ...]]
Enables the breakpoints specified.
ignore bpnumber [count]
Sets the ignore count for the given breakpoint number. If count is omitted, the ignore count is set to 0. A breakpoint becomes active when the ignore count is zero. When non-zero, the count is decremented each time the breakpoint is reached and the breakpoint is not disabled and any associated condition evaluates to true.
condition bpnumber [condition]
Condition is an expression which must evaluate to true before the breakpoint is honored. If condition is absent, any existing condition is removed; i.e., the breakpoint is made unconditional.
commands [bpnumber]

指定的断点编号bpnumber的命令的列表。命令本身出现在以下各行上。键入包含只是 '尾' 终止的命令行。示例:

(Pdb) commands 1
(com) print some_variable
(com) end
(Pdb)

若要删除所有命令从断点,键入命令并按照它立即结束 ;那就是,给没有的命令。

没有bpnumber参数,命令到最后一个引用设置断点。

您可以使用断点命令来重新启动您的程序。只需使用继续命令,或步骤或继续执行的任何其他命令。

指定恢复执行的任何命令 (目前继续步下, 一步,返回,跳跃,退出和他们的缩写) 终止命令列表 (如果该命令后紧跟结束)。这是因为任何时候你恢复执行 (即使是使用一个简单的下一步或步骤),您可能会遇到另一个断点 — — 这可能会导致歧义有关哪个列表来执行它自己命令列表。

如果您使用无提示命令在命令列表中,不会打印在断点处停止的一般信息。这可能是可取的断点,并打印一条特定的消息,然后继续。如果没有其他命令打印任何东西,你看到没有迹象表明在到达该断点。

新版本 2.5 中的。

s(tep)
Execute the current line, stop at the first possible occasion (either in a function that is called or on the next line in the current function).
n(ext)
Continue execution until the next line in the current function is reached or it returns. (The difference between next and step is that step stops inside a called function, while next executes called functions at (nearly) full speed, only stopping at the next line in the current function.)
unt(il)

继续执行直到线与更大的不是达到了当前或返回从当前帧时的行号。

在 2.6 版本新。

r(eturn)
Continue execution until the current function returns.
c(ont(inue))
Continue execution, only stop when a breakpoint is encountered.
j(ump) lineno

设置将执行的下一行。只有在最底层框架中才可用。这允许您跳回来,再次,执行代码或向前跳,跳过你不想要运行的代码。

应该指出的是,并不是所有的跳跃允许 — — 例如它是不可能跳到中间的for循环或从finally子句。

l(ist) [first[, last]]
List source code for the current file. Without arguments, list 11 lines around the current line or continue the previous listing. With one argument, list 11 lines around at that line. With two arguments, list the given range; if the second argument is less than the first, it is interpreted as a count.
a(rgs)
Print the argument list of the current function.
p expression

计算在当前上下文中的表达式并打印它的价值。

打印也可以使用,但不是一个调试器命令 — — 这执行 Python打印语句。

pp expression
Like the p command, except the value of the expression is pretty-printed using the pprint module.
alias [name [command]]

创建执行命令的别名叫的名字该命令必须能用引号括起来。可以通过%1和等等,表示可替换的参数,而%*取而代之的是所有的参数。如果给出没有命令,会显示当前别名的名称如果给不出任何参数,则列出所有别名。

别名可以嵌套,并且可以包含任何可以合法类型的 pdb 提示符。请注意那条内部 pdb 命令,可以通过别名重写。这种命令之前删除别名,它就是那么隐藏的。别名是以递归方式应用的第一个单词的命令行 ;在行中的所有其他单词还有孤独。

作为一个例子,下面是两个有用的别名 (尤其当放在.pdbrc文件中):

#Print instance variables (usage "pi classInst")
alias pi for k in %1.__dict__.keys(): print "%1.",k,"=",%1.__dict__[k]
#Print instance variables in self
alias ps pi self
unalias name
Deletes the specified alias.
[!]statement

在当前的堆栈帧的上下文中执行 (一线)的声明除非声明的第一个词就像一个调试器命令,可以省去感叹号。若要设置一个全局变量,您可以如前缀指配命令与同一行中,全局命令:

(Pdb) global list_options; list_options = ['-l']
(Pdb)
run [args ...]

重新启动调试的 Python 程序。如果提供的参数,则它分裂与"shlex",结果用作新 sys.argv。历史、 断点、 行动和调试器选项将保留。"重启"是"运行"的别名。

在 2.6 版本新。

q(uit)
Quit from the debugger. The program being executed is aborted.

脚注

[1]Whether a frame is considered to originate in a certain module is determined by the __name__ in the frame globals.