Skip to content

Commit 8d37c62

Browse files
markshannonencukouslateny
authored
GH-92678: Document that you shouldn't be doing your own dictionary offset calculations. (GH-95598)
Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: Stanley <46876382+slateny@users.noreply.github.com>
1 parent eb81c1a commit 8d37c62

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
@@ -1715,18 +1715,11 @@ and :c:type:`PyType_Type` effectively act as defaults.)
17151715
:c:member:`~PyTypeObject.tp_dictoffset` should be set to ``-4`` to indicate that the dictionary is
17161716
at the very end of the structure.
17171717

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

17311724
**Inheritance:**
17321725

Doc/whatsnew/3.11.rst

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

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

15481552
Build Changes
15491553
=============

Objects/object.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,11 @@ _PyObject_ComputedDictPointer(PyObject *obj)
10791079

10801080
/* Helper to get a pointer to an object's __dict__ slot, if any.
10811081
* Creates the dict from inline attributes if necessary.
1082-
* Does not set an exception. */
1082+
* Does not set an exception.
1083+
*
1084+
* Note that the tp_dictoffset docs used to recommend this function,
1085+
* so it should be treated as part of the public API.
1086+
*/
10831087
PyObject **
10841088
_PyObject_GetDictPtr(PyObject *obj)
10851089
{

0 commit comments

Comments
 (0)