Skip to content

Commit 45be8d6

Browse files
committed
Issue #25701: Document C API functions that both set and delete objects
Also document that the separate functions that delete objects are preferred; using PyObject_SetAttr(), _SetAttrString(), and PySequence_SetItem() to delete is deprecated.
1 parent f174689 commit 45be8d6

File tree

4 files changed

+50
-29
lines changed

4 files changed

+50
-29
lines changed

Doc/c-api/object.rst

+19-8
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,35 @@ Object Protocol
6868
.. c:function:: int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v)
6969
7070
Set the value of the attribute named *attr_name*, for object *o*, to the value
71-
*v*. Returns ``-1`` on failure. This is the equivalent of the Python statement
71+
*v*. Raise an exception and return ``-1`` on failure;
72+
return ``0`` on success. This is the equivalent of the Python statement
7273
``o.attr_name = v``.
7374
75+
If *v* is *NULL*, the attribute is deleted, however this feature is
76+
deprecated in favour of using :c:func:`PyObject_DelAttr`.
77+
7478
7579
.. c:function:: int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v)
7680
7781
Set the value of the attribute named *attr_name*, for object *o*, to the value
78-
*v*. Returns ``-1`` on failure. This is the equivalent of the Python statement
82+
*v*. Raise an exception and return ``-1`` on failure;
83+
return ``0`` on success. This is the equivalent of the Python statement
7984
``o.attr_name = v``.
8085
86+
If *v* is *NULL*, the attribute is deleted, however this feature is
87+
deprecated in favour of using :c:func:`PyObject_DelAttrString`.
88+
8189
8290
.. c:function:: int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value)
8391
84-
Generic attribute setter function that is meant to be put into a type
85-
object's ``tp_setattro`` slot. It looks for a data descriptor in the
92+
Generic attribute setter and deleter function that is meant
93+
to be put into a type object's :c:member:`~PyTypeObject.tp_setattro`
94+
slot. It looks for a data descriptor in the
8695
dictionary of classes in the object's MRO, and if found it takes preference
87-
over setting the attribute in the instance dictionary. Otherwise, the
88-
attribute is set in the object's :attr:`~object.__dict__` (if present).
89-
Otherwise, an :exc:`AttributeError` is raised and ``-1`` is returned.
96+
over setting or deleting the attribute in the instance dictionary. Otherwise, the
97+
attribute is set or deleted in the object's :attr:`~object.__dict__` (if present).
98+
On success, ``0`` is returned, otherwise an :exc:`AttributeError`
99+
is raised and ``-1`` is returned.
90100
91101
92102
.. c:function:: int PyObject_DelAttr(PyObject *o, PyObject *attr_name)
@@ -378,7 +388,8 @@ Object Protocol
378388
379389
.. c:function:: int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v)
380390
381-
Map the object *key* to the value *v*. Returns ``-1`` on failure. This is the
391+
Map the object *key* to the value *v*. Raise an exception and
392+
return ``-1`` on failure; return ``0`` on success. This is the
382393
equivalent of the Python statement ``o[key] = v``.
383394
384395

Doc/c-api/sequence.rst

+5-1
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,14 @@ Sequence Protocol
6262
6363
.. c:function:: int PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v)
6464
65-
Assign object *v* to the *i*\ th element of *o*. Returns ``-1`` on failure. This
65+
Assign object *v* to the *i*\ th element of *o*. Raise an exception
66+
and return ``-1`` on failure; return ``0`` on success. This
6667
is the equivalent of the Python statement ``o[i] = v``. This function *does
6768
not* steal a reference to *v*.
6869
70+
If *v* is *NULL*, the element is deleted, however this feature is
71+
deprecated in favour of using :c:func:`PySequence_DelItem`.
72+
6973
7074
.. c:function:: int PySequence_DelItem(PyObject *o, Py_ssize_t i)
7175

Doc/c-api/typeobj.rst

+16-10
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,13 @@ type objects) *must* have the :attr:`ob_size` field.
208208

209209
.. c:member:: setattrfunc PyTypeObject.tp_setattr
210210
211-
An optional pointer to the set-attribute-string function.
211+
An optional pointer to the function for setting and deleting attributes.
212212

213213
This field is deprecated. When it is defined, it should point to a function
214214
that acts the same as the :c:member:`~PyTypeObject.tp_setattro` function, but taking a C string
215215
instead of a Python string object to give the attribute name. The signature is
216-
the same as for :c:func:`PyObject_SetAttrString`.
216+
the same as for :c:func:`PyObject_SetAttrString`, but setting
217+
*v* to *NULL* to delete an attribute must be supported.
217218

