Skip to content

Commit f2e353b

Browse files
[3.12] gh-110803: Reorganize docs for what PyType_Slot doesn't cover (GH-110813) (#110823)
gh-110803: Reorganize docs for what PyType_Slot doesn't cover (GH-110813) * gh-110803: Reorganize docs for what PyType_Slot doesn't cover - Cover the offset fields first - Mention the old alternative for MANAGED flags, which is needed to support older Pythons - De-emphasize the internal flags: use an inline list. - Add a note to PyMemberDef saying what to do with it * Remove an older draft... (cherry picked from commit 2ab34f0) Co-authored-by: Petr Viktorin <[email protected]>
1 parent 08242cd commit f2e353b

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

Doc/c-api/structures.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,11 @@ Accessing attributes of extension types
406406
.. c:type:: PyMemberDef
407407
408408
Structure which describes an attribute of a type which corresponds to a C
409-
struct member. Its fields are, in order:
409+
struct member.
410+
When defining a class, put a NULL-terminated array of these
411+
structures in the :c:member:`~PyTypeObject.tp_members` slot.
412+
413+
Its fields are, in order:
410414
411415
.. c:member:: const char* name
412416

Doc/c-api/type.rst

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -461,21 +461,34 @@ The following functions and structs are used to create
461461
* ``Py_nb_add`` to set :c:member:`PyNumberMethods.nb_add`
462462
* ``Py_sq_length`` to set :c:member:`PySequenceMethods.sq_length`
463463
464-
The following fields cannot be set at all using :c:type:`PyType_Spec` and
465-
:c:type:`PyType_Slot`:
466-
467-
* :c:member:`~PyTypeObject.tp_dict`
468-
* :c:member:`~PyTypeObject.tp_mro`
469-
* :c:member:`~PyTypeObject.tp_cache`
470-
* :c:member:`~PyTypeObject.tp_subclasses`
471-
* :c:member:`~PyTypeObject.tp_weaklist`
464+
The following “offset” fields cannot be set using :c:type:`PyType_Slot`:
465+
466+
* :c:member:`~PyTypeObject.tp_weaklistoffset`
467+
(use :c:macro:`Py_TPFLAGS_MANAGED_WEAKREF` instead if possible)
468+
* :c:member:`~PyTypeObject.tp_dictoffset`
469+
(use :c:macro:`Py_TPFLAGS_MANAGED_DICT` instead if possible)
470+
* :c:member:`~PyTypeObject.tp_vectorcall_offset`
471+
(use ``"__vectorcalloffset__"`` in
472+
:ref:`PyMemberDef <pymemberdef-offsets>`)
473+
474+
If it is not possible to switch to a ``MANAGED`` flag (for example,
475+
for vectorcall or to support Python older than 3.12), specify the
476+
offset in :c:member:`Py_tp_members <PyTypeObject.tp_members>`.
477+
See :ref:`PyMemberDef documentation <pymemberdef-offsets>`
478+
for details.
479+
480+
The following fields cannot be set at all when creating a heap type:
481+
472482
* :c:member:`~PyTypeObject.tp_vectorcall`
473-
* :c:member:`~PyTypeObject.tp_weaklistoffset`
474-
(use :c:macro:`Py_TPFLAGS_MANAGED_WEAKREF` instead)
475-
* :c:member:`~PyTypeObject.tp_dictoffset`
476-
(use :c:macro:`Py_TPFLAGS_MANAGED_DICT` instead)
477-
* :c:member:`~PyTypeObject.tp_vectorcall_offset`
478-
(see :ref:`PyMemberDef <pymemberdef-offsets>`)
483+
(use :c:member:`~PyTypeObject.tp_new` and/or
484+
:c:member:`~PyTypeObject.tp_init`)
485+
486+
* Internal fields:
487+
:c:member:`~PyTypeObject.tp_dict`,
488+
:c:member:`~PyTypeObject.tp_mro`,
489+
:c:member:`~PyTypeObject.tp_cache`,
490+
:c:member:`~PyTypeObject.tp_subclasses`, and
491+
:c:member:`~PyTypeObject.tp_weaklist`.
479492
480493
Setting :c:data:`Py_tp_bases` or :c:data:`Py_tp_base` may be
481494
problematic on some platforms.

0 commit comments

Comments
 (0)