[Python-Dev] nested extension modules?

Guido van Rossum guido@python.org
Tue, 20 Aug 2002 13:06:24 -0400

> > It also nulls out the package context.
> Oops! I missed that part. Maybe that makes my problem imaginary, except
> that you go on to say...
> > The checkin comment I made back in 1997 explains this:
> >
> >     Fix importing of shared libraries from inside packages.
> >     This is a bit of a hack: when the shared library is loaded, the
> >     module name is "package.module", but the module calls
> >     Py_InitModule*() with just "module" for the name.  The shared
> >     library loader squirrels away the true name of the module in
> >     _Py_PackageContext, and Py_InitModule*() will substitute this (if
> >     the name actually matches).
> >
> > > b. Is there a more-sanctioned way around this other than touching
> > > _Py_PackageContext (which seems to be intended to be private)
> >
> > I think using _Py_PackageContext is your only hope.  If you contribute
> > some docs for it we'll gladly add them to the API docs.
> Hmm, my only hope for what? What I was worried about was that if I tried to
> create a nested sub-extension module from within my extension module by
> calling Py_InitModuleXXX() directly, its name would be forced to be the
> same as that of the outer extension module. Since you pointed out that
> _Py_PackageContext was being nulled out, I don't think that's much of an
> issue. What issues /do/ I need to be aware of when doing this?

I guess I misunderstood what you were trying to accomplish; I thought
you were asking if there was a more accepted way of doing this besides
setting _Py_PackageContext?

I don't understand why the nulling out of _Py_PackageContext makes a
difference for what you were trying to do -- unless the last component
of your submodule's name is the same as its parent's (e.g. you're
creating a submodule X.X inside a module X), the strcmp() could never
succeed.  Also, if the name passed to Py_InitModuleXXX() contains a
dot, the strcmp() can never succeed (since it's applied to the last
component of the package context).

--Guido van Rossum (home page: http://www.python.org/~guido/)