Skip to content

Commit d9a966a

Browse files
authored
bpo-38787: Clarify docs for PyType_GetModule and warn against common mistake (pythonGH-20215)
1 parent 398575c commit d9a966a

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

Doc/c-api/type.rst

+11-1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ Type Objects
117117
If no module is associated with the given type, sets :py:class:`TypeError`
118118
and returns ``NULL``.
119119
120+
This function is usually used to get the module in which a method is defined.
121+
Note that in such a method, ``PyType_GetModule(Py_TYPE(self))``
122+
may not return the intended result.
123+
``Py_TYPE(self)`` may be a *subclass* of the intended class, and subclasses
124+
are not necessarily defined in the same module as their superclass.
125+
See :c:type:`PyCMethod` to get the class that defines the method.
126+
120127
.. versionadded:: 3.9
121128
122129
.. c:function:: void* PyType_GetModuleState(PyTypeObject *type)
@@ -151,9 +158,12 @@ The following functions and structs are used to create
151158
If *bases* is ``NULL``, the *Py_tp_base* slot is used instead.
152159
If that also is ``NULL``, the new type derives from :class:`object`.
153160
154-
The *module* must be a module object or ``NULL``.
161+
The *module* argument can be used to record the module in which the new
162+
class is defined. It must be a module object or ``NULL``.
155163
If not ``NULL``, the module is associated with the new type and can later be
156164
retreived with :c:func:`PyType_GetModule`.
165+
The associated module is not inherited by subclasses; it must be specified
166+
for each class individually.
157167
158168
This function calls :c:func:`PyType_Ready` on the new type.
159169

0 commit comments

Comments
 (0)