using breakpoints in a normal interactive session

Carl Friedrich Bolz cfbolz at gmx.de
Thu Feb 23 03:56:18 EST 2006


dan.gass at gmail.com wrote:
> 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:
> 
> 
>>>>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
> 

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




More information about the Python-list mailing list