blob: 4015f43d8f1eedec097352f85263dc04f48432d4 [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
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 Storchaka25fc0882019-10-30 12:03:20 +020032 Return element of *o* corresponding to the string *key* or ``NULL`` on failure.
Serhiy Storchakaf5b11832018-05-22 11:02:44 +030033 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
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.