LoLBench / cpython_15 /instruction.md
lolbench26's picture
Publish LoLBench Harbor tasks from artifact branch (part 2)
a4edccc verified
|
Raw History Blame Contribute Delete
42.7 kB

You are working in /workspace/cpython, a source tree checked out at the base commit for this task. Implement the requested behavior in the source tree, then run:

lolbench-submit

Do not stop after editing files, running tests, or describing the solution. The task is complete only when lolbench-submit has created /logs/artifacts/solution.patch. If that file does not exist, continue working and run lolbench-submit again.

That command writes your implementation diff to /logs/artifacts/solution.patch, which is the artifact the Harbor verifier will grade. Before running lolbench-submit, clean or revert any test files you created or modified; test files must not be included in the final solution.patch.

This environment has no outbound internet access — curl/wget, git fetch/clone, package installs, and web fetch/search will all fail. Implement the requirements using only the code already in the workspace and your own knowledge; do not attempt to fetch or search external resources.

Now please implement the following requirements in the source tree:


PEP 793: PyModExport: A new entry point for C extension modules

Abstract

In this PEP, we propose a new entry point for C extension modules, by which one can define a module using an array of PyModuleDef_Slot structures without an enclosing PyModuleDef structure. This allows extension authors to avoid using a statically allocated PyObject, lifting the most common obstacle to making one compiled library file usable with both regular and free-threaded builds of CPython.

To make this viable, we also specify new module slot types to replace PyModuleDef's fields, and to allow adding a token similar to the Py_tp_token used for type objects.

We also add an API for defining modules from slots dynamically.

