8000 bpo-38787: Clarify docs for PyType_GetModule and warn against common … · python/cpython@d9a966a · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d9a966a

Browse files
authored
bpo-38787: Clarify docs for PyType_GetModule and warn against common mistake (GH-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

Lines changed: 11 additions & 1 deletion
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)
0