PyRun_String and dictionaries
Weiss, Mike
Mike.Weiss at PRICESystems.com
Wed Aug 28 15:59:49 EDT 2002
Help!
I've embedded python into my C++ program and I use PyRun_String() to invoke python code. I often do this multiple times per the life of my application.
For the dictionaries parameters of PyRun_String() I pass "pyMainDict", see code below:
pyMainMod = PyImport_AddModule("__main__");
pyMainDict = PyModule_GetDict(pyMainMod);
My problem is that on multiple calls to PyRun_String variables declared in scripts used on earlier calls to PyRun_String() are still hanging around when newer scripts are executed. I assume the problem is that I'm reusing the same dictionaries for globals and locals, the one from __main__.
Is there a run each script in a clean environment, one where stuff from the last run isn't accessible?
I've tries creating new dictionaries and using them but if I use anything other then __main__'s dictionary for "globals" I get strange import errors (my scripts always import other modules).
I've had some problems with using a new dictionary just for "locals". Mainly when my scripts def" and call local functions (problems with scope).
I've tried to Py_Init before and Py_Finialize after each PyRun_String() call... but that seems to crash.
What's the right way to do this?
More information about the Python-list
mailing list