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

Stefan Behnel stefan_ml at behnel.de
Fri Apr 17 08:51:34 CEST 2015

Petr Viktorin schrieb am 16.04.2015 um 13:05:
> A wart I added is "singleton modules", necessary for
> "PyState_FindModule"-like functionality. I wouldn't mind not including
> this, but it would mean the new API can't replace all use cases of the
> old PyInit_.
> Singleton Modules
> -----------------
> Modules defined by PyModuleDef may be registered with PyState_AddModule,
> and later retrieved with PyState_FindModule.
> Under the new API, there is no one-to-one mapping between PyModuleSpec
> and the module created from it.
> In particular, multiple modules may be loaded from the same description.

Is that because a single shared library (which is what the module spec
refers to, right?) can contain multiple modules? Or are you referring to
something else here?

> This means that there is no "global" instance of a module object.
> Any C-level callbacks that need access to the module state need to be passed
> a reference to the module object, either directly or indirectly.
> However, there are some modules that really need to be only loaded once:
> typically ones that wrap a C library with global state.
> These modules should set the PyModule_EXPORT_SINGLETON flag
> in PyModuleDesc.flags. When this flag is set, loading an additional
> copy of the module after it has been loaded once will return the previously
> loaded object.
> This will be done on a low level, using _PyImport_FixupExtensionObject.
> Additionally, the module will be automatically registered using
> PyState_AddSingletonModule (see below) after execution slots are processed.
> Singleton modules can be retrieved, registered or unregistered with
> the interpreter state using three new functions, which parallel their
> PyModuleDef counterparts, PyState_FindModule, PyState_AddModule,
> and PyState_RemoveModule::
>     PyObject* PyState_FindSingletonModule(PyModuleDesc *desc)
>     int PyState_AddSingletonModule(PyObject *module, PyModuleDesc *desc)
>     int PyState_RemoveSingletonModule(PyModuleDesc *desc)
> .. note::
>     If PyModuleDef is used instead of PyModuleDesc, the flag would be specified
>     as a slot with NULL value, i.e. ``{Py_mod_flag_singleton, NULL}``.
>     In this case, PyState_FindModule, PyState_AddModule and
>     PyState_RemoveModule can be used instead of the new functions.
> .. note::
>     Another possibility is to use PyModuleDef_Base in PyModuleDesc, and
>     have PyState_FindModule and friends work with either of the two structures.

Yes, this is totally a wart. However, I'm not sure I understand the actual
use case from what is written above. Could you clarify why this whole
special case is needed?


More information about the Import-SIG mailing list