blob: 682160d1475c1c4b9cfff1aa0b386dff9299ab9d [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: c
Georg Brandl54a3faa2008-01-20 09:30:57 +00002
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
Aveheuzed06ca2a22019-11-21 02:19:00 +010017 determine what type of keys it supports. This function always succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000018
19
Georg Brandl60203b42010-10-06 10:11:56 +000020.. c:function:: Py_ssize_t PyMapping_Size(PyObject *o)
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000021 Py_ssize_t PyMapping_Length(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000022
23 .. index:: builtin: len
24
Serhiy Storchakaf5b11832018-05-22 11:02:44 +030025 Returns the number of keys in object *o* on success, and ``-1`` on failure.
26 This is equivalent to the Python expression ``len(o)``.
Georg Brandl54a3faa2008-01-20 09:30:57 +000027
Georg Brandl54a3faa2008-01-20 09:30:57 +000028
Serhiy Storchakaf5b11832018-05-22 11:02:44 +030029.. c:function:: PyObject* PyMapping_GetItemString(PyObject *o, const char *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +000030
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020031 Return element of *o* corresponding to the string *key* or ``NULL`` on failure.
Serhiy Storchakaf5b11832018-05-22 11:02:44 +030032 This is the equivalent of the Python expression ``o[key]``.
33 See also :c:func:`PyObject_GetItem`.
34
35
36.. c:function:: int PyMapping_SetItemString(PyObject *o, const char *key, PyObject *v)
37
38 Map the string *key* to the value *v* in object *o*. Returns ``-1`` on
39 failure. This is the equivalent of the Python statement ``o[key] = v``.
Joannah Nanjekyee1e80002020-01-29 07:20:53 -040040 See also :c:func:`PyObject_SetItem`. This function *does not* steal a
41 reference to *v*.
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
Serhiy Storchaka3fcc1e02018-12-18 13:57:17 +020063 Note that exceptions which occur while calling the :meth:`__getitem__`
64 method will get suppressed.
65 To get error reporting use :c:func:`PyObject_GetItem()` instead.
66
Serhiy Storchakaf5b11832018-05-22 11:02:44 +030067
68.. c:function:: int PyMapping_HasKeyString(PyObject *o, const char *key)
69
70 Return ``1`` if the mapping object has the key *key* and ``0`` otherwise.
71 This is equivalent to the Python expression ``key in o``.
72 This function always succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000073
Serhiy Storchaka3fcc1e02018-12-18 13:57:17 +020074 Note that exceptions which occur while calling the :meth:`__getitem__`
75 method and creating a temporary string object will get suppressed.
76 To get error reporting use :c:func:`PyMapping_GetItemString()` instead.
77
Georg Brandl54a3faa2008-01-20 09:30:57 +000078
Georg Brandl60203b42010-10-06 10:11:56 +000079.. c:function:: PyObject* PyMapping_Keys(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000080
Oren Milman0ccc0f62017-10-08 11:17:46 +030081 On success, return a list of the keys in object *o*. On failure, return
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020082 ``NULL``.
Oren Milman0ccc0f62017-10-08 11:17:46 +030083
84 .. versionchanged:: 3.7
85 Previously, the function returned a list or a tuple.
Georg Brandl54a3faa2008-01-20 09:30:57 +000086
87
Georg Brandl60203b42010-10-06 10:11:56 +000088.. c:function:: PyObject* PyMapping_Values(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000089
Oren Milman0ccc0f62017-10-08 11:17:46 +030090 On success, return a list of the values in object *o*. On failure, return
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020091 ``NULL``.
Oren Milman0ccc0f62017-10-08 11:17:46 +030092
93 .. versionchanged:: 3.7
94 Previously, the function returned a list or a tuple.
Georg Brandl54a3faa2008-01-20 09:30:57 +000095
96
Georg Brandl60203b42010-10-06 10:11:56 +000097.. c:function:: PyObject* PyMapping_Items(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000098
Oren Milman0ccc0f62017-10-08 11:17:46 +030099 On success, return a list of the items in object *o*, where each item is a
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200100 tuple containing a key-value pair. On failure, return ``NULL``.
Oren Milman0ccc0f62017-10-08 11:17:46 +0300101
102 .. versionchanged:: 3.7
103 Previously, the function returned a list or a tuple.