Skip to content

Commit 5dce4ab

Browse files
miss-islingtonencukoukumaraditya303
authored
gh-99249: Clarify "read-only" slots tp_bases & tp_mro (GH-99342)
These slots are marked "should be treated as read-only" in the table at the start of the document. That doesn't say anything about setting them in the static struct. `tp_bases` docs did say that it should be ``NULL`` (TIL!). If you ignore that, seemingly nothing bad happens. However, some slots may not be inherited, depending on which sub-slot structs are present. (FWIW, NumPy sets tp_bases and is affected by the quirk -- though to be fair, its DUAL_INHERIT code probably predates tp_bases docs, and also the result happens to be benign.) This patch makes things explicit. It also makes the summary table legend easier to scan. (cherry picked from commit 219696a) Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
1 parent 6f658dd commit 5dce4ab

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

Doc/c-api/typeobj.rst

+25-6
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,16 @@ Quick Reference
149149
+------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
150150

151151
.. [#slots]
152-
A slot name in parentheses indicates it is (effectively) deprecated.
153-
Names in angle brackets should be treated as read-only.
154-
Names in square brackets are for internal use only.
155-
"<R>" (as a prefix) means the field is required (must be non-``NULL``).
152+
153+
**()**: A slot name in parentheses indicates it is (effectively) deprecated.
154+
155+
**<>**: Names in angle brackets should be initially set to ``NULL`` and
156+
treated as read-only.
157+
158+
**[]**: Names in square brackets are for internal use only.
159+
160+
**<R>** (as a prefix) means the field is required (must be non-``NULL``).
161+
156162
.. [#cols] Columns:
157163
158164
**"O"**: set on :c:type:`PyBaseObject_Type`
@@ -1903,8 +1909,19 @@ and :c:type:`PyType_Type` effectively act as defaults.)
19031909
19041910
Tuple of base types.
19051911

1906-
This is set for types created by a class statement. It should be ``NULL`` for
1907-
statically defined types.
1912+
This field should be set to ``NULL`` and treated as read-only.
1913+
Python will fill it in when the type is :c:func:`initialized <PyType_Ready>`.
1914+
1915+
For dynamically created classes, the ``Py_tp_bases``
1916+
:c:type:`slot <PyType_Slot>` can be used instead of the *bases* argument
1917+
of :c:func:`PyType_FromSpecWithBases`.
1918+
The argument form is preferred.
1919+
1920+
.. warning::
1921+
1922+
Multiple inheritance does not work well for statically defined types.
1923+
If you set ``tp_bases`` to a tuple, Python will not raise an error,
1924+
but some slots will only be inherited from the first base.
19081925

19091926
**Inheritance:**
19101927

@@ -1916,6 +1933,8 @@ and :c:type:`PyType_Type` effectively act as defaults.)
19161933
Tuple containing the expanded set of base types, starting with the type itself
19171934
and ending with :class:`object`, in Method Resolution Order.
19181935

1936+
This field should be set to ``NULL`` and treated as read-only.
1937+
Python will fill it in when the type is :c:func:`initialized <PyType_Ready>`.
19191938

19201939
**Inheritance:**
19211940

0 commit comments

Comments
 (0)