Skip to content

Commit 93e9fb5

Browse files
[3.6] bpo-5945: Improve mappings and sequences C API docs. (GH-7029). (GH-7049)
(cherry picked from commit f5b1183)
1 parent 1b48b9c commit 93e9fb5

File tree

4 files changed

+93
-68
lines changed

4 files changed

+93
-68
lines changed

Doc/c-api/mapping.rst

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,67 @@
55
Mapping Protocol
66
================
77

8+
See also :c:func:`PyObject_GetItem`, :c:func:`PyObject_SetItem` and
9+
:c:func:`PyObject_DelItem`.
10+
811

912
.. c:function:: int PyMapping_Check(PyObject *o)
1013
11-
Return ``1`` if the object provides mapping protocol, and ``0`` otherwise. This
12-
function always succeeds.
14+
Return ``1`` if the object provides mapping protocol or supports slicing,
15+
and ``0`` otherwise. Note that it returns ``1`` for Python classes with
16+
a :meth:`__getitem__` method since in general case it is impossible to
17+
determine what the type of keys it supports. This function always
18+
succeeds.
1319
1420
1521
.. c:function:: Py_ssize_t PyMapping_Size(PyObject *o)
1622
Py_ssize_t PyMapping_Length(PyObject *o)
1723
1824
.. index:: builtin: len
1925
20-
Returns the number of keys in object *o* on success, and ``-1`` on failure. For
21-
objects that do not provide mapping protocol, this is equivalent to the Python
22-
expression ``len(o)``.
26+
Returns the number of keys in object *o* on success, and ``-1`` on failure.
27+
This is equivalent to the Python expression ``len(o)``.
2328
2429
25-
.. c:function:: int PyMapping_DelItemString(PyObject *o, const char *key)
30+
.. c:function:: PyObject* PyMapping_GetItemString(PyObject *o, const char *key)
31+
32+
Return element of *o* corresponding to the string *key* or *NULL* on failure.
33+
This is the equivalent of the Python expression ``o[key]``.
34+
See also :c:func:`PyObject_GetItem`.
35+
2636
27-
Remove the mapping for object *key* from the object *o*. Return ``-1`` on
28-
failure. This is equivalent to the Python statement ``del o[key]``.
37+
.. c:function:: int PyMapping_SetItemString(PyObject *o, const char *key, PyObject *v)
38+
39+
Map the string *key* to the value *v* in object *o*. Returns ``-1`` on
40+
failure. This is the equivalent of the Python statement ``o[key] = v``.
41+
See also :c:func:`PyObject_SetItem`.
2942
3043
3144
.. c:function:: int PyMapping_DelItem(PyObject *o, PyObject *key)
3245
33-
Remove the mapping for object *key* from the object *o*. Return ``-1`` on
34-
failure. This is equivalent to the Python statement ``del o[key]``.
46+
Remove the mapping for the object *key* from the object *o*. Return ``-1``
47+
on failure. This is equivalent to the Python statement ``del o[key]``.
48+
This is an alias of :c:func:`PyObject_DelItem`.
3549
3650
37-
.. c:function:: int PyMapping_HasKeyString(PyObject *o, const char *key)
51+
.. c:function:: int PyMapping_DelItemString(PyObject *o, const char *key)
3852
39-
On success, return ``1`` if the mapping object has the key *key* and ``0``
40-
otherwise. This is equivalent to the Python expression ``key in o``.
41-
This function always succeeds.
53+
Remove the mapping for the string *key* from the object *o*. Return ``-1``
54+
on failure. This is equivalent to the Python statement ``del o[key]``.
4255
4356
4457
.. c:function:: int PyMapping_HasKey(PyObject *o, PyObject *key)
4558
46-
Return ``1`` if the mapping object has the key *key* and ``0`` otherwise. This
47-
is equivalent to the Python expression ``key in o``. This function always
48-
succeeds.
59+
Return ``1`` if the mapping object has the key *key* and ``0`` otherwise.
60+
This is equivalent to the Python expression ``key in o``.
61+
This function always succeeds.
62+
63+
64+
.. c:function:: int PyMapping_HasKeyString(PyObject *o, const char *key)
65+
66+
Return ``1`` if the mapping object has the key *key* and ``0`` otherwise.
67+
This is equivalent to the Python expression ``key in o``.
68+
This function always succeeds.
4969
5070
5171
.. c:function:: PyObject* PyMapping_Keys(PyObject *o)
@@ -64,15 +84,3 @@ Mapping Protocol
6484
6585
On success, return a list or tuple of the items in object *o*, where each item
6686
is a tuple containing a key-value pair. On failure, return *NULL*.
67-
68-
69-
.. c:function:: PyObject* PyMapping_GetItemString(PyObject *o, const char *key)
70-
71-
Return element of *o* corresponding to the object *key* or *NULL* on failure.
72-
This is the equivalent of the Python expression ``o[key]``.
73-
74-
75-
.. c:function:: int PyMapping_SetItemString(PyObject *o, const char *key, PyObject *v)
76-
77-
Map the object *key* to the value *v* in object *o*. Returns ``-1`` on failure.
78-
This is the equivalent of the Python statement ``o[key] = v``.

