Skip to content

Commit 60b13bb

Browse files
encukouAgent-Hellboy
authored andcommitted
pythongh-135913: Document ob_refcnt, ob_type, ob_size (pythonGH-135914)
* pythongh-135913: Document ob_refcnt, ob_type, ob_size In `typeobj.rst`, instead of `:c:member:` it would be better to use `.. c:member::` with a `:no-index:` option, see: See ref. https://www.sphinx-doc.org/en/master/usage/domains/index.html#basic-markup However, `c:member` currently does not support `:no-index:`.
1 parent 828c810 commit 60b13bb

File tree

2 files changed

+64
-22
lines changed

2 files changed

+64
-22
lines changed

Doc/c-api/structures.rst

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,52 @@ under :ref:`reference counting <countingrefs>`.
2828
object. In a normal "release" build, it contains only the object's
2929
reference count and a pointer to the corresponding type object.
3030
Nothing is actually declared to be a :c:type:`PyObject`, but every pointer
31-
to a Python object can be cast to a :c:expr:`PyObject*`. Access to the
32-
members must be done by using the macros :c:macro:`Py_REFCNT` and
33-
:c:macro:`Py_TYPE`.
31+
to a Python object can be cast to a :c:expr:`PyObject*`.
32+
33+
The members must not be accessed directly; instead use macros such as
34+
:c:macro:`Py_REFCNT` and :c:macro:`Py_TYPE`.
35+
36+
.. c:member:: Py_ssize_t ob_refcnt
37+
38+
The object's reference count, as returned by :c:macro:`Py_REFCNT`.
39+
Do not use this field directly; instead use functions and macros such as
40+
:c:macro:`!Py_REFCNT`, :c:func:`Py_INCREF` and :c:func:`Py_DecRef`.
41+
42+
The field type may be different from ``Py_ssize_t``, depending on
43+
build configuration and platform.
44+
45+
.. c:member:: PyTypeObject* ob_type
46+
47+
The object's type.
48+
Do not use this field directly; use :c:macro:`Py_TYPE` and
49+
:c:func:`Py_SET_TYPE` instead.
3450

3551

3652
.. c:type:: PyVarObject
3753
38-
This is an extension of :c:type:`PyObject` that adds the :c:member:`~PyVarObject.ob_size`
39-
field. This is only used for objects that have some notion of *length*.
40-
This type does not often appear in the Python/C API.
41-
Access to the members must be done by using the macros
42-
:c:macro:`Py_REFCNT`, :c:macro:`Py_TYPE`, and :c:macro:`Py_SIZE`.
54+
An extension of :c:type:`PyObject` that adds the
55+
:c:member:`~PyVarObject.ob_size` field.
56+
This is intended for objects that have some notion of *length*.
57+
58+
As with :c:type:`!PyObject`, the members must not be accessed directly;
59+
instead use macros such as :c:macro:`Py_SIZE`, :c:macro:`Py_REFCNT` and
60+
:c:macro:`Py_TYPE`.
61+
62+
.. c:member:: Py_ssize_t ob_size
63+
64+
A size field, whose contents should be considered an object's internal
65+
implementation detail.
66+
67+
Do not use this field directly; use :c:macro:`Py_SIZE` instead.
68+
69+
Object creation functions such as :c:func:`PyObject_NewVar` will
70+
generally set this field to the requested size (number of items).
71+
After creation, arbitrary values can be stored in :c:member:`!ob_size`
72+
using :c:macro:`Py_SET_SIZE`.
73+
74+
To get an object's publicly exposed length, as returned by
75+
the Python function :py:func:`len`, use :c:func:`PyObject_Length`
76+
instead.
4377

4478

