Good general way to always call python debugger on exception

I want my debugger to run post_mortem() anytime an exception post_mortem() , without having to change the source I'm working on. I see a lot of examples that include packaging code in a try / except block, but I would like it to always run regardless of what I'm working on.

I worked on a python script shell, but this turned out to be ugly and pretty much unusable.

I use pudb , which is equivalent to the API for pdb, so the answer is pdb-specific. I am running the code from my editor (vim) and would like pm to come anytime an exception occurs.

+4
source share
2 answers

It took several months to do nothing, but I accidentally stumbled upon a solution. I am sure this is nothing new for the more experienced.

I have the following in my environment:

 export PYTHONUSERBASE=~/.python export PYTHONPATH=$PYTHONPATH:$PYTHONUSERBASE 

And I have the following file:

 ~/.python/lib/python2.7/site-packages/usercustomize.py 

With the following contents:

 import traceback import sys try: import pudb as debugger except ImportError: import pdb as debugger def drop_debugger(type, value, tb): traceback.print_exception(type, value, tb) debugger.pm() sys.excepthook = drop_debugger __builtins__['debugger'] = debugger __builtins__['st'] = debugger.set_trace 

Now, whether interactively or not, the debugger always jumps to the exception. It might be nice to come up with this.

It is important that you do not have no-global-site-packages.txt in site-packages . This will disable the default site.py module usercustomize (my virtualenv had no-global-site-packages.txt )

Just in case, this would help others, I stayed in a bit about changing __builtins__ . It’s very convenient for me to always be able to rely on some of the tools available.

The aroma of taste.

+5
source

A possible solution is to call pdb (I don't know about pudb , but I just assume that it works the same way), like a script:

 python -m pdb script.py 

Documentation citation:

When called as a script, pdb automatically enters post-mortem debugging if the debugged program crashes abnormally. After post-mortem debugging (or after the normal exit of the program), pdb will restart the program.

+1
source

Source: https://habr.com/ru/post/1479300/


All Articles