[Python-Dev] nested extension modules?

David Abrahams dave@boost-consulting.com
Mon, 19 Aug 2002 20:35:53 -0400


Hi,

Using the source (Luke), I was trying to figure out the best way to add a
nested submodule from within an extension module. I noticed that the module
initialization code will set the module name from the package context (if
set), altogether discarding any name passed explicitly:

[modsupport.c: Py_InitModule4()]

 ...
 if (_Py_PackageContext != NULL) {
  char *p = strrchr(_Py_PackageContext, '.');
  if (p != NULL && strcmp(name, p+1) == 0) {
   name = _Py_PackageContext;
   _Py_PackageContext = NULL;
  }
 }

This _Py_PackageContext is set up from within _PyImport_LoadDynamicModule
[importdl.c:]

 ...
 oldcontext = _Py_PackageContext;
 _Py_PackageContext = packagecontext;
 (*p)();
 _Py_PackageContext = oldcontext;

IIUC, this means that when an extension module is loaded as part of a
package, any submodules I create my calling Py_InitModule<whatever> will
come out with the same name.

Questions:

a. Have I got the analysis right?
b. Is there a more-sanctioned way around this other than touching
_Py_PackageContext (which seems to be intended to be private)

TIA,
Dave

-----------------------------------------------------------
           David Abrahams * Boost Consulting
dave@boost-consulting.com * http://www.boost-consulting.com