Question

If I want to run a script and have pdb catch any exceptions come up, I invoke it like so:

python -m pdb script.py

Or:

pdb script.py

The problem is that it stops at the debugging prompt right away:

> /home/coiax/Junkyard/script.py(1)<module>()
-> import sys
(Pdb) 

And I have to type c or continue to get it to go. Is there any way of getting it to just load and start the script without initially asking if I want to set any breakpoints or whatever?

I swear I've read the pdb module documentation, and tried making a .pdbrc file containing

c

but it doesn't start auto-magically.

Était-ce utile?

La solution

What you want can trivially be achieved using ipython:

ipython yourmodule.py --pdb

Whenever an exception is raised which is not caught properly (i.e. the program crashes), you instantly land in ipython's debugger at the point where the exception was raised. From there on you can move the stack up and down, inspect variables, etc. This is a huge time saver when developing with Python; I use it all the time.

Autres conseils

Since Python 3.2 you can use the following:

python3 -m pdb -c c script.py

It works exactly as you describe and want it to work in python 3.3-- with the 'c' character in a .pdbrc file.

The pdb module documentation says this was added in 3.2:

Changed in version 3.2: .pdbrc can now contain commands that continue debugging, such as continue or next. Previously, these commands had no effect.

that's probably not the way to do to it.

you should modify/extend the existing pdb.py. the code looks like this:

while True:
    try:
        pdb._runscript(mainpyfile)
        if pdb._user_requested_quit:
            break
        print "The program finished and will be restarted" 
    except:
        traceback.print_exc()
        print "Uncaught exception. Entering post mortem debugging"
        print "Running 'cont' or 'step' will restart the program"
        t = sys.exc_info()[2]
        pdb.interaction(None, t)
        print "Post mortem debugger finished. The "+mainpyfile+" will be restarted"

it looks like you could replace `pdb._runscript(mainpyfile)' with something like

runpy.run_module(sys.argv[0], run_name="__main__", alter_sys=True)

(from PEP 338).

not a complete solution, and i haven't tested it, but maybe it helps.

edit: but for a simpler solution, for a single script, your script probably ends with something like:

if __name__ == '__main__':
    call_main_function()

you just replace it it

if __name__ == '__main__':
    try:
        call_main_function()
    except:
        import pdb
        t = sys.exc_info()[2]
        pdb.interaction(None, t)

(last code stolen from pdb.py, that's exactly what it does when it gets an unhandled exception).

Are you certain that it's not actually running? From the docs

When invoked as a script, pdb will automatically enter post-mortem debugging if the program being debugged exits abnormally. After post-mortem debugging (or after normal exit of the program), pdb will restart the program. Automatic restarting preserves pdb’s state (such as breakpoints) and in most cases is more useful than quitting the debugger upon program’s exit.

emphasis mine.

When I ran my program trying to figure out why it did the same thing (displayed the very first line of code), I kept trying different things and then finally I noticed this line:

The program finished and will be restarted

If your program is, in fact, running without exception and then restarting, it will appear to start from the very beginning of your program.

pdb does not have a command that facilitates this for you. You can try using an IDE like PyCharm of PyDev. You can do what Corley Brigman suggested, or you can simple use an IDE, that makes life a lot easier.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top