dropping into the debugger on an exception

Chris Liechti cliechti at gmx.net
Thu Jun 10 00:47:42 CEST 2004


Jon Perez <jbperez808 at yahoo.com> wrote in news:2iovgrFq6taqU1 at uni-
berlin.de:

> Thomas Heller wrote:
> 
>>>Just to be clear: you don't want this to happen all the time,
>>>you want it to happen only with a particular script, yet you
>>>don't want to modify that script at all?
>> 
>> I also don't understand *why* he wants to have it that way.
> 
> Just in case you missed the answer I gave earlier...
> 
> I don't even want this behaviour all the time with the same
> script because end-users will be confused if they get dropped
> into pdb.  Believe me, you don't want to explain what pdb is
> to people who have a hard time even navigating via the
> command prompt!
>
> The best situation would be to be able to invoke this behaviour
> only when I want it (i.e. during coding/debugging) and not have
> to change anything (including sitecustomize.py) when I don't
> want it around (the same machine might be used by both the
> developer and the end-user).
> 

i usualy use something like that:
if __name__ == '__main__':
    try:
        from optparse import OptionParser
        
        parser = OptionParser()
        #...
        parser.add_option("-d", "--debug",
                          action="store_true", dest="debug", default=False,
                          help="enable debug outputs")
        
        (options, args) = parser.parse_args()
                
        logger.setLevel(logging.WARN)
        if options.verbose:
            logger.setLevel(logging.INFO)
        
        if options.debug:
            logger.setLevel(logging.DEBUG)

        main()
    except SystemExit:
        raise
    except Exception, msg:
        if debug:
            #raise    	#i usualy use that
    	       pdb.pm()    	#that's what you want
        else:
            sys.stderr.write("%s: %s\n" % (msg.__class__.__name__, msg))
            sys.exit(1)


that way, adding a -d on the command line will enable more log messages and 
full tracebacks (or pdb). you could of also use a separate option for pdb 
or only enable it if multiple -ddd are given (debug level increased)

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list