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 @@ -1715,18 +1715,11 @@ and :c:type:`PyType_Type` effectively act as defaults.)
1715
1715
:c:member: `~PyTypeObject.tp_dictoffset ` should be set to ``-4 `` to indicate that the dictionary is
1716
1716
at the very end of the structure.
1717
1717
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.
1730
1723
1731
1724
**Inheritance: **
1732
1725
Original file line number Diff line number Diff line change @@ -1544,6 +1544,10 @@ Changes in the Python API
1544
1544
:func: `compile ` and other related functions. If invalid positions are detected,
1545
1545
a :exc: `ValueError ` will be raised. (Contributed by Pablo Galindo in :gh: `93351 `)
1546
1546
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.
1547
1551
1548
1552
Build Changes
1549
1553
=============
Original file line number Diff line number Diff line change @@ -1079,7 +1079,11 @@ _PyObject_ComputedDictPointer(PyObject *obj)
1079
1079
1080
1080
/* Helper to get a pointer to an object's __dict__ slot, if any.
1081
1081
* 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
+ */
1083
1087
PyObject * *
1084
1088
_PyObject_GetDictPtr (PyObject * obj )
1085
1089
{
You can’t perform that action at this time.
0 commit comments