blob: 0ef29616e549c5b5406038bc47a2f3776dcb0290 [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
Georg Brandl60203b42010-10-06 10:11:56 +000025.. c:function:: int PyMapping_DelItemString(PyObject *o, 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
Georg Brandl60203b42010-10-06 10:11:56 +000037.. c:function:: int PyMapping_HasKeyString(PyObject *o, 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
53 On success, return a list of the keys in object *o*. On failure, return *NULL*.
Ezio Melotti8f7649e2009-09-13 04:48:45 +000054 This is equivalent to the Python expression ``list(o.keys())``.
Georg Brandl54a3faa2008-01-20 09:30:57 +000055
56
Georg Brandl60203b42010-10-06 10:11:56 +000057.. c:function:: PyObject* PyMapping_Values(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000058
59 On success, return a list of the values in object *o*. On failure, return
Ezio Melotti8f7649e2009-09-13 04:48:45 +000060 *NULL*. This is equivalent to the Python expression ``list(o.values())``.
Georg Brandl54a3faa2008-01-20 09:30:57 +000061
62
Georg Brandl60203b42010-10-06 10:11:56 +000063.. c:function:: PyObject* PyMapping_Items(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000064
65 On success, return a list of the items in object *o*, where each item is a tuple
66 containing a key-value pair. On failure, return *NULL*. This is equivalent to
Ezio Melotti8f7649e2009-09-13 04:48:45 +000067 the Python expression ``list(o.items())``.
Georg Brandl54a3faa2008-01-20 09:30:57 +000068
69
Georg Brandl60203b42010-10-06 10:11:56 +000070.. c:function:: PyObject* PyMapping_GetItemString(PyObject *o, char *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +000071
72 Return element of *o* corresponding to the object *key* or *NULL* on failure.
73 This is the equivalent of the Python expression ``o[key]``.
74
75
Georg Brandl60203b42010-10-06 10:11:56 +000076.. c:function:: int PyMapping_SetItemString(PyObject *o, char *key, PyObject *v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000077
78 Map the object *key* to the value *v* in object *o*. Returns ``-1`` on failure.
79 This is the equivalent of the Python statement ``o[key] = v``.