如何让 yield 在调试模式下工作?

How to make yield work in debug mode?

我正在使用 ipdb 和 yield。我注意到在将它与 ipdb 一起使用时,产量并不像预期的那样。

具体来说,这段代码在使用 ipdb 进行调试时(并按下 'n' 键盘中的字符只是跳过 yield 命令而不是从函数返回)

def cats():
    print(-1)
    yield
    for i in range(4):
        print(i)
        yield

import ipdb
ipdb.set_trace()
x = cats()
next(x)
next(x)
next(x)

如何解决?

ipdb 和 pdb 都需要一个语句 after the yield 让他们在 cats() 里面停下来是 none。有趣的是,pdb 将在 return 处停止,例如:

def cats2():
        if len(__file__) > 5:
            import pdb; pdb.set_trace()
cats2()

老实说,在 pdb 及其衍生产品(如 ipdb)的背景下,我想不出解决方案。

trepan 调试器 trepan3k (for python 3) and trepan2 不会遇到这个问题。他们对待 yield 的方式与 pdb 对待 return 的方式相同。正是为了这样的事情,修复了 pdb 无法处理的许多边缘情况,我编写了这些调试器。