[C++-sig] Re: location of initialization matters?

Martin Blais blais at iro.umontreal.ca
Tue Sep 23 20:05:04 CEST 2003

just a follow up on this:  I was able to recreate the scope object for 
my module by import "itself" from the project's init framework, which is 
executed after the Python initMODULE function is called, and since at 
that point the module has had its Py_InitModule() called, I assume that 
doing a PyImport_ImportModuleEx() after that won't have any other 
consequences.  Here is the relevant code::


       ... initialize buff here

          using namespace python;

          // Obtain the handle on the already imported module to create a
          // scope.
          PyObject* m = ::PyImport_ImportModuleEx( "libDLpyextModuleA",
                                                   0, 0, 0 );
          assert( m != 0 );
          scope module_scope( object( borrowed<PyObject>( m ) ) );

	 // Boost.Python declarations using initialized objects here.
          module_scope.attr( "buff" ) = buff;

This "seems to work" (i.e. it compiles and doesn't crash and I can 
obtain the correct initialized value for the initialized buff).

I think with this in mind, I could probably leave the initMODULE 
functions empty and always put my Boost.Python declarations in the 
project's performInitialize() methods.

Do you see any potential problems with this?  I'm sure some other 
projects must have had to do a similar trick.

I guess my question boils down to:  "can one safely declare new things 
for Python once the module has already been initialized?"  (i.e. by 
"declare new things" I mean insert Boost.Python declarations, for 
example, could I declare a new class within a function call? (i.e not 
the initMODULE function))

More information about the Cplusplus-sig mailing list