drop into the interpreter

cmkl cmkleffner at gmx.de
Wed Aug 18 09:36:53 CEST 2004


"Hoang Do" <hoang at jotsite.com> wrote in message news:<MoPSc.848$PG2.811334 at twister.tampabay.rr.com>...
> is there a facility to inspect the run-time of a python script?
> Essentially, it would execute a script to a set specific point and then drop
> into the interpreter.  Something like a "Stop" or "Break"?
> 
> Hoang Do
> hoang at jotsite.com

You can use my recipe from the Python Cookbook:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/285214
import the code object called prompt:

from prompt import prompt

and 'break' your code at any location with:

exec prompt

The control flow of your program breaks at this location and
pops up a interpreter which is operating the current scope
of your program. With CTRL-D you can continue with your program.

example: (prompttest.py)
========================

if __name__=='__main__': 
    from prompt import prompt

    def my_func():
        exec prompt       # exec prompt inside a function

    class prompt_test:
        def test_method(self):
            self.dummy = 'dummy'
            exec prompt   # exec prompt inside a method
    
    # 1: exec prompt inside a method
    prompt_test().test_method()
    
    # 2: exec prompt inside a function
    my_func()
    
    # 3: exec prompt inside the modules global scope
    exec prompt

example session:
================

python prompttest.py

prompt in test_method() at prompttest.py:10  -  continue with CTRL-D
>>> dir()                   # we are in a methods scope
['_prompt', 'self']
>>> print self              # and the instance is ...
<__main__.prompt_test instance at 0x008DFEB8>
>>> self.black = 'adder'            # add an attribute to the instance
>>> self.__class__.adder = 'black'  # add an attribute to the class
>>> dir(self)
['__doc__', '__module__', 'adder', 'black', 'dummy', 'test_method']
>>> for i in (1,2):         # test a multiline command
...    print i
...
1
2
>>> dir()                   # btw: the '_prompt' object will be
deleted later on
['_prompt', 'i', 'self']
>>> ^D
--- continue ----

prompt in my_func() at prompttest.py:5  -  continue with CTRL-D
>>> dir()                   # nothing interesting in this fuctions
scope
['_prompt']
>>> globals().keys()        # a little bit more in globals()
['prompt', '__builtins__', '__file__', 'prompt_test', 'my_func',
'__name__', '__doc__']
>>> monty = 'python'        # this would vanish at the end of the
function
>>> ^D
--- continue ----

prompt in __main__ at prompttest.py:19  -  continue with CTRL-D
>>> dir()                   # no python anymore - or was it monty?
['__builtins__', '__doc__', '__file__', '__name__', '_prompt',
'my_func', 'prompt', 'prompt_test']
>>> dir(prompt_test)        # our adder attribute is still there
['__doc__', '__module__', 'adder', 'test_method']
>>> print prompt_test().adder    # and adder is still black ...
black
>>> ^D
--- continue ----



More information about the Python-list mailing list