Skip to content

Commit ffd6473

Browse files
Clarify argument/result ownership/validity for PyModule_* functions (GH-141159)
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
1 parent a7bf27f commit ffd6473

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.
@@ -671,6 +681,9 @@ or code that creates modules dynamically.
671681
:c:type:`PyMethodDef` arrays; in that case they should call this function
672682
directly.
673683
684+
The *functions* array must be statically allocated (or otherwise guaranteed
685+
to outlive the module object).
686+
674687
.. versionadded:: 3.5
675688
676689
.. c:function:: int PyModule_SetDocString(PyObject *module, const char *docstring)

0 commit comments

Comments
 (0)