Doc/c-api/object.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,8 @@ Object Protocol
360360
parameters must be non-*NULL*.
361361
362362
363-
.. c:function:: Py_ssize_t PyObject_Length(PyObject *o)
364-
Py_ssize_t PyObject_Size(PyObject *o)
363+
.. c:function:: Py_ssize_t PyObject_Size(PyObject *o)
364+
Py_ssize_t PyObject_Length(PyObject *o)
365365
366366
.. index:: builtin: len
367367
@@ -395,8 +395,8 @@ Object Protocol
395395
396396
.. c:function:: int PyObject_DelItem(PyObject *o, PyObject *key)
397397
398-
Delete the mapping for *key* from *o*. Returns ``-1`` on failure. This is the
399-
equivalent of the Python statement ``del o[key]``.
398+
Remove the mapping for the object *key* from the object *o*. Return ``-1``
399+
on failure. This is equivalent to the Python statement ``del o[key]``.
400400
401401
402402
.. c:function:: PyObject* PyObject_Dir(PyObject *o)

Doc/c-api/sequence.rst

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ Sequence Protocol
99
.. c:function:: int PySequence_Check(PyObject *o)
1010
1111
Return ``1`` if the object provides sequence protocol, and ``0`` otherwise.
12-
This function always succeeds.
12+
Note that it returns ``1`` for Python classes with a :meth:`__getitem__`
13+
method unless they are :class:`dict` subclasses since in general case it
14+
is impossible to determine what the type of keys it supports. This
15+
function always succeeds.
1316
1417
1518
.. c:function:: Py_ssize_t PySequence_Size(PyObject *o)
@@ -119,18 +122,27 @@ Sequence Protocol
119122
120123
.. index:: builtin: tuple
121124
122-
Return a tuple object with the same contents as the arbitrary sequence *o* or
123-
*NULL* on failure. If *o* is a tuple, a new reference will be returned,
125+
Return a tuple object with the same contents as the sequence or iterable *o*,
126+
or *NULL* on failure. If *o* is a tuple, a new reference will be returned,
124127
otherwise a tuple will be constructed with the appropriate contents. This is
125128
equivalent to the Python expression ``tuple(o)``.
126129
127130
128131
.. c:function:: PyObject* PySequence_Fast(PyObject *o, const char *m)
129132
130-
Return the sequence *o* as a list, unless it is already a tuple or list, in
133+
Return the sequence or iterable *o* as a list, unless it is already a tuple or list, in
131134
which case *o* is returned. Use :c:func:`PySequence_Fast_GET_ITEM` to access
132135
the members of the result. Returns *NULL* on failure. If the object is not
133-
a sequence, raises :exc:`TypeError` with *m* as the message text.
136+
a sequence or iterable, raises :exc:`TypeError` with *m* as the message text.
137+
138+
139+
.. c:function:: Py_ssize_t PySequence_Fast_GET_SIZE(PyObject *o)
140+
141+
Returns the length of *o*, assuming that *o* was returned by
142+
:c:func:`PySequence_Fast` and that *o* is not *NULL*. The size can also be
143+
gotten by calling :c:func:`PySequence_Size` on *o*, but
144+
:c:func:`PySequence_Fast_GET_SIZE` is faster because it can assume *o* is a list
145+
or tuple.
134146
135147
136148
.. c:function:: PyObject* PySequence_Fast_GET_ITEM(PyObject *o, Py_ssize_t i)
@@ -155,12 +167,3 @@ Sequence Protocol
155167
:c:func:`PySequence_GetItem` but without checking that
156168
:c:func:`PySequence_Check` on *o* is true and without adjustment for negative
157169
indices.
158-
159-
160-
.. c:function:: Py_ssize_t PySequence_Fast_GET_SIZE(PyObject *o)
161-
162-
Returns the length of *o*, assuming that *o* was returned by
163-
:c:func:`PySequence_Fast` and that *o* is not *NULL*. The size can also be
164-
gotten by calling :c:func:`PySequence_Size` on *o*, but
165-
:c:func:`PySequence_Fast_GET_SIZE` is faster because it can assume *o* is a list
166-
or tuple.

