Controlling Python

M.-A. Lemburg mal at
Mon Aug 16 17:27:22 CEST 1999

nriedlin at wrote:
> Hi all,
> in my application (written in C++) I want to use Python as the
> scripting language. The application comunicates with the hardware and
> when a specific event occurres, a thread is started, in which a python
> interpreter is created to execute a certain script. The thread should
> have a finer control over the Python-Interpreter as is possible with
> PyRun_SimpleFile().
> (E.g. I want to be able to stop the thread independent of the
> interpreter) I would like to have some kind of "hook" after, say, every
> line of python-code.
> I thought something like (pseudocode):
> while(getline(f, s) {
>   PyRun_SimpleString(s);
>   my_hook(s, lineno++,...);
> }
> should work but it fails, whenever the python-code becomes a little
> more difficult. E.g. PyRunSimpleString() failes, whenever the simple
> string continues in the following line:
> if bla:
>     pass
> Is this the way to go? If yes: how? If no: why not and in which
> direction should I turn?

You can use a callback which you can register with the
sys.settrace() function. A less fine-grained hook is available
through sys.setprofile(). See the docs for more infos.

Marc-Andre Lemburg
Y2000:                                                   138 days left
Python Pages:                 

More information about the Python-list mailing list