473,466 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

drop into the interpreter

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
ho***@jotsite.com
Jul 18 '05 #1
6 2415
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
Jul 18 '05 #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"?


ipython has a mode designed specifically for this:

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

Best,

f
Jul 18 '05 #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"?


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
Jul 18 '05 #4
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
ho***@jotsite.com

"Ray Buvel" <rl*****@gmail.com> wrote in message
news:eh*******************@twister.rdc-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

Jul 18 '05 #5
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 <mi*********@zoran.com>
http://tebeka.spymac.net
The only difference between children and adults is the price of the toys
Jul 18 '05 #6
"Hoang Do" <ho***@jotsite.com> wrote in message news:<Mo******************@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
ho***@jotsite.com


You can use my recipe from the Python Cookbook:
http://aspn.activestate.com/ASPN/Coo.../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 ----
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Anon | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all I am a beginner teaching myself python, and I am enjoying it immensely :) As a language it is great, I real treat to study, I actually...
1
by: Donnie Leen | last post by:
I wrote a program embbed boost.python, each thread running a sub-interpreter, i made a module implement by boost.python, and i wish this module imported in each sub-interpreter, i find that the...
12
by: Rex Eastbourne | last post by:
Hi, I'm interested in running a Python interpreter in Emacs. I have Python extensions for Emacs, and my python menu lists "C-c !" as the command to run the interpreter. Yet when I run it I get...
4
by: Ian Giblin | last post by:
I am an experienced C programmer, learning C++ by writinging a mathematical toolkit in the framework of a script interpreter. I am posting here to ask for advice (or references) on the object...
12
by: ozbear | last post by:
If one were writing a C interpreter, is there anything in the standard standard that requires the sizeof operator to yield the same value for two different variables of the same type? Let's...
3
by: bruce | last post by:
Hi... Never used python, but I have a question regarding Drop Down Menus. Does Python allow me to create a website, that will permit the user to create Drop Down menus that can be initiated with...
6
by: gr | last post by:
hi.. I must implement an interpreter in C programming language that will get the source code of a program in text file format and execute it. but i don't know C language enough to write this...
3
by: Robin Becker | last post by:
As part of some django usage I need to get some ReportLab C extensions into a state where they can be safely used with mod_python. Unfortunately we have C code that seems incompatible with...
2
by: Evil Son | last post by:
Hello group, If I switched from mod_php to fast-cgi, would I need to make any changes to my php source? Also, will something like APC still be useful? Will my database connections suddenly...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.