drop into the interpreter

H

Hoang Do

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
(e-mail address removed)
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Hoang said:
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
 
R

Ray Buvel

Hoang said:
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
 
D

Dan Thompson

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
(e-mail address removed)

Ray Buvel said:
Hoang said:
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
 
M

Miki Tebeka

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
 
C

cmkl

Hoang Do said:
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
(e-mail address removed)

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 ...
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
2deleted later on
['_prompt', 'i', 'self']--- continue ----

prompt in my_func() at prompttest.py:5 - continue with CTRL-D
scope
['_prompt']
['prompt', '__builtins__', '__file__', 'prompt_test', 'my_func',
'__name__', '__doc__']--- continue ----

prompt in __main__ at prompttest.py:19 - continue with CTRL-D['__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 ----
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top