Search code examples
pythonpython-3.xyieldipdb

How to make yield work in debug mode?


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?


Solution

  • 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.