@@ -184,7 +184,7 @@ like this:
184184
185185.. code-block :: c
186186
187- PyModuleDef_Slot *PyModExport_<NAME>(PyObject *spec );
187+ PyModuleDef_Slot *PyModExport_<NAME>(void );
188188
189189 where ``<NAME> `` is the name of the module.
190190For non-ASCII names, it will instead look for ``PyModExportU_<NAME> ``,
@@ -194,12 +194,7 @@ with ``<NAME>`` encoded as for existing ``PyInitU_*`` hooks
194194If not found, the import will continue as in previous Python versions (that is,
195195by looking up a ``PyInit_* `` or ``PyInitU_* `` function).
196196
197- If found, Python will call the hook with the appropriate
198- ``importlib.machinery.ModuleSpec `` object as *spec *.
199- To support duck-typing, extensions should not type-check this object, and
200- if possible, implement fallbacks for any missing attributes.
201- (The argument is mainly meant for introspection, testing, or use with
202- specialized loaders.)
197+ If found, Python will call the hook with no arguments.
203198
204199On failure, the export hook must return NULL with an exception set.
205200This will cause the import to fail.
@@ -225,13 +220,17 @@ A new function will be added to create a module from an array of slots:
225220
226221.. code-block :: c
227222
228- PyObject *PyModule_FromSlotsAndSpec(PyModuleDef_Slot *slots, PyObject *spec)
223+ PyObject *PyModule_FromSlotsAndSpec(const PyModuleDef_Slot *slots, PyObject *spec)
229224
230225 The *slots * argument must point to an array of ``PyModuleDef_Slot `` structures,
231226terminated by a slot with ``slot=0 `` (typically written as ``{0} `` in C).
232227There are no required slots, though *slots * must not be ``NULL ``.
233228It follows that minimal input contains only the terminator slot.
234229
230+ .. note ::
231+
232+ If :pep: `803 ` is accepted, the ``Py_mod_abi `` slot will be mandatory.
233+
235234The *spec * argument is a duck-typed ModuleSpec-like object, meaning that any
236235attributes defined for ``importlib.machinery.ModuleSpec `` have matching
237236semantics.
@@ -274,20 +273,33 @@ For modules created from a *def*, calling this is equivalent to
274273calling ``PyModule_ExecDef(module, PyModule_GetDef(module)) ``.
275274
276275
276+ .. _pep793-token :
277+
277278Tokens
278279------
279280
280281Module objects will optionally store a “token”: a ``void* `` pointer
281282similar to ``Py_tp_token `` for types.
282283
284+ .. note ::
285+
286+ This is specialized functionality meant replace the
287+ ``PyType_GetModuleByDef `` function; users that don't need
288+ ``PyType_GetModuleByDef `` will most likely not need tokens either.
289+
290+ This section contains the technical specification;
291+ for an example of intended usage, see ``exampletype_repr `` in the
292+ :ref: `Example section <pep793-example >`.
293+
283294If specified, using a new ``Py_mod_token `` slot, the module token must:
284295
285296- outlive the module, so it's not reused for something else while the module
286297 exists; and
287298- "belong" to the extension module where the module lives, so it will not
288299 clash with other extension modules.
289300
290- (Typically, it should point to a static constant.)
301+ (Typically, it should be the slots array or ``PyModuleDef `` that a module is
302+ created from, or another static constant for dynamically created modules.)
291303
292304When the address of a ``PyModuleDef `` is used as a module's token,
293305the module should behave as if it was created from that ``PyModuleDef ``.
@@ -317,7 +329,7 @@ will return 0 on success and -1 on failure:
317329 int PyModule_GetToken(PyObject *, void **token_p)
318330
319331 A new ``PyType_GetModuleByToken `` function will be added, with a signature
320- like the existing ``PyType_GetModuleByDef `` but a ``void *token `` argument,
332+ like the existing ``PyType_GetModuleByDef `` but a ``const void *token `` argument,
321333and the same behaviour except matching tokens rather than only defs,
322334and returning a strong reference.
323335
@@ -388,17 +400,17 @@ Python will load a new module export hook, with two variants:
388400
389401.. code-block :: c
390402
391- PyModuleDef_Slot *PyModExport_<NAME>(PyObject *spec );
392- PyModuleDef_Slot *PyModExportU_<ENCODED_NAME>(PyObject *spec );
403+ PyModuleDef_Slot *PyModExport_<NAME>(void );
404+ PyModuleDef_Slot *PyModExportU_<ENCODED_NAME>(void );
393405
394406 The following functions will be added:
395407
396408.. code-block :: c
397409
398- PyObject *PyModule_FromSlotsAndSpec(PyModuleDef_Slot *, PyObject *spec)
410+ PyObject *PyModule_FromSlotsAndSpec(const PyModuleDef_Slot *, PyObject *spec)
399411 int PyModule_Exec(PyObject *)
400412 int PyModule_GetToken(PyObject *, void**)
401- PyObject *PyType_GetModuleByToken(PyTypeObject *type, void *token)
413+ PyObject *PyType_GetModuleByToken(PyTypeObject *type, const void *token)
402414 int PyModule_GetStateSize(PyObject *, Py_ssize_t *result);
403415
404416 A new macro will be added:
@@ -541,7 +553,7 @@ wrappers, the :ref:`pep793-shim` below may be more useful.
541553 PyMODEXPORT_FUNC PyModExport_examplemodule(PyObject);
542554
543555 PyMODEXPORT_FUNC
544- PyModExport_examplemodule(PyObject *spec )
556+ PyModExport_examplemodule(void )
545557 {
546558 return module_slots;
547559 }
@@ -591,9 +603,6 @@ This implementation places a few additional requirements on the slots array:
591603- A ``Py_mod_name `` slot is required.
592604- Any ``Py_mod_token `` must be set to ``&module_def_and_token ``, defined here.
593605
594- It also passes ``NULL `` as *spec * to the ``PyModExport `` export hook.
595- A proper implementation would pass ``None `` instead.
596-
597606.. literalinclude :: pep-0793/shim.c
598607 :language: c
599608
@@ -651,6 +660,33 @@ A function also allows the extension to introspect its environment in a limited
651660way -- for example, to tailor the returned data to the current Python version.
652661
653662
663+ Changing ``PyModuleDef `` to not be ``PyObject ``
664+ -----------------------------------------------
665+
666+ It is possible to change ``PyModuleDef `` to no longer include the ``PyObject ``
667+ header, and continue using the current ``PyInit_* `` hook.
668+ There are several issues with this approach:
669+
670+ - The import machinery would need to examine bit-patterns in the objects to
671+ distinguish between different memory layouts:
672+
673+ - the “old” ``PyObject ``-based ``PyModuleDef ``, returned by current ``abi3 ``
674+ extensions,
675+ - the new ``PyModuleDef ``,
676+ - ``PyObject ``-based module objects, for single-phase initialization.
677+
678+ This is fragile, and places constraints on future changes to ``PyObject ``:
679+ the memory layouts need to stay *distinguishable * until both single-phase
680+ initialization and the current Stable ABI are no longer supported.
681+
682+
683+ - ``PyModuleDef_Init `` is documented to “Ensure a module definition is a
684+ properly initialized Python object that correctly reports its type and
685+ a reference count.”
686+ This would need to change without warning, breaking any user code that treats
687+ ``PyModuleDef ``\ s as Python objects.
688+
689+
654690Possible Future Directions
655691==========================
656692
0 commit comments