sys.settrace() and stopping the Python interpreter

Peter Hansen peter at engcorp.com
Mon Jun 2 07:54:49 EDT 2003


Francis Joanis wrote:
> 
> I am relatively new to Python and I am looking forward to embed it
> under a C++ application (gcc/linux) to add scripting capability.
> 
> I've seen various posts about being able or not to stop the execution
> of a running Python script based on whatever conditions.
> 
> From what I've seen there is no built-in ways of doing this directly
> as a Python feature since it was not designed/implemented.
> 
> On the other hand, I've experimented with the sys.settrace function
> (in order to do some condition checking then to raise an exception to
> stop the interpreter) but from what I've seen, setting up a trace
> function won't take care of Python's built in functions (like
> 'print').
> 
> I'd like to know if there is a way to set a trace function to process
> every instructions?

settrace() *does* execute for every instruction - every Python 
bytecode instruction.  The problem is that print is *not* a
function (even if you unnecessarily use parentheses with it)
but rather an actual bytecode instruction.  As far as I know,
there is no way for you to break print down into any component
instructions to trace, because there are none.

-Peter




More information about the Python-list mailing list