[C++-sig] Handling namespaces and dicts in embedded python

Lutz Paelike lutz_p at gmx.net
Thu Oct 30 15:25:18 CET 2003


Hi,

i have the same problem as described in this posting:

http://aspn.activestate.com/ASPN/Mail/Message/python-list/766049

I load a script like this :
#############################
global a

class MyClass:
	def run(self):
		pass
def init(arg):
	global a
	a = MyClass()
def compute(arg):
	a.run()
def destroy(arg):
	del a

init(None)
print a
# prints: <__main__.MyClass instance at 0x008FA800>
#################################

compile it via Py_CompileString and store the codeobject in individual objects.

After that i want to call the compute  function on a regular basis.

I have local dicts associated with the individual objects and a shared global dict

I managed to call the init function inside the compiled code object, but i get this exception:

exceptions.NameError
global name 'MyClass' is not defined
Traceback (most recent call last):
   File "F:/simple_test.py", line 14, in ?
     init(None)
   File "F:/simple_test.py", line 8, in init
     a = MyClass()
NameError: global name 'MyClass' is not defined

Somehow the definitions of my functions and of the class are not stored neither in the local dict nor in the shared 
global dict. However if i add this line to my python code:

globals()["MyClass"] = MyClass

It works, but my program crashes in PyFrame_New then.

Can anybody explain how the namespace is handled in this situation and how to resolve this problem ?

btw i use python 2.3.2 , a recent boost cvs checkout and msvc 6 sp5

If run the script with the normal interpreter it works like expected.

Thanks for your help,

Lutz





More information about the Cplusplus-sig mailing list