blob: 308a9761f87ea960ac0b89f2592c722319280a9a [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _mapping:
4
5Mapping Protocol
6================
7
8
Georg Brandl60203b42010-10-06 10:11:56 +00009.. c:function:: int PyMapping_Check(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000010
11 Return ``1`` if the object provides mapping protocol, and ``0`` otherwise. This
12 function always succeeds.
13
14
Georg Brandl60203b42010-10-06 10:11:56 +000015.. c:function:: Py_ssize_t PyMapping_Size(PyObject *o)
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000016 Py_ssize_t PyMapping_Length(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000017
18 .. index:: builtin: len
19
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)``.
23
Georg Brandl54a3faa2008-01-20 09:30:57 +000024
Serhiy Storchakac6792272013-10-19 21:03:34 +030025.. c:function:: int PyMapping_DelItemString(PyObject *o, const char *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +000026
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]``.
29
30
Georg Brandl60203b42010-10-06 10:11:56 +000031.. c:function:: int PyMapping_DelItem(PyObject *o, PyObject *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +000032
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]``.
35
36
Serhiy Storchaka03863d22015-06-21 17:11:21 +030037.. c:function:: int PyMapping_HasKeyString(PyObject *o, const char *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +000038
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.
42
43
Georg Brandl60203b42010-10-06 10:11:56 +000044.. c:function:: int PyMapping_HasKey(PyObject *o, PyObject *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +000045
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.
49
50
Georg Brandl60203b42010-10-06 10:11:56 +000051.. c:function:: PyObject* PyMapping_Keys(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000052
Oren Milman0ccc0f62017-10-08 11:17:46 +030053 On success, return a list of the keys in object *o*. On failure, return
54 *NULL*.
55
56 .. versionchanged:: 3.7
57 Previously, the function returned a list or a tuple.
Georg Brandl54a3faa2008-01-20 09:30:57 +000058
59
Georg Brandl60203b42010-10-06 10:11:56 +000060.. c:function:: PyObject* PyMapping_Values(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000061
Oren Milman0ccc0f62017-10-08 11:17:46 +030062 On success, return a list of the values in object *o*. On failure, return
63 *NULL*.
64
65 .. versionchanged:: 3.7
66 Previously, the function returned a list or a tuple.
Georg Brandl54a3faa2008-01-20 09:30:57 +000067
68
Georg Brandl60203b42010-10-06 10:11:56 +000069.. c:function:: PyObject* PyMapping_Items(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000070
Oren Milman0ccc0f62017-10-08 11:17:46 +030071 On success, return a list of the items in object *o*, where each item is a
72 tuple containing a key-value pair. On failure, return *NULL*.
73
74 .. versionchanged:: 3.7
75 Previously, the function returned a list or a tuple.
Georg Brandl54a3faa2008-01-20 09:30:57 +000076
77
Serhiy Storchakac6792272013-10-19 21:03:34 +030078.. c:function:: PyObject* PyMapping_GetItemString(PyObject *o, const char *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +000079
80 Return element of *o* corresponding to the object *key* or *NULL* on failure.
81 This is the equivalent of the Python expression ``o[key]``.
82
83
Serhiy Storchakac6792272013-10-19 21:03:34 +030084.. c:function:: int PyMapping_SetItemString(PyObject *o, const char *key, PyObject *v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000085
86 Map the object *key* to the value *v* in object *o*. Returns ``-1`` on failure.
87 This is the equivalent of the Python statement ``o[key] = v``.