4579
.. c:macro:: PyObject_HEAD
@@ -103,9 +137,8 @@ under :ref:`reference counting <countingrefs>`.
103137
104138
Get the type of the Python object *o*.
105139
106-
Return a :term:`borrowed reference`.
107-
108-
Use the :c:func:`Py_SET_TYPE` function to set an object type.
140+
The returned reference is :term:`borrowed <borrowed reference>` from *o*.
141+
Do not release it with :c:func:`Py_DECREF` or similar.
109142
110143
.. versionchanged:: 3.11
111144
:c:func:`Py_TYPE()` is changed to an inline static function.
@@ -122,16 +155,26 @@ under :ref:`reference counting <countingrefs>`.
122155
123156
.. c:function:: void Py_SET_TYPE(PyObject *o, PyTypeObject *type)
124157
125-
Set the object *o* type to *type*.
158+
Set the type of object *o* to *type*, without any checking or reference
159+
counting.
160+
161+
This is a very low-level operation.
162+
Consider instead setting the Python attribute :attr:`~object.__class__`
163+
using :c:func:`PyObject_SetAttrString` or similar.
164+
165+
Note that assigning an incompatible type can lead to undefined behavior.
166+
167+
If *type* is a :ref:`heap type <heap-types>`, the caller must create a
168+
new reference to it.
169+
Similarly, if the old type of *o* is a heap type, the caller must release
170+
a reference to that type.
126171
127172
.. versionadded:: 3.9
128173
129174
130175
.. c:function:: Py_ssize_t Py_SIZE(PyVarObject *o)
131176
132-
Get the size of the Python object *o*.
133-
134-
Use the :c:func:`Py_SET_SIZE` function to set an object size.
177+
Get the :c:member:`~PyVarObject.ob_size` field of *o*.
135178
136179
.. versionchanged:: 3.11
137180
:c:func:`Py_SIZE()` is changed to an inline static function.
@@ -140,7 +183,7 @@ under :ref:`reference counting <countingrefs>`.
140183
141184
.. c:function:: void Py_SET_SIZE(PyVarObject *o, Py_ssize_t size)
142185
143-
Set the object *o* size to *size*.
186+
Set the :c:member:`~PyVarObject.ob_size` field of *o* to *size*.
144187
145188
.. versionadded:: 3.9
146189

Doc/c-api/typeobj.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,9 @@ metatype) initializes :c:member:`~PyTypeObject.tp_itemsize`, which means that it
492492
type objects) *must* have the :c:member:`~PyVarObject.ob_size` field.
493493

494494

495-
.. c:member:: Py_ssize_t PyObject.ob_refcnt
495+
:c:member:`PyObject.ob_refcnt`
496496

497-
This is the type object's reference count, initialized to ``1`` by the
497+
The type object's reference count is initialized to ``1`` by the
498498
``PyObject_HEAD_INIT`` macro. Note that for :ref:`statically allocated type
499499
objects <static-types>`, the type's instances (objects whose :c:member:`~PyObject.ob_type`
500500
points back to the type) do *not* count as references. But for
@@ -506,7 +506,7 @@ type objects) *must* have the :c:member:`~PyVarObject.ob_size` field.
506506
This field is not inherited by subtypes.
507507

508508

509-
.. c:member:: PyTypeObject* PyObject.ob_type
509+
:c:member:`PyObject.ob_type`
510510

511511
This is the type's type, in other words its metatype. It is initialized by the
512512
argument to the ``PyObject_HEAD_INIT`` macro, and its value should normally be
@@ -532,14 +532,13 @@ type objects) *must* have the :c:member:`~PyVarObject.ob_size` field.
532532
PyVarObject Slots
533533
-----------------
534534

535-
.. c:member:: Py_ssize_t PyVarObject.ob_size
535+
:c:member:`PyVarObject.ob_size`
536536

537537
For :ref:`statically allocated type objects <static-types>`, this should be
538538
initialized to zero. For :ref:`dynamically allocated type objects
539539
<heap-types>`, this field has a special internal meaning.
540540

541-
This field should be accessed using the :c:func:`Py_SIZE()` and
542-
:c:func:`Py_SET_SIZE()` macros.
541+
This field should be accessed using the :c:func:`Py_SIZE()` macro.
543542

544543
**Inheritance:**
545544

0 commit comments

Comments
 (0)