33.. _moduleobjects :
44
55Module Objects
6- --------------
6+ ==============
77
88.. index :: pair: object; module
99
10-
1110.. c :var :: PyTypeObject PyModule_Type
1211
1312 .. index :: single: ModuleType (in module types)
@@ -139,8 +138,9 @@ Module Objects
139138Module definition
140139-----------------
141140
142- Modules defined using the C API are typically defined using an array of *slots *
143- The slots provide a “description" of how a module should be created.
141+ Modules created using the C API are typically defined using an
142+ array of :dfn: `slots `.
143+ The slots provide a "description" of how a module should be created.
144144
145145.. versionchanged :: next
146146
@@ -162,7 +162,7 @@ in an array of slots.
162162
163163 .. c:member:: int slot
164164
165- A slot ID, chosen from the available values explained below.
165+ A slot ID, chosen from the available ``Py_mod_*`` values explained below.
166166
167167 An ID of 0 marks the end of a :c:type:`!PyModuleDef_Slot` array.
168168
@@ -175,11 +175,9 @@ in an array of slots.
175175
176176 .. versionadded:: 3.5
177177
178- The available slot types are:
179-
180178
181- Description slots
182- .................
179+ Metadata slots
180+ ..............
183181
184182.. c:macro:: Py_mod_name
185183
@@ -213,9 +211,6 @@ Feature slots
213211 A pointer to a :c:struct:`PyABIInfo` structure that describes the ABI that
214212 the extension is using.
215213
216- When the module is loaded, the :c:struct:`!PyABIInfo` in this slot is checked
217- using :c:func:`PyABIInfo_Check`.
218-
219214 A suitable :c:struct:`!PyABIInfo` variable can be defined using the
220215 :c:macro:`PyABIInfo_VAR` macro, as in:
221216
@@ -228,6 +223,9 @@ Feature slots
228223 ...
229224 };
230225
226+ When creating a module, Python checks the value of this slot
227+ using :c:func: `PyABIInfo_Check `.
228+
231229 .. versionadded :: 3.15
232230
233231.. c :macro :: Py_mod_multiple_interpreters
@@ -255,9 +253,6 @@ Feature slots
255253 This slot determines whether or not importing this module
256254 in a subinterpreter will fail.
257255
258- Multiple ``Py_mod_multiple_interpreters`` slots may not be specified
259- in one module definition.
260-
261256 If ``Py_mod_multiple_interpreters`` is not specified, the import
262257 machinery defaults to ``Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED``.
263258
@@ -283,8 +278,6 @@ Feature slots
283278 this module will cause the GIL to be automatically enabled. See
284279 :ref:`whatsnew313-free-threaded-cpython` for more detail.
285280
286- Multiple ``Py_mod_gil`` slots may not be specified in one module definition.
287-
288281 If ``Py_mod_gil`` is not specified, the import machinery defaults to
289282 ``Py_MOD_GIL_USED``.
290283
@@ -306,8 +299,8 @@ Creation and initialization slots
306299 The function will be called with:
307300
308301 - *spec *: a ``ModuleSpec ``-like object, meaning that any attributes defined
309- for :py:class: `importlib.machinery.ModuleSpec `
310- have matching semantics. However, any of the attributes may be missing.
302+ for :py:class: `importlib.machinery.ModuleSpec ` have matching semantics.
303+ However, any of the attributes may be missing.
311304 - *def *: ``NULL ``, or the module definition if the module is created from one.
312305
313306 The function should return a new module object, or set an error
@@ -324,21 +317,27 @@ Creation and initialization slots
324317 names through symlinks, all while sharing a single module definition.
325318
326319 There is no requirement for the returned object to be an instance of
327- :c:type: `PyModule_Type `. Any type can be used, as long as it supports
328- setting and getting import-related attributes.
329- However, only ``PyModule_Type `` instances may be returned if the
330- ``PyModuleDef `` has non-``NULL `` ``m_traverse ``, ``m_clear ``,
331- ``m_free ``; non-zero ``m_size ``; or slots other than ``Py_mod_create ``.
320+ :c:type: `PyModule_Type `.
321+ However, some slots may only be used with
322+ :c:type: `!PyModule_Type ` instances; in particular:
323+
324+ - :c:macro: `Py_mod_exec `,
325+ - :ref: `module state slots <ext-module-state-slots >` (``Py_mod_state_* ``),
326+ - :c:macro:`Py_mod_token`.
332327
333328 .. versionchanged:: next
334329
335- The *slots * argument may be ``ModuleSpec ``-like object, rather than
330+ The *slots* argument may be a ``ModuleSpec``-like object, rather than
336331 a true :py:class:`~importlib.machinery.ModuleSpec` instance.
337332 Note that previous versions of CPython did not enforce this.
338333
334+ The *def* argument may now be ``NULL``, since modules are not necessarily
335+ made from definitions.
336+
339337.. c:macro:: Py_mod_exec
340338
341- Specifies a function that is called to *execute * the module.
339+ Specifies a function that is called to :dfn:`execute`, or initialize,
340+ the module.
342341 This is equivalent to executing the code of a Python module: typically,
343342 this function adds classes and constants to the module.
344343 The signature of the function is:
@@ -380,13 +379,63 @@ Creation and initialization slots
380379
381380 Use :c:member:`PyModuleDef.m_methods` instead to support previous versions.
382381
382+ .. _ext-module-state:
383+
384+ Module state
385+ ------------
386+
387+ Extension modules can have *module state* -- a
388+ piece of memory that is allocated on module creation,
389+ and freed when the module object is deallocated.
390+ The module state is specified using :ref:`dedicated slots <ext-module-state-slots>`.
391+
392+ A typical use of module state is storing an exception type -- or indeed *any*
393+ type object defined by the module --
394+
395+ Unlike the module's Python attributes, Python code cannot replace or delete
396+ data stored in module state.
397+
398+ Keeping per-module information in attributes and module state, rather than in
399+ static globals, makes module objects *isolated* and safer for use in
400+ multiple sub-interpreters.
401+ It also helps Python do an orderly clean-up when it shuts down.
402+
403+ Extensions that keep references to Python objects as part of module state must
404+ implement :c:macro:`Py_mod_state_traverse` and :c:macro:`Py_mod_state_clear`
405+ functions to avoid reference leaks.
406+
407+ To retrieve the state from a given module, use the following functions:
408+
409+ .. c:function:: void* PyModule_GetState(PyObject *module)
410+
411+ Return the "state" of the module, that is, a pointer to the block of memory
412+ allocated at module creation time, or ``NULL ``. See
413+ :c:macro: `Py_mod_state_size `.
414+
415+ On error, return ``NULL `` with an exception set.
416+ Use :c:func: `PyErr_Occurred ` to tell this case apart from missing
417+ module state.
418+
419+
420+ .. c :function :: int PyModule_GetStateSize (PyObject *, Py_ssize_t *result)
421+
422+ Set *\* result * to the size of the module's state, as specified using
423+ :c:macro: `Py_mod_state_size ` (or :c:member: `PyModuleDef.m_size `),
424+ and return 0.
425+
426+ On error, set *\*result* to -1, and return -1 with an exception set.
427+
428+ .. versionadded:: next
429+
430+
383431
384432.. _ext-module-state-slots:
385433
386434Slots for defining module state
387435...............................
388436
389- See :ref:`ext-module-state` for an explanation of module state.
437+ The following :c:member:`PyModuleDef_Slot.slot` IDs are available for
438+ defining the module state.
390439
391440.. c:macro:: Py_mod_state_size
392441
@@ -400,7 +449,8 @@ See :ref:`ext-module-state` for an explanation of module state.
400449 module does not support sub-interpreters, because it has global state.
401450 Negative *size* is only allowed when using
402451 :ref:`legacy single-phase initialization <single-phase-initialization>`
403- or when :ref:`creating modules dynamically <module-from-slots>`.
452+ or when creating modules dynamically using :c:func:`PyModule_Create`
453+ or :c:func:`PyModule_Create2`.
404454
405455 See :PEP:`3121` for more details.
406456
@@ -505,51 +555,6 @@ See :ref:`ext-module-state` for an explanation of module state.
505555
506556 .. versionadded :: next
507557
508- .. _ext-module-state :
509-
510- Module state
511- ------------
512-
513- Extension modules can have *module state * -- a
514- piece of memory that is allocated on module creation,
515- and freed when the module object is deallocated.
516- The module state is specified using :ref: `dedicated slots <ext-module-state-slots >`.
517-
518- Unlike the module's Python attributes, Python code cannot replace or delete
519- data stored in module state.
520-
521- Keeping per-module information in attributes and module state, rather than in
522- static globals, makes module objects *isolated * and safer for use in
523- multiple sub-interpreters.
524- It also helps Python do an orderly clean-up when it shuts down.
525-
526- Extensions that keep references to Python objects as part of module state must
527- implement :c:macro: `Py_mod_state_traverse ` and :c:macro: `Py_mod_state_clear `
528- functions to avoid reference leaks.
529-
530- To retrieve the state from a given module, use the following functions:
531-
532- .. c :function :: void * PyModule_GetState (PyObject *module)
533-
534- Return the "state" of the module, that is, a pointer to the block of memory
535- allocated at module creation time, or ``NULL ``. See
536- :c:macro: `Py_mod_state_size `.
537-
538- On error, return ``NULL `` with an exception set.
539- Use :c:func: `PyErr_Occurred ` to tell this case apart from a missing
540- :c:type: `!PyModuleDef `.
541-
542-
543- .. c :function :: int PyModule_GetStateSize (PyObject *, Py_ssize_t *result)
544-
545- Set *\* result * to the size of the module's state, as specified using
546- :c:macro: `Py_mod_state_size ` (or :c:member: `PyModuleDef.m_size `),
547- and return 0.
548-
549- On error, set *\*result* to -1, and return -1 with an exception set.
550-
551- .. versionadded:: next
552-
553558
554559.. _ext-module-token :
555560
0 commit comments