Cedric Adjih adjih at
Sun Apr 30 18:45:48 CEST 2000

mdefreitas at wrote:

>> Do you want to run several python scripts (and several ui.commands
>> C code) concurently (how would you synchronize?) ?

> I do not want to run concurrently, I just want to run in one thread of
> execution, but recursively.


>> Note that if all you want is to have C code that calls Python
>> code that calls C code (through a module) that calls Python code
>> without threads, PyRun_XXX functions are re-entrant.

> Thanks! That's what I needed to know. I now modified (and simplified)
> my code as follows:

> void interp(char *script) {
>    static int nest_level = 0;
>    FILE *fd = fopen(script, "r");
>    if (nest_level == 0) {
>       Py_Initialize();
>       PyRun_SimpleString("import ui\n"); // import my ui
>    }
>    nest_level++;
>    PyRun_SimpleFile(fd, script);
>    nest_level--;
>    if (nest_level == 0) Py_Finalize();
> }

> The only problem is that the nested script that is run "remembers" the
> variable settings from it's caller. Is there a way to give each nested
> python script a fresh, clean environment? The nested script should not
> effect the environment of it's caller either. Is that what the global
> and local dictionaries for the API function PyRun_File are for? If so,
> is there any examples on their usage. The C/API doc isn't all that
> explicit.

  PyRun_File is actually implementing most of the "execfile(...)"
code (see
Python source code for execfile is also an example of use for 
  Passing empty dictionaries would be a way to have a "clean"
environment except if a script imports a Python module and fiddles
with its internals. Because modules will be shared among scripts.
But fiddling with internals is very bad style.

-- Cedric

More information about the Python-list mailing list