pdb中"next"和"until"之间的区别是什么

Eri*_*ric 5 python pdb

我正在使用Python 2.6.6,并使用pdb来调试我的Python程序,但我不清楚pdb中"next"和"until"之间的区别是什么,看起来它们都将继续执行直到下一行在当前的功能.

unu*_*tbu 9

pdb 帮助文档是这样描述的:

(Pdb) help next
n(ext)
Continue execution until the next line in the current function
is reached or it returns.

(Pdb) help until
unt(il)
Continue execution until the line with a number greater than the current
one is reached or until the current frame returns
Run Code Online (Sandbox Code Playgroud)

更有帮助的是Doug Hellman 在他的本周 Python 模块教程中给出了一个例子,说明了不同之处:

until 命令与 next 类似,不同之处在于它明确地继续执行,直到执行到达同一函数中行号高于当前值的行。这意味着,例如,until 可用于越过循环的结尾。

pdb_next.py

import pdb

def calc(i, n):
    j = i * n
    return j

def f(n):
    for i in range(n):
        j = calc(i, n)
        print i, j
    return

if __name__ == '__main__':
    pdb.set_trace()
    f(5)
Run Code Online (Sandbox Code Playgroud)
$ python pdb_next.py
> .../pdb_next.py(21)<module>()
-> f(5)
(Pdb) step
--Call--
> .../pdb_next.py(13)f()
-> def f(n):

(Pdb) step
> .../pdb_next.py(14)f()
-> for i in range(n):

(Pdb) step
> .../pdb_next.py(15)f()
-> j = calc(i, n)

(Pdb) next
> .../pdb_next.py(16)f()
-> print i, j

(Pdb) until
0 0
1 5
2 10
3 15
4 20
> .../pdb_next.py(17)f()
-> return

(Pdb)
Run Code Online (Sandbox Code Playgroud)

在运行 until 之前,当前行是 16,即循环的最后一行。在直到运行之后,执行在第 17 行,并且循环已经用完。

的目的until同名的 gdb 命令共享:

直到

继续运行,直到到达当前堆栈帧中超过当前行的源行。此命令用于避免多次单步执行循环。和next命令一样,只不过until遇到跳转时,会自动继续执行,直到程序计数器大于跳转的地址。这意味着当您在单步执行循环后到达循环末尾时,until 使您的程序继续执行,直到它退出循环。相比之下,循环末尾的下一个命令只是简单地退回到循环的开头,这会迫使您单步执行下一次迭代。