Multi-phase initialization¶
An alternate way to specify extensions is to request "multi-phase initialization".
Extension modules created this way behave more like Python modules: the
initialization is split between the creation phase, when the module object
is created, and the execution phase, when it is populated.
The distinction is similar to the __new__() and __init__() methods
of classes.
Unlike modules created using single-phase initialization, these modules are not
singletons: if the sys.modules entry is removed and the module is re-imported,
a new module object is created, and the old module is subject to normal garbage
collection -- as with Python modules.
By default, multiple modules created from the same definition should be
independent: changes to one should not affect the others.
This means that all state should be specific to the module object (using e.g.
using PyModule_GetState()), or its contents (such as the module's
__dict__ or individual classes created with PyType_FromSpec()).
All modules created using multi-phase initialization are expected to support
sub-interpreters. Making sure multiple modules
are independent is typically enough to achieve this.
To request multi-phase initialization, the initialization function
(PyInit_modulename) returns a PyModuleDef instance with non-empty
m_slots. Before it is returned, the PyModuleDef
instance must be initialized with the following function:
PyObject* PyModuleDef_Init(PyModuleDef*def)¶
Return value: Borrowed reference.
Ensures a module definition is a properly initialized Python object that
correctly reports its type and reference count.
Returns def cast to PyObject*, or NULL if an error occurred.
3.5 新版功能.
The m_slots member of the module definition must point to an array of
PyModuleDef_Slot structures:
PyModuleDef_Slot¶
int slot¶
A slot ID, chosen from the available values explained below.
void* value¶
Value of the slot, whose meaning depends on the slot ID.
3.5 新版功能.
The m_slots array must be terminated by a slot with id 0.
The available slot types are:
Py_mod_create¶
Specifies a function that is called to create the module object itself.
The value pointer of this slot must point to a function of the signature:
The function receives a ModuleSpec
instance, as defined in PEP 451, and the module definition.
It should return a new module object, or set an error
and return NULL.
This function should be kept minimal. In particular, it should not
call arbitrary Python code, as trying to import the same module again may
result in an infinite loop.
Multiple Py_mod_create slots may not be specified in one module
definition.
If Py_mod_create is not specified, the import machinery will create
a normal module object using PyModule_New(). The name is taken from
spec, not the definition, to allow extension modules to dynamically adjust
to their place in the module hierarchy and be imported under different
names through symlinks, all while sharing a single module definition.
There is no requirement for the returned object to be an instance of
PyModule_Type. Any type can be used, as long as it supports
setting and getting import-related attributes.
However, only PyModule_Type instances may be returned if the
PyModuleDef has non-NULL m_traverse, m_clear,
m_free; non-zero m_size; or slots other than Py_mod_create.
Py_mod_exec¶
Specifies a function that is called to execute the module.
This is equivalent to executing the code of a Python module: typically,
this function adds classes and constants to the module.
The signature of the function is:
int exec_module(PyObject*module)¶
If multiple Py_mod_exec slots are specified, they are processed in the
order they appear in the m_slots array.
See PEP 489 for more details on multi-phase initialization.