Creating Modules/Namespaces from C/C++ for better class/function encapsulation/cleanup

kanedatakeshi at gmx.de kanedatakeshi at gmx.de
Fri Apr 1 14:41:36 EST 2005


Hi folks!

The problem is the following:

I use Python 2.1 embedded in a C++ game engine. Some of the engines
complexer objects (AI, game logic, etc.) use python classes that act as
an extension of those "parent" C++-classes.

The python classes are currently loaded on demand by their "parent"
C++-instances, say if an enemy gets activated and starts running. I do
that by:
1.) reading the needed python script (dedicated to that enemy class)
into a string and then
2) calling 'Py_CompileString()' followed by
3) 'PyEval_EvalCode()'

Then I have access to all class(es) defined in the python script
implementing all the needed methods for my enemy.
It looks a bit tedious than just calling e.g. 'PyRun_SimpleFile()', but
I thought, that PyEval_EvalCode() could provide me with means to force
a local and global namespace on the interpreted code.

Problem and Question:
To get rid of ALL code/class definitions/etc. defined when I loaded and
interpreted the enemy's python script, I want the python interpreter to
force a namespace or module context on everything associated with my
enemy, so I can be sure I cleaned up everything that is related to the
previous loading operation. I tried to use a new module (e.g. calling
'PyImport_AddModule') as my local namespace, but the script couldn't
find any built-in functions or even other classes defined in the very
same script file anymore when I did that.

Any idea how to do that properly?

Cheers!

PiLLe




More information about the Python-list mailing list