Skip to content

Commit 5ae4df1

Browse files
committed
gh-135913: Document ob_refcnt, ob_type, ob_size
1 parent e5f03b9 commit 5ae4df1

2 files changed

Lines changed: 59 additions & 16 deletions

File tree

Doc/c-api/structures.rst

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,49 @@ 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+
.. c:member:: PyTypeObject* ob_type
43+
44+
The object's type.
45+
Do not use this field directly; use :c:macro:`Py_TYPE` and
46+
:c:func:`Py_SET_TYPE` instead.
3447

3548

3649
.. c:type:: PyVarObject
3750
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`.
51+
An extension of :c:type:`PyObject` that adds the
52+
:c:member:`~PyVarObject.ob_size` field.
53+
This is intended for objects that have some notion of *length*.
54+
55+
As with :c:type:`!PyObject`, the members must not be accessed directly;
56+
instead use macros such as :c:macro:`Py_SIZE`, :c:macro:`Py_REFCNT` and
57+
:c:macro:`Py_TYPE`.
58+
59+
.. c:member:: Py_ssize_t ob_size
60+
61+
A size field, whose contents should be considered an object's internal
62+
implementation detail.
63+
64+
Do not use this field directly; use :c:macro:`Py_SIZE` instead.
65+
66+
Object creation functions such as :c:func:`PyObject_NewVar` will
67+
generally set this field to the requested size (number of items).
68+
After creation, arbitrary values can be stored in :c:member:`!ob_size`
69+
using :c:macro:`Py_SET_SIZE`.
70+
71+
To get an object's publicly exposed length, as returned by
72+
the Python function :py:func:`len`, use :c:func:`PyObject_Length`
73+
instead.
4374

4475

4576
.. c:macro:: PyObject_HEAD
@@ -103,9 +134,8 @@ under :ref:`reference counting <countingrefs>`.
103134
104135
Get the type of the Python object *o*.
105136
106-
Return a :term:`borrowed reference`.
107-
108-
Use the :c:func:`Py_SET_TYPE` function to set an object type.
137+
The returned reference is :term:`borrowed <borrowed reference>` from *o*.
138+
Do not release it with :c:func:`Py_DECREF` or similar.
109139
110140
.. versionchanged:: 3.11
111141
:c:func:`Py_TYPE()` is changed to an inline static function.
@@ -122,16 +152,26 @@ under :ref:`reference counting <countingrefs>`.
122152
123153
.. c:function:: void Py_SET_TYPE(PyObject *o, PyTypeObject *type)
124154
125-
Set the object *o* type to *type*.
155+
Set the type of object *o* to *type*, without any checking or reference
156+
counting.
157+
158+
This is a very low-level operation.
159+
Consider instead setting the Python attribute :attr:`~object.__class__`
160+
using :c:func:`PyObject_SetAttrString` or similar.
161+
162+
Note that assigning an incompatible type can lead to undefined behavior.
163+
164+
If *type* is a :ref:`heap type <heap-types>`, the caller must create a
165+
new reference to it.
166+
Similarly, if the old type of *o* is a heap type, the caller must release
167+
a reference to that type.
126168
127169
.. versionadded:: 3.9
128170
129171
130172
.. c:function:: Py_ssize_t Py_SIZE(PyVarObject *o)
131173
132-
Get the size of the Python object *o*.
133-
134-
Use the :c:func:`Py_SET_SIZE` function to set an object size.
174+
Get the :c:member:`~PyVarObject.ob_size` field of *o*.
135175
136176
.. versionchanged:: 3.11
137177
:c:func:`Py_SIZE()` is changed to an inline static function.
@@ -140,7 +180,7 @@ under :ref:`reference counting <countingrefs>`.
140180
141181
.. c:function:: void Py_SET_SIZE(PyVarObject *o, Py_ssize_t size)
142182
143-
Set the object *o* size to *size*.
183+
Set the :c:member:`~PyVarObject.ob_size` field of *o* to *size*.
144184
145185
.. versionadded:: 3.9
146186

Doc/c-api/typeobj.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ type objects) *must* have the :c:member:`~PyVarObject.ob_size` field.
493493

494494

495495
.. c:member:: Py_ssize_t PyObject.ob_refcnt
496+
:noindex:
496497

497498
This is the type object's reference count, initialized to ``1`` by the
498499
``PyObject_HEAD_INIT`` macro. Note that for :ref:`statically allocated type
@@ -507,6 +508,7 @@ type objects) *must* have the :c:member:`~PyVarObject.ob_size` field.
507508

508509

509510
.. c:member:: PyTypeObject* PyObject.ob_type
511+
:noindex:
510512

511513
This is the type's type, in other words its metatype. It is initialized by the
512514
argument to the ``PyObject_HEAD_INIT`` macro, and its value should normally be
@@ -533,6 +535,7 @@ PyVarObject Slots
533535
-----------------
534536

535537
.. c:member:: Py_ssize_t PyVarObject.ob_size
538+
:noindex:
536539

537540
For :ref:`statically allocated type objects <static-types>`, this should be
538541
initialized to zero. For :ref:`dynamically allocated type objects

0 commit comments

Comments
 (0)