Changing global variables in embedding Python

John Dunn jhndnn at
Thu Sep 26 19:46:14 EDT 2002


I have embedding Python in a C++ application. All is well, except I
cannot figure out how to modify globals via Python script. After I
load in my script files, I do this-

module = PyImport_AddModule("<embed>");
dict =  PyModule_GetDict(module); 
PyDict_SetItemString( dict, "__builtins__", PyEval_GetBuiltins());
PyObject* args = PyTuple_New(1);

... fill out arguments to function here....

PyObject* func = PyDict_GetItemString( dict, func_name );
static PyObject* test = PyString_FromString( "test string" );
PyDict_SetItemString( dict, "test", test );      
PyObject* result = check( PyEval_CallObject( func, args), 0 ); 

The Python function can access test fine, but it can't modify test. In
the following code, the first line works properly, but the second will
fail with the error 'local variable 'test' referenced before
assignment'. The message object is an argument to the function.

message.print( test )
test = "test2"

Any idea what I might be doing wrong?

Thanks for any help-

John Dunn
Peak Audio, a division of Cirrus Logic Inc.

More information about the Python-list mailing list