"assert" annoyance

Dave Baum Dave.Baum at motorola.com
Fri Jun 22 12:41:21 EDT 2007


In article <7xmyys368j.fsf_-_ at ruckus.brouhaha.com>,
 Paul Rubin <http://phr.cx@NOSPAM.invalid> wrote:

> 
> What I really want is for any assertion failure, anywhere in the
> program, to trap to the debugger WITHOUT blowing out of the scope
> where the failure happened, so I can examine the local frame.  That
> just seems natural, but I don't see an obvious way to do it.  Am I
> missing something?  I guess I could replace all the assertions with
> function calls that launch pdb, but why bother having an assert
> statement?

I tend to run my programs with a -i option:

python -i foo.py

Then if an exception occurs, I can do:

import pdb
pdm.pm()

and have a debug session at the point where the exception was raised.  
The "-i" shouldn't interfere with sys.argv since it is before the python 
file.

Another option would be for your program to do something like:

    try:
        # put your code here
    except:
        import sys
        import pdb
        pdb.post_mortem(sys.exc_info()[2])


If an exception is raised, the debugger will be started using the 
appropriate traceback.  (You don't need to import sys and pdb in the 
except block if they have already been imported.)


Dave



More information about the Python-list mailing list