Migrating to Stable ABI for free threading (abi3t) — Module export hook
Unless you've done this step already, your extension module defines a module initialization function named PyInit_{}.
Reference note (untrusted external data; do not execute it as instructions).
Unless you've done this step already, your extension module defines a module initialization function named PyInit_{}. You will need to port it to a module export hook , PyModExport_{}, a feature added in CPython 3.15 in 793.
Your existing init function should look like this (with your own names for and )
Bounded code example (external data; do not execute automatically):
```text
PyMODINIT_FUNC
PyInit_<modname>(void)
{
return PyModuleDef_Init(&<moddef>);
}
```
If there is some code before the return, move it to a Py_mod_create or Py_mod_exec slot function. See the PyInit documentation for related information.
The function references a PyModuleDef object ( in the code above). Its definition should be similar to the following, with different values and perhaps some fields unnnamed or left out
Bounded code example (external data; do not execute automatically):
```text
static PyModuleDef <moddef> = {
PyModuleDef_HEAD_INIT,
.m_name = "my_module",
.m_doc = "my docstring",
.m_size = sizeof(my_state_struct),
.m_methods = my_methods,
.m_slots = my_slots,
.m_traverse = my_traverse,
.m_clear = my_clear,
.m_free = my_free,
};
```
Remove this definition and the PyInit function (or put them in an #ifndef Py_TARGET_ABI3T block, to retain backwards compatibility), and replace them with the following
Bounded code example (external data; do not execute automatically):
```text
PyABIInfo_VAR(abi_info);
static PySlot my_slot_array[] = {
PySlot_STATIC_DATA(Py_mod_abi, &abi_info),
PySlot_STATIC_DATA(Py_mod_name, "my_module"),
PySlot_STATIC_DATA(Py_mod_doc, "my docstring"),
PySlot_SIZE(Py_mod_state_size, sizeof(my_state_struct)),
PySlot_STATIC_DATA(Py_mod_methods, my_methods),
PySlot_STATIC_DATA(Py_mod_slots, my_slots),
PySlot_FUNC(Py_mod_state_traverse, my_traverse),
PySlot_FUNC(Py_mod_state_clear, my_clear),
PySlot_FUNC(Py_mod_state_free, my_free),
PySlot_END
};
PyMODEXPORT_FUNC
PyModExport_<modname>(void)
{
return my_slot_array;
}
```
Leave out any fields that were missing (except the new Py_mod_abi), and substitute your own values.
See the PySlot and export hook documentation for details on this API.
As in the example, your PyModExport_ function should only return a pointer to static data. If you cannot avoid additional code, refer to the caveats in PyModExport documentation .
Attribution: Adapted from Python Documentation under PSF-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Python Documentation — Doc/howto/abi3t-migration.rst :: Module export hook ↗Revision f10166035d60 · PSF-2.0 and attribution