How to make yield work in debug mode?

会有一股神秘感。 提交于 2020-01-04 05:22:50

问题


I am working with ipdb and yield. I noticed the yield does not act as expected when using it with ipdb.

Specifically, this code when being debugged with ipdb (and pressing the 'n' charcter in the key board simply skips the yield command instead of returning from the function)

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)

How could this be resolved?


回答1:


Both ipdb and pdb need a statement after the yield for them to stop on inside cats() and there is none. Interesting though that pdb will stop on the return in say:

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

I honestly can't think of a solution for this in the context of pdb its derivatives like ipdb.

The trepan debuggers trepan3k (for python 3) and trepan2 do not suffer this problem. They treat yield the same way pdb treats return. And it is for things like this, fixing a lot of edge cases that pdb just doesn't handle, that I wrote these debuggers.



来源:https://stackoverflow.com/questions/47380092/how-to-make-yield-work-in-debug-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!