drop into the interpreter

Discussion in 'Python' started by Hoang Do, Aug 12, 2004.

  1. Hoang Do

    Hoang Do Guest

    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 Do, Aug 12, 2004
    #1
    1. Advertising

  2. Hoang Do wrote:
    > 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"?


    Not exactly that, but code.InteractiveConsole comes *very* close.

    Regards,
    Martin
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Aug 12, 2004
    #2
    1. Advertising

  3. Hoang Do wrote:

    > 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"?


    ipython has a mode designed specifically for this:

    http://ipython.scipy.org/doc/manual/node9.html

    Best,

    f
    Fernando Perez, Aug 12, 2004
    #3
  4. Hoang Do

    Ray Buvel Guest

    Hoang Do wrote:
    > 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"?


    This can be done fairly easily by creating a module (lets call it
    interactive) with the following code in it.
    -----------
    import sys,os

    def debug_exception(type, value, traceback):
    # Restore redirected standard I/O
    sys.stdin = sys.__stdin__
    sys.stdout = sys.__stdout__
    sys.stderr = sys.__stderr__

    # Kick the interpreter into interactive mode and call the original
    # exception handler.
    os.environ['PYTHONINSPECT'] = '1'
    sys.__excepthook__(type, value, traceback)

    sys.excepthook = debug_exception
    -----------

    Now if you import this module and raise an unhandled exception, you will
    be in interactive mode. In other words, I think the following script
    does what you are asking for.

    -----------
    import interactive

    raise RuntimeError('Interactive Mode')
    -----------

    This also has the advantage that if there are no unhandled exceptions in
    your script, the script runs and terminates normally.

    Enjoy,
    Ray Buvel
    Ray Buvel, Aug 13, 2004
    #4
  5. Hoang Do

    Dan Thompson Guest

    Thank you much Ray... this is exactly what I needed. I use the shell almost
    exclusively and debugging through it helps with this little snippet.

    Hoang Do


    "Ray Buvel" <> wrote in message
    news:eh2Tc.92478$-kc.rr.com...
    > Hoang Do wrote:
    > > 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"?

    >
    > This can be done fairly easily by creating a module (lets call it
    > interactive) with the following code in it.
    > -----------
    > import sys,os
    >
    > def debug_exception(type, value, traceback):
    > # Restore redirected standard I/O
    > sys.stdin = sys.__stdin__
    > sys.stdout = sys.__stdout__
    > sys.stderr = sys.__stderr__
    >
    > # Kick the interpreter into interactive mode and call the original
    > # exception handler.
    > os.environ['PYTHONINSPECT'] = '1'
    > sys.__excepthook__(type, value, traceback)
    >
    > sys.excepthook = debug_exception
    > -----------
    >
    > Now if you import this module and raise an unhandled exception, you will
    > be in interactive mode. In other words, I think the following script
    > does what you are asking for.
    >
    > -----------
    > import interactive
    >
    > raise RuntimeError('Interactive Mode')
    > -----------
    >
    > This also has the advantage that if there are no unhandled exceptions in
    > your script, the script runs and terminates normally.
    >
    > Enjoy,
    > Ray Buvel
    Dan Thompson, Aug 13, 2004
    #5
  6. Hoang Do

    Miki Tebeka Guest

    Hello Hoang,

    > 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"?

    "pdb" module set_trace does what you want (and you're in the deubgger
    environment...)
    ---
    import pdb

    print 1
    pdb.set_trace()
    print 2
    ---

    HTH.
    --
    ------------------------------------------------------------------------
    Miki Tebeka <>
    http://tebeka.spymac.net
    The only difference between children and adults is the price of the toys
    Miki Tebeka, Aug 15, 2004
    #6
  7. Hoang Do

    cmkl Guest

    "Hoang Do" <> wrote in message news:<MoPSc.848$>...
    > 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
    >


    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 ----
    cmkl, Aug 18, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SirPoonga
    Replies:
    2
    Views:
    730
    Ben Strackany
    Jan 7, 2005
  2. Oliver Klein
    Replies:
    0
    Views:
    667
    Oliver Klein
    Aug 24, 2003
  3. weiwei
    Replies:
    0
    Views:
    990
    weiwei
    Jan 5, 2007
  4. Replies:
    3
    Views:
    737
    Ziga Seilnacht
    Jan 3, 2007
  5. msimmons
    Replies:
    0
    Views:
    441
    msimmons
    Jul 16, 2009
Loading...

Share This Page