Skip to content

Commit c96b26c

Browse files
miss-islingtonencukouslatenymarkshannon
authored
GH-92678: Document that you shouldn't be doing your own dictionary offset calculations. (GH-95598) (GH-95821)
Co-authored-by: Petr Viktorin <[email protected]> Co-authored-by: Stanley <[email protected]> Co-authored-by: Mark Shannon <[email protected]>
1 parent 2d36d5e commit c96b26c

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

Doc/c-api/object.rst

+18
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ Object Protocol
126126
A generic implementation for the getter of a ``__dict__`` descriptor. It
127127
creates the dictionary if necessary.
128128
129+
This function may also be called to get the :py:attr:`~object.__dict__`
130+
of the object *o*. Pass ``NULL`` for *context* when calling it.
131+
Since this function may need to allocate memory for the
132+
dictionary, it may be more efficient to call :c:func:`PyObject_GetAttr`
133+
when accessing an attribute on the object.
134+
135+
On failure, returns ``NULL`` with an exception set.
136+
129137
.. versionadded:: 3.3
130138
131139
@@ -137,6 +145,16 @@ Object Protocol
137145
.. versionadded:: 3.3
138146
139147
148+
.. c:function:: PyObject** _PyObject_GetDictPtr(PyObject *obj)
149+
150+
Return a pointer to :py:attr:`~object.__dict__` of the object *obj*.
151+
If there is no ``__dict__``, return ``NULL`` without setting an exception.
152+
153+
This function may need to allocate memory for the
154+
dictionary, so it may be more efficient to call :c:func:`PyObject_GetAttr`
155+
when accessing an attribute on the object.
156+
157+
140158
.. c:function:: PyObject* PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid)
141159
142160
Compare the values of *o1* and *o2* using the operation specified by *opid*,

Doc/c-api/typeobj.rst

+5-12
Original file line numberDiff line numberDiff line change
@@ -1709,18 +1709,11 @@ and :c:type:`PyType_Type` effectively act as defaults.)
17091709
:c:member:`~PyTypeObject.tp_dictoffset` should be set to ``-4`` to indicate that the dictionary is
17101710
at the very end of the structure.
17111711

1712-
The real dictionary offset in an instance can be computed from a negative
1713-
:c:member:`~PyTypeObject.tp_dictoffset` as follows::
1714-
1715-
dictoffset = tp_basicsize + abs(ob_size)*tp_itemsize + tp_dictoffset
1716-
if dictoffset is not aligned on sizeof(void*):
1717-
round up to sizeof(void*)
1718-
1719-
where :c:member:`~PyTypeObject.tp_basicsize`, :c:member:`~PyTypeObject.tp_itemsize` and :c:member:`~PyTypeObject.tp_dictoffset` are
1720-
taken from the type object, and :attr:`ob_size` is taken from the instance. The
1721-
absolute value is taken because ints use the sign of :attr:`ob_size` to
1722-
store the sign of the number. (There's never a need to do this calculation
1723-
yourself; it is done for you by :c:func:`_PyObject_GetDictPtr`.)
1712+
The :c:member:`~PyTypeObject.tp_dictoffset` should be regarded as write-only.
1713+
To get the pointer to the dictionary call :c:func:`PyObject_GenericGetDict`.
1714+
Calling :c:func:`PyObject_GenericGetDict` may need to allocate memory for the
1715+
dictionary, so it is may be more efficient to call :c:func:`PyObject_GetAttr`
1716+
when accessing an attribute on the object.
17241717

17251718
**Inheritance:**
17261719

Doc/whatsnew/3.11.rst

+4
Original file line numberDiff line numberDiff line change
@@ -1549,6 +1549,10 @@ Changes in the Python API
15491549
:func:`compile` and other related functions. If invalid positions are detected,
15501550
a :exc:`ValueError` will be raised. (Contributed by Pablo Galindo in :gh:`93351`)
15511551

1552+
* :c:member:`~PyTypeObject.tp_dictoffset` should be treated as write-only.
1553+
It can be set to describe C extension clases to the VM, but should be regarded
1554+
as meaningless when read. To get the pointer to the object's dictionary call
1555+
:c:func:`PyObject_GenericGetDict` instead.
15521556

15531557
Build Changes
15541558
=============

Objects/object.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,11 @@ _PyObject_DictPointer(PyObject *obj)
10861086

10871087
/* Helper to get a pointer to an object's __dict__ slot, if any.
10881088
* Creates the dict from inline attributes if necessary.
1089-
* Does not set an exception. */
1089+
* Does not set an exception.
1090+
*
1091+
* Note that the tp_dictoffset docs used to recommend this function,
1092+
* so it should be treated as part of the public API.
1093+
*/
10901094
PyObject **
10911095
_PyObject_GetDictPtr(PyObject *obj)
10921096
{

0 commit comments

Comments
 (0)