218219
This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_setattro`: a subtype
219220
inherits both :c:member:`~PyTypeObject.tp_setattr` and :c:member:`~PyTypeObject.tp_setattro` from its base type when
@@ -351,9 +352,10 @@ type objects) *must* have the :attr:`ob_size` field.
351352

352353
.. c:member:: setattrofunc PyTypeObject.tp_setattro
353354
354-
An optional pointer to the set-attribute function.
355+
An optional pointer to the function for setting and deleting attributes.
355356

356-
The signature is the same as for :c:func:`PyObject_SetAttr`. It is usually
357+
The signature is the same as for :c:func:`PyObject_SetAttr`, but setting
358+
*v* to *NULL* to delete an attribute must be supported. It is usually
357359
convenient to set this field to :c:func:`PyObject_GenericSetAttr`, which
358360
implements the normal way of setting object attributes.
359361

@@ -724,7 +726,7 @@ type objects) *must* have the :attr:`ob_size` field.
724726
typedef struct PyGetSetDef {
725727
char *name; /* attribute name */
726728
getter get; /* C function to get the attribute */
727-
setter set; /* C function to set the attribute */
729+
setter set; /* C function to set or delete the attribute */
728730
char *doc; /* optional doc string */
729731
void *closure; /* optional additional data for getter and setter */
730732
} PyGetSetDef;
@@ -775,12 +777,14 @@ type objects) *must* have the :attr:`ob_size` field.
775777

776778
.. c:member:: descrsetfunc PyTypeObject.tp_descr_set
777779
778-
An optional pointer to a "descriptor set" function.
780+
An optional pointer to a function for setting and deleting
781+
a descriptor's value.
779782

780783
The function signature is ::
781784

782785
int tp_descr_set(PyObject *self, PyObject *obj, PyObject *value);
783786

787+
The *value* argument is set to *NULL* to delete the value.
784788
This field is inherited by subtypes.
785789

786790
.. XXX explain.
@@ -1171,9 +1175,11 @@ Mapping Object Structures
11711175

11721176
.. c:member:: objobjargproc PyMappingMethods.mp_ass_subscript
11731177
1174-
This function is used by :c:func:`PyObject_SetItem` and has the same
1175-
signature. If this slot is *NULL*, the object does not support item
1176-
assignment.
1178+
This function is used by :c:func:`PyObject_SetItem` and
1179+
:c:func:`PyObject_DelItem`. It has the same signature as
1180+
:c:func:`PyObject_SetItem`, but *v* can also be set to *NULL* to delete
1181+
an item. If this slot is *NULL*, the object does not support item
1182+
assignment and deletion.
11771183

11781184

11791185
.. _sequence-structs:
@@ -1222,7 +1228,7 @@ Sequence Object Structures
12221228
12231229
This function is used by :c:func:`PySequence_SetItem` and has the same
12241230
signature. This slot may be left to *NULL* if the object does not support
1225-
item assignment.
1231+
item assignment and deletion.
12261232

12271233
.. c:member:: objobjproc PySequenceMethods.sq_contains
12281234

Include/abstract.h

+10-10
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
192192
int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v);
193193
194194
Set the value of the attribute named attr_name, for object o,
195-
to the value, v. Returns -1 on failure. This is
196-
the equivalent of the Python statement: o.attr_name=v.
195+
to the value v. Raise an exception and return -1 on failure; return 0 on
196+
success. This is the equivalent of the Python statement o.attr_name=v.
197197
198198
*/
199199

@@ -202,8 +202,8 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
202202
int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v);
203203
204204
Set the value of the attribute named attr_name, for object o,
205-
to the value, v. Returns -1 on failure. This is
206-
the equivalent of the Python statement: o.attr_name=v.
205+
to the value v. Raise an exception and return -1 on failure; return 0 on
206+
success. This is the equivalent of the Python statement o.attr_name=v.
207207
208208
*/
209209

@@ -435,9 +435,9 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
435435
PyAPI_FUNC(int) PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v);
436436

437437
/*
438-
Map the object, key, to the value, v. Returns
439-
-1 on failure. This is the equivalent of the Python
440-
statement: o[key]=v.
438+
Map the object key to the value v. Raise an exception and return -1
439+
on failure; return 0 on success. This is the equivalent of the Python
440+
statement o[key]=v.
441441
*/
442442

443443
PyAPI_FUNC(int) PyObject_DelItemString(PyObject *o, const char *key);
@@ -993,9 +993,9 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
993993
PyAPI_FUNC(int) PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v);
994994

995995
/*
996-
Assign object v to the ith element of o. Returns
997-
-1 on failure. This is the equivalent of the Python
998-
statement: o[i]=v.
996+
Assign object v to the ith element of o. Raise an exception and return
997+
-1 on failure; return 0 on success. This is the equivalent of the
998+
Python statement o[i]=v.
999999
*/
10001000

10011001
PyAPI_FUNC(int) PySequence_DelItem(PyObject *o, Py_ssize_t i);

0 commit comments

Comments
 (0)