Cathing sys.exit() in debugger

吃可爱长大的小学妹 提交于 2019-12-11 03:03:30

问题


My programme ends unexpectedly. When I run it through pdb, it ends with:

The program exited via sys.exit(). Exit status:                                                                         

but then there is no record of the stack at the instance it exited. I have no idea why it happens. Short of step and nexting through everything, is there a way to instruct pdb to enter the debugger when this is attempted, instead of honouring the sys.exit()?


回答1:


A simple-ish solution would be to monkey-patch sys.exit() before running:

$ python -m pdb my_script.py

(Pdb) def no_exit(code): raise RuntimeError('halt')
(Pdb) import sys
(Pdb) sys.exit = no_exit
(Pdb) cont


来源:https://stackoverflow.com/questions/42835017/cathing-sys-exit-in-debugger

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