@@ -181,7 +181,8 @@ Metadata slots
181181
182182.. c:macro:: Py_mod_name
183183
184- Name for the new module, as a NUL-terminated UTF8-encoded ``const char *``.
184+ :c:type:`Slot ID <PyModuleDef_Slot.slot>` for the name of the new module,
185+ as a NUL-terminated UTF8-encoded ``const char *``.
185186
186187 Note that modules are typically created using a
187188 :py:class:`~importlib.machinery.ModuleSpec`, and when they are, the
@@ -195,8 +196,10 @@ Metadata slots
195196
196197.. c:macro:: Py_mod_doc
197198
198- Docstring for the module, as a NUL-terminated UTF8-encoded ``const char *``.
199- Usually set to a docstring variable created with :c:macro:`PyDoc_STRVAR`.
199+ :c:type:`Slot ID <PyModuleDef_Slot.slot>` for the docstring of the new
200+ module, as a NUL-terminated UTF8-encoded ``const char *``.
201+
202+ Usually it is set to a variable created with :c:macro:`PyDoc_STRVAR`.
200203
201204 .. versionadded:: next
202205
@@ -208,7 +211,8 @@ Feature slots
208211
209212.. c:macro:: Py_mod_abi
210213
211- A pointer to a :c:struct:`PyABIInfo` structure that describes the ABI that
214+ :c:type:`Slot ID <PyModuleDef_Slot.slot>` whose value points to
215+ a :c:struct:`PyABIInfo` structure describing the ABI that
212216 the extension is using.
213217
214218 A suitable :c:struct:`!PyABIInfo` variable can be defined using the
@@ -230,7 +234,7 @@ Feature slots
230234
231235.. c :macro :: Py_mod_multiple_interpreters
232236
233- Specifies one of the following values :
237+ :c:type: ` Slot ID <PyModuleDef_Slot.slot> ` whose value is one of :
234238
235239 .. c :namespace :: NULL
236240
@@ -260,7 +264,7 @@ Feature slots
260264
261265.. c:macro:: Py_mod_gil
262266
263- Specifies one of the following values :
267+ :c:type:`Slot ID <PyModuleDef_Slot.slot>` whose value is one of :
264268
265269 .. c:namespace:: NULL
266270
@@ -289,8 +293,9 @@ Creation and initialization slots
289293
290294.. c:macro:: Py_mod_create
291295
292- Specifies a function that is called to create the module object itself.
293- The *value* pointer of this slot must point to a function of the signature:
296+ :c:type:`Slot ID <PyModuleDef_Slot.slot>` for a function that creates
297+ the module object itself.
298+ The function must have the signature:
294299
295300 .. c:function:: PyObject* create_module(PyObject *spec, PyModuleDef *def)
296301 :no-index-entry:
@@ -336,10 +341,10 @@ Creation and initialization slots
336341
337342.. c:macro:: Py_mod_exec
338343
339- Specifies a function that is called to :dfn:`execute`, or initialize,
340- the module.
341- This is equivalent to executing the code of a Python module: typically,
342- this function adds classes and constants to the module.
344+ :c:type:`Slot ID <PyModuleDef_Slot.slot>` for a function that will
345+ :dfn:`execute`, or initialize, the module.
346+ This function does the equivalent to executing the code of a Python module:
347+ typically, it adds classes and constants to the module.
343348 The signature of the function is:
344349
345350 .. c:function:: int exec_module(PyObject* module)
@@ -363,9 +368,9 @@ Creation and initialization slots
363368
364369.. c:macro:: Py_mod_methods
365370
366- A pointer to a table of module-level functions, as an array of
367- :c:type:`PyMethodDef` values suitable as the *functions* argument to
368- :c:func:`PyModule_AddFunctions`.
371+ :c:type:`Slot ID <PyModuleDef_Slot.slot>` for a table of module-level
372+ functions, as an array of :c:type:`PyMethodDef` values suitable as the
373+ *functions* argument to :c:func:`PyModule_AddFunctions`.
369374
370375 Like other slot IDs, a slots array may only contain one
371376 :c:macro:`!Py_mod_methods` entry.
@@ -439,19 +444,13 @@ defining the module state.
439444
440445.. c:macro:: Py_mod_state_size
441446
442- Size of the module state, cast to a ``void*`` pointer.
447+ :c:type:`Slot ID <PyModuleDef_Slot.slot>` for the size of the module state,
448+ in bytes.
443449
444- Setting this to a non-negative value means that the module can be
450+ Setting the value to a non-negative value means that the module can be
445451 re-initialized and specifies the additional amount of memory it requires
446452 for its state.
447453
448- Setting the size to ``-1`` means that the
449- module does not support sub-interpreters, because it has global state.
450- Negative *size* is only allowed when using
451- :ref:`legacy single-phase initialization <single-phase-initialization>`
452- or when creating modules dynamically using :c:func:`PyModule_Create`
453- or :c:func:`PyModule_Create2`.
454-
455454 See :PEP:`3121` for more details.
456455
457456 Use :c:func:`PyModule_GetStateSize` to retrieve the size of a given module.
@@ -462,7 +461,8 @@ defining the module state.
462461
463462.. c:macro:: Py_mod_state_traverse
464463
465- A traversal function to call during GC traversal of the module object.
464+ :c:type:`Slot ID <PyModuleDef_Slot.slot>` for a traversal function to call
465+ during GC traversal of the module object.
466466
467467 The signature of the function, and meanings of the arguments,
468468 is similar as for :c:member:`PyTypeObject.tp_traverse`:
@@ -484,7 +484,8 @@ defining the module state.
484484
485485.. c:macro:: Py_mod_state_clear
486486
487- A clear function to call during GC clearing of the module object.
487+ :c:type:`Slot ID <PyModuleDef_Slot.slot>` for a clear function to call
488+ during GC clearing of the module object.
488489
489490 The signature of the function is:
490491
@@ -511,7 +512,8 @@ defining the module state.
511512
512513.. c:macro:: Py_mod_state_free
513514
514- A function to call during deallocation of the module object.
515+ :c:type:`Slot ID <PyModuleDef_Slot.slot>` for a function to call during
516+ deallocation of the module object.
515517
516518 The signature of the function is:
517519
@@ -530,38 +532,13 @@ defining the module state.
530532
531533 Use :c:member:`PyModuleDef.m_free` instead to support previous versions.
532534
533- .. c:macro:: Py_mod_token
534-
535- A pointer intended to identify of the module state's memory layout.
536- See the :ref:`ext-module-token` section for details.
537-
538- This slot is not needed for typical extensions; it exists for specialized
539- use cases -- namely, dynamically created modules.
540- If you use the slot, you must ensure that:
541-
542- * The pointer outlives the class, so it's not reused for something else
543- while the class exists.
544- * It "belongs" to the extension module where the class lives, so it will not
545- clash with other extensions.
546- (Preventing clashes with other modules defined in the same extension is
547- the extension's responsibility; you don't need to use the token for this.)
548- * If the token points to a :c:type:`PyModuleDef` struct, the module should
549- behave as if it was created from that :c:type:`PyModuleDef`.
550- In particular, the module state must have matching layout and semantics.
551-
552- Modules created from :c:type:`PyModuleDef` allways use the address of
553- the :c:type:`PyModuleDef` as the token; :c:macro:`!Py_mod_token` cannot
554- be used in :c:member: `PyModuleDef.m_slots `.
555-
556- .. versionadded :: next
557-
558535
559536.. _ext-module-token:
560537
561538Module token
562539............
563540
564- Each module has an associated *token *, a pointer-sized value intended to
541+ Each module may have an associated *token*: a pointer-sized value intended to
565542identify of the module state's memory layout.
566543This means that if you have a module object, but you are not sure if it
567544“belongs” to your extension, you can check using code like this:
@@ -575,32 +552,48 @@ This means that if you have a module object, but you are not sure if it
575552 return NULL;
576553 }
577554 if (module_token != your_token) {
578- if (!PyErr_Occurred()) {
579- PyErr_SetString(PyExc_ValueError, "unexpected module")
580- }
555+ PyErr_SetString (PyExc_ValueError, "unexpected module")
581556 return NULL;
582557 }
583558
584- // Now it's safe to cast: this module's state has the expected memory layout
559+ // This module's state has the expected memory layout; it's safe to cast
585560 struct my_state state = (struct my_state*)PyModule_GetState(module)
586- if (state == NULL) {
587- return NULL;
588- }
589561
590562A module's token -- and the *your_token* value to use in the above code -- is:
591563
564+ - For modules created with :c:type:`PyModuleDef`: the address of that
565+ :c:type:`PyModuleDef`;
592566 - For modules defined with the :c:macro: `Py_mod_token ` slot: the value
593567 of that slot;
594568- For modules created from an ``PyModExport_* ``
595569 :ref: `export hook <extension-export-hook >`: the slots array that the export
596570 hook returned (unless overriden with :c:macro: `Py_mod_token `).
597- - For modules created with :c:type:`PyModuleDef`: the address of that
598- :c:type:`PyModuleDef`.
571+
572+ .. c:macro:: Py_mod_token
573+
574+ :c:type:`Slot ID <PyModuleDef_Slot.slot>` for the module token.
575+
576+ If you use this slot to set the module token (rather than rely on the
577+ default), you must ensure that:
578+
579+ * The pointer outlives the class, so it's not reused for something else
580+ while the class exists.
581+ * It "belongs" to the extension module where the class lives, so it will not
582+ clash with other extensions.
583+ * If the token points to a :c:type:`PyModuleDef` struct, the module should
584+ behave as if it was created from that :c:type:`PyModuleDef`.
585+ In particular, the module state must have matching layout and semantics.
586+
587+ Modules created from :c:type:`PyModuleDef` allways use the address of
588+ the :c:type:`PyModuleDef` as the token.
589+ This means that :c:macro:`!Py_mod_token` cannot be used in
590+ :c:member:`PyModuleDef.m_slots`.
591+
592+ .. versionadded:: next
599593
600594.. c:function:: int PyModule_GetToken(PyObject *module, void* * result)
601595
602596 Set *\*result* to the module's token and return 0.
603- See :c:macro:`Py_mod_token` for information about module tokens.
604597
605598 On error, set *\*result* to NULL, and return -1 with an exception set.
606599
@@ -638,24 +631,25 @@ rather than from an extension's :ref:`export hook <extension-export-hook>`.
638631 (:c:data: `Py_mod_exec `).
639632 Both :c:func:`!PyModule_FromSlotsAndSpec` and :c:func:`PyModule_Exec`
640633 must be called to fully initialize a module.
641- (Python's default import machinery takes additional steps between
642- these two calls, such as inserting the module object in
643- :py:attr: `sys.modules ` and setting
644- :ref: `import-related attributes <import-mod-attrs >`.)
634+ (See also :ref: `multi-phase-initialization `.)
645635
646636 The *slots* array only needs to be valid for the duration of the
647637 :c:func:`!PyModule_FromSlotsAndSpec` call.
648638 In particular, it may be heap-allocated.
649639
650640 .. versionadded:: next
651641
652- .. c:function:: int PyModule_Exec(PyObject *)
642+ .. c:function:: int PyModule_Exec(PyObject *module )
653643
654- Execute the :c:data: `Py_mod_exec ` slot(s) of the given module.
644+ Execute the :c:data: `Py_mod_exec ` slot(s) of the given * module* .
655645
656646 On success, return 0.
657647 On error, return -1 with an exception set.
658648
649+ For clarity: If *module* has no slots, for example if it uses
650+ :ref:`legacy single-phase initialization <single-phase-initialization>`,
651+ this function does nothing and returns 0.
652+
659653 .. versionadded:: next
660654
661655
@@ -699,25 +693,38 @@ remove it.
699693 The required initial value for :c:member:`!PyModuleDef.m_base`.
700694
701695 .. c:member:: const char *m_name
702- const char *m_doc
703- Py_ssize_t m_size
704- PyMethodDef *m_methods
705696
706- These members correspond to the :c:macro:`Py_mod_name`,
707- :c:macro:`Py_mod_doc`, :c:macro:`Py_mod_state_size`,
708- and :c:macro:`Py_mod_methods` slots, respectively.
697+ Corresponds to the :c:macro:`Py_mod_name` slot.
709698
710- Setting these members to NULL is equivalent to omitting the
711- corresponding slots.
699+ .. c:member:: const char *m_doc
700+
701+ These members correspond to the :c:macro:`Py_mod_doc` slot.
702+ Setting this to NULL is equivalent to omitting the slot.
703+
704+ .. c:member:: Py_ssize_t m_size
705+
706+ Corresponds to the :c:macro:`Py_mod_state_size` slot.
707+ Setting this to zero is equivalent to omitting the slot.
708+
709+ When using :ref:`legacy single-phase initialization <single-phase-initialization>`
710+ or when creating modules dynamically using :c:func:`PyModule_Create`
711+ or :c:func:`PyModule_Create2`, :c:member:`!m_size` may be set to -1.
712+ This indicates that the module does not support sub-interpreters,
713+ because it has global state.
714+
715+ .. c:member:: PyMethodDef *m_methods
716+
717+ Corresponds to the :c:macro:`Py_mod_methods` slot.
718+ Setting this to NULL is equivalent to omitting the slot.
712719
713720 .. c:member:: PyModuleDef_Slot* m_slots
714721
715722 An array of additional slots, terminated by a ``{0, NULL}`` entry.
716723
717724 This array may not contain slots corresponding to :c:type:`PyModuleDef`
718725 members.
719- For example, you cannot use a :c:macro:`Py_mod_name` slot; the module
720- name must be given as :c:member:`PyModuleDef.m_name`.
726+ For example, you cannot use :c:macro:`Py_mod_name` in :c:member:`!m_slots`;
727+ the module name must be given as :c:member:`PyModuleDef.m_name`.
721728
722729 .. versionchanged:: 3.5
723730
@@ -726,13 +733,6 @@ remove it.
726733
727734 .. c:member:: inquiry m_reload
728735
729- const char *m_doc
730- Py_ssize_t m_size
731- PyMethodDef* m_methods
732- traverseproc m_traverse
733- inquiry m_clear
734- freefunc m_free
735-
736736 .. c :member :: traverseproc m_traverse
737737 inquiry m_clear
738738 freefunc m_free
@@ -746,7 +746,7 @@ remove it.
746746
747747 .. versionchanged :: 3.9
748748
749- :c:member: `! m_traverse `, :c:member: `m_clear ` and :c:member: `m_free `
749+ :c:member: `m_traverse `, :c:member: `m_clear ` and :c:member: `m_free `
750750 functions are longer called before the module state is allocated.
751751
752752
0 commit comments