The existing API (PyInit_*) is soft-deprecated. (That is: it will continue to work without warnings, and it'll be fully documented and supported, but we plan to not add any new features to it.)

Background & Motivation

The memory layout of Python objects differs between regular and free-threading builds. So, an ABI that supports both regular and free-threading builds cannot include the current PyObject memory layout. To stay compatible with existing ABI (and API), it cannot support statically allocated Python objects.

There is one type of object that is needed in most extension modules and is allocated statically in virtually all cases: the PyModuleDef returned from the module export hooks (that is, PyInit_* functions).

Module export hooks (PyInit_* functions) can return two kinds of objects:

  1. A fully initialized module object (for so-called single-phase initialization). This was the only option in 3.4 and below. Modules created this way have surprising (but backwards-compatible) behaviour around multiple interpreters or repeated loading. (Specifically, the contents of such a module's __dict__ are shared across all instances of the module object.)

    The module returned is typically created using the PyModule_Create function, which requires a statically allocated (or at least long-lived) PyModuleDef struct.

    It is possible to bypass this using the lower-level PyModule_New* API. This avoids the need for PyModuleDef, but offers much less functionality.

  2. A PyModuleDef object containing a description of how to create a module object. This option, multi-phase initialization, was introduced in PEP 489; see its motivation for why it exists.

The interpreter cannot distinguish between these cases before the export hook is called.

The interpreter switch

Python 3.12 added a way for modules to mark whether they may be loaded in a subinterpreter: the Py_mod_multiple_interpreters slot. Setting it to the “not supported” value signals that an extension can only be loaded in the main interpreter.

Unfortunately, Python can only get this information by calling the module export hook. For single-phase modules, that creates the module object and runs arbitrary initialization code. For modules that set Py_mod_multiple_interpreters to “not supported”, this initialization needs to happen in the main interpreter.

To make this work, if a new module is loaded in a sub-interpreter, Python temporarily switches to the main interpreter, calls the export hook there, and then either switches back and redoes the import, or fails.

This unnecessary and fragile extra work highlights the underlying design issue: Python has no way to get information about an extension before the extension can, potentially, fully initialize itself.

Rationale

For avoiding the module export hook requiring a statically allocated PyObject*, two options come to mind:

  • Returning a dynamically allocated object, whose ownership is transferred to the interpreter. This stucture could be very similar to the existing PyModuleDef, since it needs to contain the same data. Unlike the existing PyModuleDef, this one would need to be reference-counted so that it both outlives “its” module and does not leak.

  • Adding a new export hook, which does not return a PyObject*.

    This was considered already for Python 3.5 in PEP 489, but rejected:

    Keeping only the PyInit hook name, even if it’s not entirely appropriate for exporting a definition, yielded a much simpler solution.

    Alas, after a decade of fixing the implications of this choice, the solution is no longer simple.

A new hook will also allow Python to avoid the second issue mentioned in Motivation -- the interpreter switch. Effectivelly, it will add a new phase to multi-phase initialization, in which Python can check whether the module is compatible.

Using slots without a wrapper struct

The existing PyModuleDef is a struct with some fixed fields and a “slots” array. Unlike slots, the fixed fields cannot be individually deprecated and replaced. This proposal does away with fixed fields and proposes using a slots array directly, without a wrapper struct.

The PyModuleDef_Slot struct does have some downsides compared to fixed fields. We believe these are fixable, but leave that out of scope of this PEP (see “Improving slots in general” in the Possible Future Directions section).

Tokens

A static PyModuleDef has another purpose besides describing how a module should be created. As a statically allocated singleton that remains attached to the module object, it allows extension authors to check whether a given Python module is “theirs”: if a module object has a known PyModuleDef, its module state will have a known memory layout.

An analogous issue was solved for types by adding Py_tp_token. This proposal adds the same mechanism to modules.

Unlike types, the import mechanism often has a pointer that's known to be suitable as a token value; in these cases it can provide a default token. Thus, module tokens do not need a variant of the inelegant Py_TP_USE_SPEC.

To help extensions that straddle Python versions, PyModuleDef addresses are used as default tokens, and where it's reasonable, they are made interchangeable with tokens.

Soft-deprecating the existing export hook

The only reason for authors of existing extensions to switch to the API proposed here is that it allows a single module for both free-threaded and non-free-threaded builds. It is important that Python allows that, but for many existing modules, it is nowhere near worth losing compatibility with 3.14 and lower versions.

It is much too early to plan deprecation of the old API.

Instead, this PEP proposes to stop adding new features to the PyInit_* scheme. After all, the perfect time for extension authors to switch is when they want to modify module initialization anyway.

Specification

The export hook

When importing an extension module, Python will now first look for an export hook like this:

PyModuleDef_Slot *PyModExport_<NAME>(void);

where <NAME> is the name of the module. For non-ASCII names, it will instead look for PyModExportU_<NAME>, with <NAME> encoded as for existing PyInitU_* hooks (that is, punycode-encoded with hyphens replaced by underscores).

If not found, the import will continue as in previous Python versions (that is, by looking up a PyInit_* or PyInitU_* function).

If found, Python will call the hook with no arguments.

On failure, the export hook must return NULL with an exception set. This will cause the import to fail. (Python will not fall back to PyInit_* on error.)

On success, the hook must return a pointer to an array of PyModuleDef_Slot structs. Python will then create a module based on the given slots by calling functions proposed below: PyModule_FromSlotsAndSpec and PyModule_Exec. See their description for requirements on the slots array.

The returned array and all data it points to (recursively) must remain valid and constant until runtime shutdown. (We expect functions to export a static constant, or one of several constants chosen depending on, for example, Py_Version. Dynamic behaviour should generally happen in the Py_mod_create and Py_mod_exec functions.)

Dynamic creation

A new function will be added to create a module from an array of slots:

PyObject *PyModule_FromSlotsAndSpec(const PyModuleDef_Slot *slots, PyObject *spec)

The slots argument must point to an array of PyModuleDef_Slot structures, terminated by a slot with slot=0 (typically written as {0} in C). There are no required slots, though slots must not be NULL. It follows that minimal input contains only the terminator slot.

Note: If PEP 803 is accepted, the Py_mod_abi slot will be mandatory. The spec argument is a duck-typed ModuleSpec-like object, meaning that any attributes defined for importlib.machinery.ModuleSpec have matching semantics. The name attribute is required, but this limitation may be lifted in the future. The name will be used instead of the Py_mod_name slot (just like PyModule_FromDefAndSpec ignores PyModuleDef.m_name).

The slots arrays for both PyModule_FromSlotsAndSpec and the new export hook will only allow up to one Py_mod_exec slot. Arrays in PyModuleDef.m_slots may have more; this will not change. This limitation is easy to work around and multiple exec slots are rarely used [#multiexec]_.

For modules created without a PyModuleDef, the Py_mod_create function will be called with NULL for the second argument (def). (In the future, if we find a use case for passing the input slots array, a new slot with an updated signature can be added.)

Unlike the PyModExport_* hook, the slots array may be changed or destroyed after the PyModule_FromSlotsAndSpec call. (That is, Python must take a copy of all input data.) As an exception, any PyMethodDef array given by Py_mod_methods must be statically allocated (or be otherwise guaranteed to outlive the objects created from it). This limitation may be lifted in the future.

A new function, PyModule_Exec, will be added to run the exec slot(s) for a module. This acts like PyModule_ExecDef, but supports modules created using slots, and does not take an explicit def:

int PyModule_Exec(PyObject *module)

Calling this is required to fully initialize a module. PyModule_FromSlotsAndSpec will not run it (just like PyModule_FromDefAndSpec does not call PyModule_ExecDef).

For modules created from a def, calling this is equivalent to calling PyModule_ExecDef(module, PyModule_GetDef(module)).

Tokens

Module objects will optionally store a “token”: a void* pointer similar to Py_tp_token for types.

Note: This is specialized functionality meant replace the PyType_GetModuleByDef function; users that don't need PyType_GetModuleByDef will most likely not need tokens either. This section contains the technical specification; for an example of intended usage, see exampletype_repr in the Example section <pep793-example>. If specified, using a new Py_mod_token slot, the module token must:

  • outlive the module, so it's not reused for something else while the module exists; and
  • "belong" to the extension module where the module lives, so it will not clash with other extension modules.

(Typically, it should be the slots array or PyModuleDef that a module is created from, or another static constant for dynamically created modules.)

When the address of a PyModuleDef is used as a module's token, the module should behave as if it was created from that PyModuleDef. In particular, the module state must have matching layout and semantics.

Modules created using the PyModule_FromSlotsAndSpec or the PyModExport_<NAME> export hook can use a new Py_mod_token slot to set the token.

Modules created from a PyModuleDef will have the token set to that definition. An explicit Py_mod_token slot will we rejected for these. (This allows implementations to share storage for the token and def.)

For modules created via the new export hook, the token will be set to the address of the slots array by default. (This does not apply to modules created by PyModule_FromSlotsAndSpec, as that function's input might not outlive the module.)

The token will not be set for non-PyModuleType instances.

A PyModule_GetToken function will be added to get the token. Since the result may be NULL, it will be passed via a pointer; the function will return 0 on success and -1 on failure:

int PyModule_GetToken(PyObject *, void **token_p)

A new PyType_GetModuleByToken function will be added, with a signature like the existing PyType_GetModuleByDef but a const void *token argument, and the same behaviour except matching tokens rather than only defs, and returning a strong reference.

For easier backwards compatibility, the existing PyType_GetModuleByDef will be changed to also allow a token (cast to a PyModuleDef * pointer) as the def argument. That is, PyType_GetModuleByToken and PyType_GetModuleByDef will differ only in the formal signature of the second argument and by returning a borrowed vs. strong reference. (The PyModule_GetDef function will not get a similar change, as users may access members of its result.)

New slots

For each field of the PyModuleDef struct, except ones from PyModuleDef_HEAD_INIT, a new slot ID will be provided: Py_mod_name, Py_mod_doc, Py_mod_clear, etc. Slots related to the module state rather than the module object will use a Py_mod_state_ prefix. See pep793-api-summary for a full list.

All new slots -- these and Py_tp_token discussed above -- may not be repeated in the slots array, and may not be used in a PyModuleDef.m_slots array. They may not have a NULL value (instead, the slot can be omitted entirely).

Note that currently, for modules created from a spec (that is, using PyModule_FromDefAndSpec), the PyModuleDef.m_name member is ignored and the name from the spec is used instead. All API proposed in this document creates modules from a spec, and it will ignore Py_mod_name in the same way. The slot will be optional, but extension authors are strongly encouraged to include it for the benefit of future APIs, external tooling, debugging, and introspection.

Bits & Pieces

A PyMODEXPORT_FUNC macro will be added, similar to the PyMODINIT_FUNC macro but with PyModuleDef_Slot * as the return type.

A PyModule_GetStateSize function will be added to retrieve the size set by Py_mod_state_size or PyModuleDef.m_size. Since the result may be -1 (for single-phase-init modules), it will be output via a pointer; the function will return 0 on success and -1 on failure:

int PyModule_GetStateSize(PyObject *, Py_ssize_t *result);

Soft-deprecating the existing export hook

The PyInit_* export hook will be soft-deprecated <pep387-soft-deprecation>.

New API summary

Python will load a new module export hook, with two variants:

PyModuleDef_Slot *PyModExport_<NAME>(void);
PyModuleDef_Slot *PyModExportU_<ENCODED_NAME>(void);

The following functions will be added:

PyObject *PyModule_FromSlotsAndSpec(const PyModuleDef_Slot *, PyObject *spec)
int PyModule_Exec(PyObject *)
int PyModule_GetToken(PyObject *, void**)
PyObject *PyType_GetModuleByToken(PyTypeObject *type, const void *token)
int PyModule_GetStateSize(PyObject *, Py_ssize_t *result);

A new macro will be added:

PyMODEXPORT_FUNC

And new slot types (#define\d names for small integers):

  • Py_mod_name (equivalent to PyModuleDef.m_name)
  • Py_mod_doc (equivalent to PyModuleDef.m_doc)
  • Py_mod_state_size (equivalent to PyModuleDef.m_size)
  • Py_mod_methods (equivalent to PyModuleDef.m_methods)
  • Py_mod_state_traverse (equivalent to PyModuleDef.m_traverse)
  • Py_mod_state_clear (equivalent to PyModuleDef.m_clear)
  • Py_mod_state_free (equivalent to PyModuleDef.m_free)
  • Py_mod_token (see above)

All this will be added to the Limited API.

Backwards Compatibility

If an existing module is ported to use the new mechanism, then PyModule_GetDef will start returning NULL for it. (This matches PyModule_GetDef's current documentation.) We claim that how a module was defined is an implementation detail of that module, so this should not be considered a breaking change.

Similarly, the PyType_GetModuleByDef function may stop matching modules whose definition changed. Module authors may avoid this by explicitly setting a def as the token.

PyType_GetModuleByDef will now accept a module token as the def argument. We specify a suitable restriction on using PyModuleDef addresses as tokens, and non-PyModuleDef pointers were previously invalid input, so this is not a backwards-compatibility issue.

The Py_mod_create function may now be called with NULL for the second argument. This could trip people porting from def to slots, so it needs to be mentioned in porting notes.

Forward compatibility

If a module defines the new export hook, CPython versions that implement this PEP will ignore the traditional PyInit_* hook.

Extensions that straddle Python versions are expected to define both hooks; each build of CPython will “pick” the newest one that it supports.

Porting guide

Here is a guide to convert an existing module to the new API, including some tricky edge cases. It should be moved to a HOWTO in the documentation.

This guide is meant for hand-written modules. For code generators and language wrappers, the pep793-shim below may be more useful.

#. Scan your code for uses of PyModule_GetDef. This function will return NULL for modules that use the new mechanism. Instead:

  • For getting the contents of the module's PyModuleDef, use the C struct directly. Alternatively, get attributes from the module using, for example, PyModule_GetNameObject, the __doc__ attribute, and PyModule_GetStateSize. (Note that Python code can mutate a module's attributes.)
  • For testing if a module object is “yours”, use PyModule_GetToken instead. Later in this guide, you'll set the token to be the existing PyModuleDef structure.

#. Optionally, scan your code for uses of PyType_GetModuleByDef, and replace them with PyType_GetModuleByToken. Later in this guide, you'll set the token to be the existing PyModuleDef structure.

(You may skip this step if targetting Python versions that don't expose PyType_GetModuleByToken, since PyType_GetModuleByDef is backwards-compatible.)

#. Look at the function identified by Py_mod_create, if any. Make sure that it does not use its second argument (PyModuleDef), as it will be called with NULL. Instead of the argument, use the existing PyModuleDef struct directly. #. If using multiple Py_mod_exec slots, consolidate them: pick one of the functions, or write a new one, and call the others from it. Remove all but one Py_mod_exec slots. #. Make a copy of the existing PyModuleDef_Slot array pointed to by the m_slots member of your PyModuleDef. If you don't have an existing slots array, create one like this:

static PyModuleDef_Slot module_slots[] = {
    {0}
};

Give this array a unique name. Further examples will assume that you've named it module_slots.

#. Add slots for all members of the existing PyModuleDef structure. See pep793-api-summary for a list of the new slots. For example, to add a name and docstring:

static PyModuleDef_Slot module_slots[] = {
    {Py_mod_name, "mymodule"},
    {Py_mod_doc, (char*)PyDoc_STR("my docstring")},
    // ... (keep existing slots here)
    {0}
};

#. If you switched from PyModule_GetDef to PyModule_GetToken, and/or if you use PyType_GetModuleByDef or PyType_GetModuleByToken, add a Py_mod_token slot pointing to the existing PyModuleDef struct:

static PyModuleDef_Slot module_slots[] = {
    // ... (keep existing slots here)
    {Py_mod_token, &your_module_def},
    {0}
};

#. Add a new export hook.

PyMODEXPORT_FUNC PyModExport_examplemodule(PyObject);

PyMODEXPORT_FUNC
PyModExport_examplemodule(void)
{
    return module_slots;
}

The new export hook will be used on Python 3.15 and above. Once your module no longer supports lower versions:

#. Delete the PyInit_ function.

#. If the existing PyModuleDef struct is used only for Py_mod_token and/or PyType_GetModuleByToken, you may remove the Py_mod_token line and replace &your_module_def with module_slots everywhere else.

#. Delete any unused data. The PyModuleDef struct and the original slots array are likely to be unused.

Backwards compatibility shim

It is possible to write a generic function that implements the “old” export hook (PyInit_) in terms of the API proposed here.

The following implementation can be copied and pasted to a project; only the names PyInit_examplemodule (twice) and PyModExport_examplemodule should need adjusting.

When added to the pep793-example below and compiled with a non-free-threaded build of this PEP's reference implementation, the resulting extension is compatible with non-free-threading 3.9+ builds, in addition to a free-threading build of the reference implementation. (The module must be named without a version tag, e.g. examplemodule.so, and be placed on sys.path.)

Full support for creating such modules will require backports of some new API, and support in build/install tools. This is out of scope of this PEP. (In particular, the demo “cheats” by using a subset of Limited API 3.15 that happens to work on 3.9; a proper implementation would use Limited API 3.9 with backport shims for new API like Py_mod_name.)

This implementation places a few additional requirements on the slots array:

  • Slots that correspond to PyModuleDef members must come first.
  • A Py_mod_name slot is required.
  • Any Py_mod_token must be set to &module_def_and_token, defined here.

(see pep-0793/shim.c for the full listing):language: c

Security Implications

None known

How to Teach This

In addition to regular reference docs, the pep793-porting-notes should be added as a new HOWTO.

Example

(see pep-0793/examplemodule.c for the full listing):language: c

Rejected Ideas

Exporting a data pointer rather than a function

This proposes a new module export function, which is expected to return static constant data. That data could be exported directly as a data pointer.

With a function, we avoid dealing with a new kind of exported symbol.

A function also allows the extension to introspect its environment in a limited way -- for example, to tailor the returned data to the current Python version.

Changing PyModuleDef to not be PyObject

It is possible to change PyModuleDef to no longer include the PyObject header, and continue using the current PyInit_* hook. There are several issues with this approach:

  • The import machinery would need to examine bit-patterns in the objects to distinguish between different memory layouts:

    • the “old” PyObject-based PyModuleDef, returned by current abi3 extensions,
    • the new PyModuleDef,
    • PyObject-based module objects, for single-phase initialization.

    This is fragile, and places constraints on future changes to PyObject: the memory layouts need to stay distinguishable until both single-phase initialization and the current Stable ABI are no longer supported.

  • PyModuleDef_Init is documented to “Ensure a module definition is a properly initialized Python object that correctly reports its type and a reference count.” This would need to change without warning, breaking any user code that treats PyModuleDef\ s as Python objects.

Possible Future Directions

These ideas are out of scope for this proposal.

Improving slots in general

Slots -- and specifically the existing PyModuleDef_Slot -- do have a few shortcomings. The most important are:

  • Type safety: void * is used for data pointers, function pointers and small integers, requiring casting that is technically undefined behaviour in C -- but works in practice on all relevant architectures. (For example: Py_tp_doc marks a string; Py_mod_gil an integer.)

  • Limited forward compatibility: if an extension provides a slot ID that's unknown to the current interpreter, module creation will fail. This makes it cumbersome to use “optional” features -- ones that should only take effect if the interpreter supports them. (The recently added slots Py_mod_gil and Py_mod_multiple_interpreters are good examples.)

    One workaround is to check Py_Version in the export function, and return a slot array suitable for the current interpreter.

Updating defaults

With a new API, we could update defaults for the Py_mod_multiple_interpreters and Py_mod_gil slots.

The inittab

We'll need to allow PyModuleDef-less slots in the inittab -- that is, add a new variant of PyImport_ExtendInittab. Should that be part of this PEP?

The inittab is used for embedding, where a common/stable ABI is not that important. So, it might be OK to leave this to a later change.

Linked PEP 489 — Multi-phase extension module initialization

Abstract

This PEP proposes a redesign of the way in which built-in and extension modules interact with the import machinery. This was last revised for Python 3.0 in PEP 3121, but did not solve all problems at the time. The goal is to solve import-related problems by bringing extension modules closer to the way Python modules behave; specifically to hook into the ModuleSpec-based loading mechanism introduced in PEP 451.

This proposal draws inspiration from PyType_Spec of PEP 384 to allow extension authors to only define features they need, and to allow future additions to extension module declarations.

Extensions modules are created in a two-step process, fitting better into the ModuleSpec architecture, with parallels to __new__ and __init__ of classes.

Extension modules can safely store arbitrary C-level per-module state in the module that is covered by normal garbage collection and supports reloading and sub-interpreters. Extension authors are encouraged to take these issues into account when using the new API.

The proposal also allows extension modules with non-ASCII names.

Not all problems tackled in PEP 3121 are solved in this proposal. In particular, problems with run-time module lookup (PyState_FindModule) are left to a future PEP.

Motivation

Python modules and extension modules are not being set up in the same way. For Python modules, the module object is created and set up first, then the module code is being executed (PEP 302). A ModuleSpec object (PEP 451) is used to hold information about the module, and passed to the relevant hooks.

For extensions (i.e. shared libraries) and built-in modules, the module init function is executed straight away and does both the creation and initialization. The initialization function is not passed the ModuleSpec, or any information it contains, such as the __file__ or fully-qualified name. This hinders relative imports and resource loading.

In Py3, modules are also not being added to sys.modules, which means that a (potentially transitive) re-import of the module will really try to re-import it and thus run into an infinite loop when it executes the module init function again. Without access to the fully-qualified module name, it is not trivial to correctly add the module to sys.modules either. This is specifically a problem for Cython generated modules, for which it's not uncommon that the module init code has the same level of complexity as that of any 'regular' Python module. Also, the lack of __file__ and __name__ information hinders the compilation of "__init__.py" modules, i.e. packages, especially when relative imports are being used at module init time.

Furthermore, the majority of currently existing extension modules has problems with sub-interpreter support and/or interpreter reloading, and, while it is possible with the current infrastructure to support these features, it is neither easy nor efficient. Addressing these issues was the goal of PEP 3121, but many extensions, including some in the standard library, took the least-effort approach to porting to Python 3, leaving these issues unresolved. This PEP keeps backwards compatibility, which should reduce pressure and give extension authors adequate time to consider these issues when porting.

The current process

Currently, extension and built-in modules export an initialization function named "PyInit_modulename", named after the file name of the shared library. This function is executed by the import machinery and must return a fully initialized module object. The function receives no arguments, so it has no way of knowing about its import context.

During its execution, the module init function creates a module object based on a PyModuleDef object. It then continues to initialize it by adding attributes to the module dict, creating types, etc.

In the back, the shared library loader keeps a note of the fully qualified module name of the last module that it loaded, and when a module gets created that has a matching name, this global variable is used to determine the fully qualified name of the module object. This is not entirely safe as it relies on the module init function creating its own module object first, but this assumption usually holds in practice.

The proposal

The initialization function (PyInit_modulename) will be allowed to return a pointer to a PyModuleDef object. The import machinery will be in charge of constructing the module object, calling hooks provided in the PyModuleDef in the relevant phases of initialization (as described below).

This multi-phase initialization is an additional possibility. Single-phase initialization, the current practice of returning a fully initialized module object, will still be accepted, so existing code will work unchanged, including binary compatibility.

The PyModuleDef structure will be changed to contain a list of slots, similarly to PEP 384's PyType_Spec for types. To keep binary compatibility, and avoid needing to introduce a new structure (which would introduce additional supporting functions and per-module storage), the currently unused m_reload pointer of PyModuleDef will be changed to hold the slots. The structures are defined as:

typedef struct {
    int slot;
    void *value;
} PyModuleDef_Slot;

typedef struct PyModuleDef {
    PyModuleDef_Base m_base;
    const char* m_name;
    const char* m_doc;
    Py_ssize_t m_size;
    PyMethodDef *m_methods;
    PyModuleDef_Slot *m_slots;  /* changed from `inquiry m_reload;` */
    traverseproc m_traverse;
    inquiry m_clear;
    freefunc m_free;
} PyModuleDef;

The m_slots member must be either NULL, or point to an array of PyModuleDef_Slot structures, terminated by a slot with id set to 0 (i.e. {0, NULL}).

To specify a slot, a unique slot ID must be provided. New Python versions may introduce new slot IDs, but slot IDs will never be recycled. Slots may get deprecated, but will continue to be supported throughout Python 3.x.

A slot's value pointer may not be NULL, unless specified otherwise in the slot's documentation.

The following slots are currently available, and described later:

  • Py_mod_create
  • Py_mod_exec

Unknown slot IDs will cause the import to fail with SystemError.

When using multi-phase initialization, the m_name field of PyModuleDef will not be used during importing; the module name will be taken from the ModuleSpec.

Before it is returned from PyInit_*, the PyModuleDef object must be initialized using the newly added PyModuleDef_Init function. This sets the object type (which cannot be done statically on certain compilers), refcount, and internal bookkeeping data (m_index). For example, an extension module "example" would be exported as:

static PyModuleDef example_def = {...}

PyMODINIT_FUNC
PyInit_example(void)
{
    return PyModuleDef_Init(&example_def);
}

The PyModuleDef object must be available for the lifetime of the module created from it -- usually, it will be declared statically.

Module Creation Phase

Creation of the module object -- that is, the implementation of ExecutionLoader.create_module -- is governed by the Py_mod_create slot.

The Py_mod_create slot

The Py_mod_create slot is used to support custom module subclasses. The value pointer must point to a function with the following signature:

PyObject* (*PyModuleCreateFunction)(PyObject *spec, PyModuleDef *def)

The function receives a ModuleSpec instance, as defined in PEP 451, and the PyModuleDef structure. It should return a new module object, or set an error and return NULL.

This function is not responsible for setting import-related attributes specified in PEP 451 (such as __name__ or __loader__) on the new module.

There is no requirement for the returned object to be an instance of types.ModuleType. Any type can be used, as long as it supports setting and getting attributes, including at least the import-related attributes. However, only ModuleType instances support module-specific functionality such as per-module state and processing of execution slots. If something other than a ModuleType subclass is returned, no execution slots may be defined; if any are, a SystemError is raised.

Note that when this function is called, the module's entry in sys.modules is not populated yet. Attempting to import the same module again (possibly transitively), may lead to an infinite loop. Extension authors are advised to keep Py_mod_create minimal, an in particular to not call user code from it.

Multiple Py_mod_create slots may not be specified. If they are, import will fail with SystemError.

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.

Post-creation steps

If the Py_mod_create function returns an instance of types.ModuleType or a subclass (or if a Py_mod_create slot is not present), the import machinery will associate the PyModuleDef with the module. This also makes the PyModuleDef accessible to execution phase, the PyModule_GetDef function, and garbage collection routines (traverse, clear, free).

If the Py_mod_create function does not return a module subclass, then m_size must be 0, and m_traverse, m_clear and m_free must all be NULL. Otherwise, SystemError is raised.

Additionally, initial attributes specified in the PyModuleDef are set on the module object, regardless of its type:

  • The docstring is set from m_doc, if non-NULL.
  • The module's functions are initialized from m_methods, if any.

Module Execution Phase

Module execution -- that is, the implementation of ExecutionLoader.exec_module -- is governed by "execution slots". This PEP only adds one, Py_mod_exec, but others may be added in the future.

The execution phase is done on the PyModuleDef associated with the module object. For objects that are not a subclass of PyModule_Type (for which PyModule_GetDef would fail), the execution phase is skipped.

Execution slots may be specified multiple times, and are processed in the order they appear in the slots array. When using the default import machinery, they are processed after import-related attributes specified in PEP 451 (such as __name__ or __loader__) are set and the module is added to sys.modules.

Pre-Execution steps

Before processing the execution slots, per-module state is allocated for the module. From this point on, per-module state is accessible through PyModule_GetState.

The Py_mod_exec slot

The entry in this slot must point to a function with the following signature:

int (*PyModuleExecFunction)(PyObject* module)

It will be called to initialize a module. Usually, this amounts to setting the module's initial attributes. The "module" argument receives the module object to initialize.

The function must return 0 on success, or, on error, set an exception and return -1.

Legacy Init

The backwards-compatible single-phase initialization continues to be supported. In this scheme, the PyInit function returns a fully initialized module rather than a PyModuleDef object. In this case, the PyInit hook implements the creation phase, and the execution phase is a no-op.

Modules that need to work unchanged on older versions of Python should stick to single-phase initialization, because the benefits it brings can't be back-ported.

Functions incompatible with multi-phase initialization

The PyModule_Create function will fail when used on a PyModuleDef structure with a non-NULL m_slots pointer. The function doesn't have access to the ModuleSpec object necessary for multi-phase initialization.

The PyState_FindModule function will return NULL, and PyState_AddModule and PyState_RemoveModule will also fail on modules with non-NULL m_slots. PyState registration is disabled because multiple module objects may be created from the same PyModuleDef.

Export Hook Name

As portable C identifiers are limited to ASCII, module names must be encoded to form the PyInit hook name.

For ASCII module names, the import hook is named PyInit_<modulename>, where <modulename> is the name of the module.

For module names containing non-ASCII characters, the import hook is named PyInitU_<encodedname>, where the name is encoded using CPython's "punycode" encoding (RFC 3492 with a lowercase suffix), with hyphens ("-") replaced by underscores ("_").

In Python:

def export_hook_name(name):
    try:
        suffix = b'_' + name.encode('ascii')
    except UnicodeEncodeError:
        suffix = b'U_' + name.encode('punycode').replace(b'-', b'_')
    return b'PyInit' + suffix

Examples:

Module name Init hook name
spam PyInit_spam
lančmít PyInitU_lanmt_2sa6t
スパム PyInitU_zck5b2b

For modules with non-ASCII names, single-phase initialization is not supported.

Summary of API Changes and Additions

New functions:

  • PyModule_FromDefAndSpec (macro)
  • PyModule_FromDefAndSpec2
  • PyModule_ExecDef
  • PyModule_SetDocString
  • PyModule_AddFunctions
  • PyModuleDef_Init

New macros:

  • Py_mod_create
  • Py_mod_exec

New types:

  • PyModuleDef_Type will be exposed

New structures:

  • PyModuleDef_Slot

Other changes:

PyModuleDef.m_reload changes to PyModuleDef.m_slots.

BuiltinImporter and ExtensionFileLoader will now implement create_module and exec_module.

The internal _imp module will have backwards incompatible changes: create_builtin, create_dynamic, and exec_dynamic will be added; init_builtin, load_dynamic will be removed.

PEP 489 introduced multi-phase initialization and the PyModuleDef_Slot array that PEP 793 builds upon. PEP 793's "Rationale" defers to PEP 489's motivation for why multi-phase init exists, and its "Specification" reuses the same PyModuleDef_Slot structure -- the new PyModExport_* hook returns an array of these slots directly, without the wrapping PyModuleDef introduced here. PEP 793's Py_mod_name, Py_mod_doc, Py_mod_state_size, Py_mod_methods, Py_mod_state_traverse, Py_mod_state_clear, and Py_mod_state_free slots replace the PyModuleDef fields specified above. The "Rejected Ideas" section's note that a PyModuleExport hook was previously considered (with the comment "Keeping only the PyInit hook name ... yielded a much simpler solution") is the historical antecedent PEP 793 explicitly reverses.

Source: https://peps.python.org/pep-0489/

Linked PEP 387 — Backwards Compatibility Policy

Soft Deprecation

A soft deprecation can be used when using an API which should no longer be used to write new code, but it remains safe to continue using it in existing code. The API remains documented and tested, but will not be developed further (no enhancement).

The main difference between a "soft" and a (regular) "hard" deprecation is that the soft deprecation does not imply scheduling the removal of the deprecated API.

Another difference is that a soft deprecation does not issue a warning: it's only mentioned in the documentation, whereas usually a "hard" deprecation issues a DeprecationWarning warning at runtime. The documentation of a soft deprecation should explain why the API should be avoided, and if possible propose a replacement.

If the decision is made to deprecate (in the regular sense) a feature that is currently soft deprecated, the deprecation must follow the Backwards Compatibility Rules (i.e., there is no exception because the feature is already soft deprecated).

PEP 793's "Soft-deprecating the existing export hook" section invokes this definition: the existing PyInit_* API is marked as soft-deprecated, meaning it continues to work without warnings and remains fully supported and documented, but Python will not add new features to it. New features (such as combined free-threaded/non-free-threaded build support) only land on the new PyModExport_* hook.

Source: https://peps.python.org/pep-0387/#soft-deprecation