Skip to content

Commit 6384c79

Browse files
[3.13] Clarify argument/result ownership/validity for PyModule_* functions (GH-141159) (GH-141191)
(cherry picked from commit ffd6473) Co-authored-by: Peter Bierma <[email protected]>
1 parent cab85ce commit 6384c79

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

Doc/c-api/module.rst

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Module Objects
1313
.. index:: single: ModuleType (in module types)
1414

1515
This instance of :c:type:`PyTypeObject` represents the Python module type. This
16-
is exposed to Python programs as ``types.ModuleType``.
16+
is exposed to Python programs as :py:class:`types.ModuleType`.
1717

1818

1919
.. c:function:: int PyModule_Check(PyObject *p)
@@ -71,6 +71,9 @@ Module Objects
7171
``PyObject_*`` functions rather than directly manipulate a module's
7272
:attr:`~object.__dict__`.
7373
74+
The returned reference is borrowed from the module; it is valid until
75+
the module is destroyed.
76+
7477
7578
.. c:function:: PyObject* PyModule_GetNameObject(PyObject *module)
7679
@@ -90,6 +93,10 @@ Module Objects
9093
Similar to :c:func:`PyModule_GetNameObject` but return the name encoded to
9194
``'utf-8'``.
9295
96+
The returned buffer is only valid until the module is renamed or destroyed.
97+
Note that Python code may rename a module by setting its :py:attr:`~module.__name__`
98+
attribute.
99+
93100
.. c:function:: void* PyModule_GetState(PyObject *module)
94101
95102
Return the "state" of the module, that is, a pointer to the block of memory
@@ -126,6 +133,9 @@ Module Objects
126133
Similar to :c:func:`PyModule_GetFilenameObject` but return the filename
127134
encoded to 'utf-8'.
128135
136+
The returned buffer is only valid until the module's :py:attr:`~module.__file__` attribute
137+
is reassigned or the module is destroyed.
138+
129139
.. deprecated:: 3.2
130140
:c:func:`PyModule_GetFilename` raises :exc:`UnicodeEncodeError` on
131141
unencodable filenames, use :c:func:`PyModule_GetFilenameObject` instead.
@@ -528,6 +538,9 @@ objects dynamically. Note that both ``PyModule_FromDefAndSpec`` and
528538
``PyModuleDef``, using either ``PyModule_Create`` or
529539
``PyModule_FromDefAndSpec``.
530540
541+
The *functions* array must be statically allocated (or otherwise guaranteed
542+
to outlive the module object).
543+
531544
.. versionadded:: 3.5
532545
533546
Support functions

0 commit comments

Comments
 (0)