Creating a debugable function with exec

35 Views Asked by At

Is it possible to debug the code of a function that was defined via exec in a convenient way?

foo_source = """\
def foo():
    print("start")
    breakpoint()
    print("end")
"""

exec(foo_source)
foo()

The breakpoint in the code works, and I can step through the function, but I don't see the code listing.

$ python foo.py
start
> <string>(4)foo()
(Pdb) list
[EOF]
(Pdb) next
end
--Return--
> <string>(4)foo()->None
(Pdb)
0

There are 0 best solutions below