[C++-sig] once again about global dictionary replacement

Gennadiy Rozental rogeeff at gmail.com
Wed Sep 24 07:02:30 CEST 2008


Hi,

This is an example that illustrate problem similar to what I have.

Let's say I have c++ function 

bp::object s_save;
void foo( bp::object const& obj )
{
   s_save = obj;
}

exported with the same name in python.

Now let say in one place of my program I execute following string (this is an
input to my program for example)

"
import some_module_abc
def boo()
   .... do something here and use some_module_abc

foo( boo )
"

when I execute this string I use command like 
exec( code, my_module_dictionary )

I specify my_module_dictionary as local context because I want all the symbols
defined in string (functions and classes) to become members of my module.

Some time later I execute stored function like this

s_save();

Now here is a punchline: python complains it does not know about some_module_abc

The reason is that import occurred in local context of first exec call,
resulting in all symbols to be imported into my module dictionary, not global
dictionary. At execution point global dictionary is the same (not sure now, but
it seems bound to the python object). Local dictionary is temporary containing
function arguments (if any). Both know nothing about some_module_abc.

It would help if I can as the point of invocation do one of these
a) replace the bound global dictionary
b) update the bound global dictionary
c) update local dictionary that is going to be used

Can anyone suggest the solution?

Thanks,

Gennadiy





More information about the Cplusplus-sig mailing list