简体   繁体   中英

Python: how to control namespace after an unhandled exception?

Is there any way to control which namespace you'll end up in after an unhandled exception?

For example, in an IPython prompt, run myscript.py on a file with these contents:

def spam():
    ham = "ham"
    crash = 1/0
    eggs = "eggs"

if __name__ == '__main__':
    foo = "foo"
    spam()

it crashes out with the zero division, and returns to IPython prompt with foo now in the namespace, but no ham . For some post mortem inspection, I want to return to the interpreter in the scope of the function spam() (ie ham would be available, eggs and foo would not).

We can access those things with pdb.set_trace() and re-running the code, but it would be great if there was a shortcut to step back a frame.

You can enable the automatic calling of pdb in the ipython prompt by typing %pdb .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM