File tree 4 files changed +32
-13
lines changed
4 files changed +32
-13
lines changed Original file line number Diff line number Diff line change @@ -126,6 +126,14 @@ Object Protocol
126
126
A generic implementation for the getter of a ``__dict__ `` descriptor. It
127
127
creates the dictionary if necessary.
128
128
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
+
129
137
.. versionadded :: 3.3
130
138
131
139
@@ -137,6 +145,16 @@ Object Protocol
137
145
.. versionadded :: 3.3
138
146
139
147
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
+
140
158
.. c :function :: PyObject* PyObject_RichCompare (PyObject *o1, PyObject *o2, int opid)
141
159
142
160
Compare the values of *o1 * and *o2 * using the operation specified by *opid *,
Original file line number Diff line number Diff line change @@ -1709,18 +1709,11 @@ and :c:type:`PyType_Type` effectively act as defaults.)
1709
1709
:c:member: `~PyTypeObject.tp_dictoffset ` should be set to ``-4 `` to indicate that the dictionary is
1710
1710
at the very end of the structure.
1711
1711
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.
1724
1717
1725
1718
**Inheritance: **
1726
1719
Original file line number Diff line number Diff line change @@ -1549,6 +1549,10 @@ Changes in the Python API
1549
1549
:func: `compile ` and other related functions. If invalid positions are detected,
1550
1550
a :exc: `ValueError ` will be raised. (Contributed by Pablo Galindo in :gh: `93351 `)
1551
1551
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.
1552
1556
1553
1557
Build Changes
1554
1558
=============
Original file line number Diff line number Diff line change @@ -1086,7 +1086,11 @@ _PyObject_DictPointer(PyObject *obj)
1086
1086
1087
1087
/* Helper to get a pointer to an object's __dict__ slot, if any.
1088
1088
* 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
+ */
1090
1094
PyObject * *
1091
1095
_PyObject_GetDictPtr (PyObject * obj )
1092
1096
{
You can’t perform that action at this time.
0 commit comments