Line by line execution of python code

Justin Powell valkenar at yahoo.com
Thu Jul 13 14:28:07 EDT 2006


Hi, I'm looking for suggestions on how to accomplish something in python.  If
this is the wrong list for such things, I appologize and please disregard the
rest.

My application needs to allow users to create scripts which will be executed
in a statement-by-statement fashion.  Here's a little pseudo-code:

def someUserProgram():
	while 1:
		print "I am a user program"
		
def mainProgram():
	someProgram = someUserProgram
	while 1:
		print "Doing some stuff here"
		executeOneStatement(someProgram)

def executeOneStatement(program):
	# What goes in here?

I would expect the output to look like this:

Doing some stuff here 
Doing some stuff here 
I am a sub program 
Doing some stuff here 
Doing some stuff here 
I am a sub program
etc.

It's possible to use generators to accomplish this, but unfortunately a
state-ment by statement executing would require that every other statement is
a yield.  That would either force the users to put in a yield after every
statement, or require a mechanism that parses the source and inserts yield
statement before execution.  Neither of these are really satisfactory 

The other methods I've considered for doing this cleanly (subclassing the
debugger, or the interactive interpreter) seem to revolve around sys.settrace
and a callback trace function. I couldn't figure out a way to use this
mechanism to resume the main loop from the trace function, and then also
resume the user program the next time around, without the call stack just
spiraling out of control.

Anybody have a solution to this?  I've done a bit of searching, looking
through python docs and sources and so forth, and I'm pretty stumped at this
point.

Thanks.

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Python-list mailing list