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

时间:2015-01-03 02:36:58

标签: python pdb

我正在使用Python 2.6.6,并使用pdb来调试我的Python程序,但我不清楚" next"之间的区别是什么? "直到"在pdb中,似乎它们都会继续执行,直到当前函数的下一行。

1 个答案:

答案 0 :(得分:3)

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

更有帮助,Doug Hellman gives an example in his Python Module Tutorial of the Week说明了差异:

  

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)

$ 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)
  

在运行之前,当前行是16,最后一行   环。直到跑完,执行在第17行,循环已经   耗尽。

eponymous gdb command分享until的目的:

  

,直到

     

继续运行,直到到达当前堆栈帧中当前行的源行。此命令用于避免单个   不止一次地踩过一个循环。这就像下一个命令,   除了直到遇到跳跃,它会自动继续   执行直到程序计数器大于地址   跳。这意味着当您在单个循环后到达循环结束时   踩到它,直到让你的程序继续执行,直到   它退出循环。相反,循环结束时的下一个命令   简单地回到循环的开头,这迫使你   逐步完成下一次迭代。

相关问题