Doc/c-api/typeobj.rst

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,21 +1151,24 @@ Mapping Object Structures
11511151

11521152
.. c:member:: lenfunc PyMappingMethods.mp_length
11531153
1154-
This function is used by :c:func:`PyMapping_Length` and
1154+
This function is used by :c:func:`PyMapping_Size` and
11551155
:c:func:`PyObject_Size`, and has the same signature. This slot may be set to
11561156
*NULL* if the object has no defined length.
11571157

11581158
.. c:member:: binaryfunc PyMappingMethods.mp_subscript
11591159
1160-
This function is used by :c:func:`PyObject_GetItem` and has the same
1161-
signature. This slot must be filled for the :c:func:`PyMapping_Check`
1162-
function to return ``1``, it can be *NULL* otherwise.
1160+
This function is used by :c:func:`PyObject_GetItem` and
1161+
:c:func:`PySequence_GetSlice`, and has the same signature as
1162+
:c:func:`!PyObject_GetItem`. This slot must be filled for the
1163+
:c:func:`PyMapping_Check` function to return ``1``, it can be *NULL*
1164+
otherwise.
11631165

11641166
.. c:member:: objobjargproc PyMappingMethods.mp_ass_subscript
11651167
1166-
This function is used by :c:func:`PyObject_SetItem` and
1167-
:c:func:`PyObject_DelItem`. It has the same signature as
1168-
:c:func:`PyObject_SetItem`, but *v* can also be set to *NULL* to delete
1168+
This function is used by :c:func:`PyObject_SetItem`,
1169+
:c:func:`PyObject_DelItem`, :c:func:`PyObject_SetSlice` and
1170+
:c:func:`PyObject_DelSlice`. It has the same signature as
1171+
:c:func:`!PyObject_SetItem`, but *v* can also be set to *NULL* to delete
11691172
an item. If this slot is *NULL*, the object does not support item
11701173
assignment and deletion.
11711174

@@ -1185,26 +1188,29 @@ Sequence Object Structures
11851188

11861189
.. c:member:: lenfunc PySequenceMethods.sq_length
11871190
1188-
This function is used by :c:func:`PySequence_Size` and :c:func:`PyObject_Size`,
1189-
and has the same signature.
1191+
This function is used by :c:func:`PySequence_Size` and
1192+
:c:func:`PyObject_Size`, and has the same signature. It is also used for
1193+
handling negative indices via the :c:member:`~PySequenceMethods.sq_item`
1194+
and the :c:member:`~PySequenceMethods.sq_ass_item` slots.
11901195

