blob: b8eaadbd702c106e2413d9d6ff282774a169ad3c [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _mapping:
4
5Mapping Protocol
6================
7
Serhiy Storchakaf5b11832018-05-22 11:02:44 +03008See also :c:func:`PyObject_GetItem`, :c:func:`PyObject_SetItem` and
9:c:func:`PyObject_DelItem`.
10
Georg Brandl54a3faa2008-01-20 09:30:57 +000011
Georg Brandl60203b42010-10-06 10:11:56 +000012.. c:function:: int PyMapping_Check(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000013
Serhiy Storchakaf5b11832018-05-22 11:02:44 +030014 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.
Georg Brandl54a3faa2008-01-20 09:30:57 +000019
20
Georg Brandl60203b42010-10-06 10:11:56 +000021.. c:function:: Py_ssize_t PyMapping_Size(PyObject *o)
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000022 Py_ssize_t PyMapping_Length(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000023
24 .. index:: builtin: len
25
Serhiy Storchakaf5b11832018-05-22 11:02:44 +030026 Returns the number of keys in object *o* on success, and ``-1`` on failure.
27 This is equivalent to the Python expression ``len(o)``.
Georg Brandl54a3faa2008-01-20 09:30:57 +000028
Georg Brandl54a3faa2008-01-20 09:30:57 +000029
Serhiy Storchakaf5b11832018-05-22 11:02:44 +030030.. c:function:: PyObject* PyMapping_GetItemString(PyObject *o, const char *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +000031
Serhiy Storchakaf5b11832018-05-22 11:02:44 +030032 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
36
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`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000042
43
Georg Brandl60203b42010-10-06 10:11:56 +000044.. c:function:: int PyMapping_DelItem(PyObject *o, PyObject *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +000045
Serhiy Storchakaf5b11832018-05-22 11:02:44 +030046 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`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000049
50
Serhiy Storchakaf5b11832018-05-22 11:02:44 +030051.. c:function:: int PyMapping_DelItemString(PyObject *o, const char *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +000052
Serhiy Storchakaf5b11832018-05-22 11:02:44 +030053 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]``.
Georg Brandl54a3faa2008-01-20 09:30:57 +000055
56
Georg Brandl60203b42010-10-06 10:11:56 +000057.. c:function:: int PyMapping_HasKey(PyObject *o, PyObject *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +000058
Serhiy Storchakaf5b11832018-05-22 11:02:44 +030059 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.
Georg Brandl54a3faa2008-01-20 09:30:57 +000069
70
Georg Brandl60203b42010-10-06 10:11:56 +000071.. c:function:: PyObject* PyMapping_Keys(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000072
Oren Milman0ccc0f62017-10-08 11:17:46 +030073 On success, return a list of the keys in object *o*. On failure, return
74 *NULL*.
75
76 .. versionchanged:: 3.7
77 Previously, the function returned a list or a tuple.
Georg Brandl54a3faa2008-01-20 09:30:57 +000078
79
Georg Brandl60203b42010-10-06 10:11:56 +000080.. c:function:: PyObject* PyMapping_Values(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000081
Oren Milman0ccc0f62017-10-08 11:17:46 +030082 On success, return a list of the values in object *o*. On failure, return
83 *NULL*.
84
85 .. versionchanged:: 3.7
86 Previously, the function returned a list or a tuple.
Georg Brandl54a3faa2008-01-20 09:30:57 +000087
88
Georg Brandl60203b42010-10-06 10:11:56 +000089.. c:function:: PyObject* PyMapping_Items(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000090
Oren Milman0ccc0f62017-10-08 11:17:46 +030091 On success, return a list of the items in object *o*, where each item is a
92 tuple containing a key-value pair. On failure, return *NULL*.
93
94 .. versionchanged:: 3.7
95 Previously, the function returned a list or a tuple.