[Import-SIG] PEP 489: Redesigning extension module loading; version 4

Nick Coghlan ncoghlan at gmail.com
Thu May 14 18:45:54 CEST 2015

On 14 May 2015 at 22:38, Petr Viktorin <encukou at gmail.com> wrote:
> I think the "initialized" vs. "exported" distinction is an
> implementation detail of the module, and this would expose it too
> much.
> According to its README, freeze "[parses] the program (and all its
> modules) and scans the generated byte code for IMPORT instructions". I
> think py2exe does something similar. The end users of such tools would
> need to designate which modules use init vs. export.
> Allowing PyInit to optionally return PyModuleDef is a bit of a hack,
> but it keeps the details isolated between the module and the import
> machinery.
> PyModuleDef is a PyObject, so the PyInit signature matches. Just the
> PyInit name is a bit misleading :(

Agreed it makes the name of PyInit_* a bit misleading, but also agreed
that it sounds like a good trick for making this work in a way that
can handle _PyImport_inittab appropriately.

In terms of documenting it in a way that lets the hook name still make
sense, perhaps we can refer to returning PyModuleDef as "multi-phase
initialisation"? That is:

- initialise the module definition
- create the module object
- execute the module body

If you *don't* return a module definition, then the import system will
assume single phase initialisation.

> I think I have a favorite direction now. (Sorry for asking for
> directions and then wanting to ignore them! The discussion is
> helpful.)

I find that seeing a suggestion I don't like often sparks new ideas as
I attempt to figure out why I don't like it :)

> Somewhat related: any thoughts on the legacy init example code [0]?
> You asked for an example like this; is it what you had in mind? If you
> compile this with a PEP-489 Python with the stable API, the .so can be
> used with older Pythons as well.
> I now think it's a bit silly: it would be enough to use #ifdef: define
> either PyModuleExport or PyInit, depending on the Python version.
> This won't do if you're targetting the stable API, but in that case
> you can't use any of the new PEP 489 features anyway, so it's enough
> to only define PyInit.
> Or is there something I missed?

I think the idea above makes it mandatory to use "#ifdef" to request
multi-phase initialisation on 3.5+ and single-phase initialisation on
earlier versions. An example of the relevant incantations might still
be useful though.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Import-SIG mailing list