11911196
.. c:member:: binaryfunc PySequenceMethods.sq_concat
11921197
11931198
This function is used by :c:func:`PySequence_Concat` and has the same
11941199
signature. It is also used by the ``+`` operator, after trying the numeric
1195-
addition via the :c:member:`~PyTypeObject.tp_as_number.nb_add` slot.
1200+
addition via the :c:member:`~PyNumberMethods.nb_add` slot.
11961201

11971202
.. c:member:: ssizeargfunc PySequenceMethods.sq_repeat
11981203
11991204
This function is used by :c:func:`PySequence_Repeat` and has the same
12001205
signature. It is also used by the ``*`` operator, after trying numeric
1201-
multiplication via the :c:member:`~PyTypeObject.tp_as_number.nb_multiply`
1202-
slot.
1206+
multiplication via the :c:member:`~PyNumberMethods.nb_multiply` slot.
12031207

12041208
.. c:member:: ssizeargfunc PySequenceMethods.sq_item
12051209
12061210
This function is used by :c:func:`PySequence_GetItem` and has the same
1207-
signature. This slot must be filled for the :c:func:`PySequence_Check`
1211+
signature. It is also used by :c:func:`PyObject_GetItem`, after trying
1212+
the subscription via the :c:member:`~PyMappingMethods.mp_subscript` slot.
1213+
This slot must be filled for the :c:func:`PySequence_Check`
12081214
function to return ``1``, it can be *NULL* otherwise.
12091215

12101216
Negative indexes are handled as follows: if the :attr:`sq_length` slot is
@@ -1215,28 +1221,36 @@ Sequence Object Structures
12151221
.. c:member:: ssizeobjargproc PySequenceMethods.sq_ass_item
12161222
12171223
This function is used by :c:func:`PySequence_SetItem` and has the same
1218-
signature. This slot may be left to *NULL* if the object does not support
1224+
signature. It is also used by :c:func:`PyObject_SetItem` and
1225+
:c:func:`PyObject_DelItem`, after trying the item assignment and deletion
1226+
via the :c:member:`~PyMappingMethods.mp_ass_subscript` slot.
1227+
This slot may be left to *NULL* if the object does not support
12191228
item assignment and deletion.
12201229

12211230
.. c:member:: objobjproc PySequenceMethods.sq_contains
12221231
12231232
This function may be used by :c:func:`PySequence_Contains` and has the same
12241233
signature. This slot may be left to *NULL*, in this case
1225-
:c:func:`PySequence_Contains` simply traverses the sequence until it finds a
1226-
match.
1234+
:c:func:`!PySequence_Contains` simply traverses the sequence until it
1235+
finds a match.
12271236

12281237
.. c:member:: binaryfunc PySequenceMethods.sq_inplace_concat
12291238
12301239
This function is used by :c:func:`PySequence_InPlaceConcat` and has the same
1231-
signature. It should modify its first operand, and return it.
1240+
signature. It should modify its first operand, and return it. This slot
1241+
may be left to *NULL*, in this case :c:func:`!PySequence_InPlaceConcat`
1242+
will fall back to :c:func:`PySequence_Concat`. It is also used by the
1243+
augmented assignment ``+=``, after trying numeric inplace addition
1244+
via the :c:member:`~PyNumberMethods.nb_inplace_add` slot.
12321245

12331246
.. c:member:: ssizeargfunc PySequenceMethods.sq_inplace_repeat
12341247
12351248
This function is used by :c:func:`PySequence_InPlaceRepeat` and has the same
1236-
signature. It should modify its first operand, and return it.
1237-
1238-
.. XXX need to explain precedence between mapping and sequence
1239-
.. XXX explains when to implement the sq_inplace_* slots
1249+
signature. It should modify its first operand, and return it. This slot
1250+
may be left to *NULL*, in this case :c:func:`!PySequence_InPlaceRepeat`
1251+
will fall back to :c:func:`PySequence_Repeat`. It is also used by the
1252+
augmented assignment ``*=``, after trying numeric inplace multiplication
1253+
via the :c:member:`~PyNumberMethods.nb_inplace_multiply` slot.
12401254

12411255

12421256
.. _buffer-structs:

0 commit comments

Comments
 (0)