using breakpoints in a normal interactive session

D

dan.gass

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? And possibly resume? For example:
.... x = 1
.... magic_breakpoint()
.... y = 1
.... print "got here"
....Traceback (most recent call last):
File "<stdin>", line 1, in ?
Traceback (most recent call last):
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'x' is not defined
 
R

R. Bernstein

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:
 
C

Carl Friedrich Bolz

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? And possibly resume? For example:



... x = 1
... magic_breakpoint()
... y = 1
... print "got here"
...


Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in a


Traceback (most recent call last):


got here


Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'x' is not defined

you can use the standard-library code module for that: instead of
magic_breakpoint() just call code.interact(local=locals()). The
magic_resume() would be a regular Ctrl-D (or Ctrl-Z Enter under
windows). You can also package this nicely into a convenient function,
see for example the third example on the following page:

http://effbot.org/librarybook/code.htm

Cheers,

Carl Friedrich Bolz
 
R

R. Bernstein

In revising pydb the code and documentation for the routine originally
described, I learn that the pdb equivalent (sort of) is called
set_trace().

However set_trace() will terminate the program when you quit the
debugger, so I've retained this routine and made a couple of
corrections -- in particular to support a restart and make "show args"
work. The changes are in pydb's CVS. Lacking a better name, the
routine is called "debugger".

There is one other difference between set_trace() and debugger(). In
set_trace you stop at the statement following set_trace(), With
debugger() the call trace shows you in debugger and you may need to
switch to the next most-recent call frame to get info about the
program being debugged.

A downside of the debugger() approach is that debug session
information can't be saved between calls: each call is a new instance
of the debugger and when it is left via "quit" the instance is
destroyed. (In the case of pydb.set_trace() the issue never comes up
because the program is terminated on exit.)
 
D

dan.gass

Carl -- Perfect! That is exactly what I want. I hoped it would be
that easy. Thanks for taking the time to post the solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,107
Latest member
AmeliaAmad

Latest Threads

Top