1.2.8 Running Arbitrary Python Commands (debug, !, ipython, python)

You can run arbitrary commands that change state. However there are some caveats. An assignment to a local variable doesn't have an effect, although changes to instance variables and globals do. This seems to be an artifact of how the interpreter is written to speed up name look-up for access to local variables. As a result, the locals dictionary seen may be created on the fly. (I could very well have the reason wrong, but net effect is the same--modifying a local variable doesn't still does not change the value after the command is finished.)

(Pydb) list
 23  	    
 24  	def gcd(a,b):
 25  	    """ GCD. We assume positive numbers"""
 26  	
 27  	    # Make: a <= b
 28  ->	    if a > b:
 29  	       (a, b) = (b, a)
 30  	       
 31  	    if a <= 0:
 32  	        return None
(Pydb) p a
3
(Pydb) a=5; print a
5
(Pydb) p a
3

Another thing to keep in mind is the scope of setting new local variables. They persist as long as the debugger stays inside the same scope. Furthermore you can change these. Continuing from above:

(Pydb) d = 5
(Pydb) p d
5
(Pydb) n
31     if a <= 0:
(Pydb) p d
5
(Pydb) d = 6 
(Pydb) p d
6
finish
--Return from level 4 => 1 (<type 'int'>)
35     return gcd(b-a, a)
(Pydb) p d
6
(Pydb) n
34         return a
(Pydb) p d
*** NameError: d

[!]statement

Execute the (one-line) statement in the context of the current stack frame. The exclamation point can be omitted unless the first word of the statement resembles a debugger command. To set a global variable, you can prefix the assignment command with a "global" command on the same line, e.g.:

(Pydb) global list_options; list_options = ['-l']
(Pydb)

debug statement

Enter a recursive debugger that steps through the code argument (which is an arbitrary expression or statement to be executed in the current environment). The prompt is changed to indicate nested behavior. See 1.2.1

ipython options...

Run ipython as a subshell. You need to have ipython (IPython) installed for this command.

options are the options passed to ipython. If not are given the following options are passed:

  -noconfirm_exit -prompt_in1 'Pydb In [\#]: '

python

Run python as a subshell.

See About this document... for information on suggesting changes.