当已经处于 pdb 模式时,pdb 进入一个函数

pdb step into a function when already in pdb mode

在 pdb 模式下,我经常想单步执行一个函数。这是一个说明我可能会做什么的情况。给定代码:

def f(x):
    print('doing important stuff..')
    result = g(x) + 2
    return result

def g(x):
    print('some cool stuff going on here, as well')
    0 / 0  # oops! a bug
    return x + 5

现在,假设我在 print('doing important stuff...')result = g(x) + 2 之间设置了一个断点。所以现在,f(x) 看起来像这样:

def f(x):
    print('doing important stuff..')
    __import__('pdb').set_trace()  # or something similar..
    result = g(x) + 2
    return result

然后我用 x=5 调用函数 f(x),期望得到结果 12。调用时,我在 f 中第二行的交互式 pdb 会话中结束。点击 n 会给我错误(在本例中为 ZeroDivisionError)。现在,我想以交互方式进入 g(x) 函数以查看错误可能是什么。是否有可能在不“移动” g(x) 中的断点并重新 运行 一切的情况下做到这一点?我只是想在第一行输入函数 g,同时仍处于 pdb 模式。

我已经搜索了以前的 SO 问题和答案 + 查看了文档,但仍然没有找到任何解决这种特殊情况的方法。

您可能正在寻找 s 命令:它 s 进入下一个函数。

在调试模式下,您可以使用 h(帮助)查看所有可用的命令。另见 the docs