using breakpoints in a normal interactive session

R. Bernstein rocky at panix.com
Wed Feb 22 18:03:30 EST 2006


dan.gass at gmail.com writes:

> Is there a way to temporarily halt execution of a script (without using
> a debugger) and have it put you in an interactive session where you
> have access to the locals?  

Here's what I was able to do using the Extended Python debugger.
http://bashdb.sourceforge.net/pydb/. I'm sure there's a similar (if
not even simpler) way to do this in the stock debugger; but I'll let
others suggest that ;-)

First add this routine:

def call_debugger():
    from pydbdisp import Display, DisplayNode
    import pydb, inspect, sys
    try:
        raise Exception
    except:
        frame=inspect.currentframe()
        p = pydb.Pdb()
        p.reset()
        p.display = Display()
        p._program_sys_argv = list(sys.argv)
        p.interaction(frame, sys.exc_traceback)

And then call it from your program as you indicated below for
"magic_breakpoint()". For "magic_resume()" just issue "quit"
"continue" or give a termanal EOF. 

That the above routine is so long suggests some initialization
probably should be moved around. And in a future release (if there is
one), I'll consider adding something like the above routine.


And possibly resume?  For example:
> 
> >>> def a():
> ...   x = 1
> ...   magic_breakpoint()
> ...   y = 1
> ...   print "got here"
> ...
> >>> a()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "<stdin>", line 3, in a
>   File "<stdin>", line 2, in magic_breakpoint
> >>> x
> 1
> >>> y
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> NameError: name 'y' is not defined
> >>> magic_resume()
> got here
> >>> x
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> NameError: name 'x' is not defined



More information about the Python-list mailing list