"Visually following", line by line, the execution of a program

Alex Martelli aleaxit at yahoo.com
Sun Oct 17 11:13:29 EDT 2004


Andr? Roberge <andre.roberge at ns.sympatico.ca> wrote:

> I want to "import and execute" a python program (input_test.py below)
> within another program (execute_test.py below) and "watch it" being
> executed.
> By watching it, I mean to display the file input_test.py in a window
> (GUI) and highlighting the line being executed.  

The functionality you require is known as 'tracing'.

See section 9.2, "How it works", in the Python Library Reference, for
all details.  With sys.settrace, you establish your chosen 'tracing
function', and it gets called with three arguments, frame, event, arg.
event is a string which can be 'call', 'line','return', 'exception'.
The 'line' event is the main one you care about: from the frame object
you can find out what file and line is about to be executed.  Be sure to
have the trace function return itself if it wants to keep tracing,
because for most events the trace function's return value is taken as
the new trace function to use (locally, i.e. within the function
currently being traced) for future tracing, None meaning stop tracing
(at this level of function call).


Alex



More information about the Python-list mailing list