Debian Codegraph @

Doc/c-api/mapping.rst

raw
1 .. highlight:: c
2
3 .. _mapping:
4
5 Mapping Protocol
6 ================
7
8 See also :c:func:`PyObject_GetItem`, :c:func:`PyObject_SetItem` and
9 :c:func:`PyObject_DelItem`.
10
11
12 .. c:function:: int PyMapping_Check(PyObject *o)
13
14 Return ``1`` if the object provides the mapping protocol or supports slicing,
15 and ``0`` otherwise. Note that it returns ``1`` for Python classes with
16 a :meth:`~object.__getitem__` method, since in general it is impossible to
17 determine what type of keys the class supports. This function always succeeds.
18
19
20 .. c:function:: Py_ssize_t PyMapping_Size(PyObject *o)
21 Py_ssize_t PyMapping_Length(PyObject *o)
22
23 .. index:: pair: built-in function; len
24
25 Returns the number of keys in object *o* on success, and ``-1`` on failure.
26 This is equivalent to the Python expression ``len(o)``.
27
28
29 .. c:function:: PyObject* PyMapping_GetItemString(PyObject *o, const char *key)
30
31 This is the same as :c:func:`PyObject_GetItem`, but *key* is
32 specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
33 rather than a :c:expr:`PyObject*`.
34
35
36 .. c:function:: int PyMapping_GetOptionalItem(PyObject *obj, PyObject *key, PyObject **result)
37
38 Variant of :c:func:`PyObject_GetItem` which doesn't raise
39 :exc:`KeyError` if the key is not found.
40
41 If the key is found, return ``1`` and set *\*result* to a new
42 :term:`strong reference` to the corresponding value.
43 If the key is not found, return ``0`` and set *\*result* to ``NULL``;
44 the :exc:`KeyError` is silenced.
45 If an error other than :exc:`KeyError` is raised, return ``-1`` and
46 set *\*result* to ``NULL``.
47
48 .. versionadded:: 3.13
49
50
51 .. c:function:: int PyMapping_GetOptionalItemString(PyObject *obj, const char *key, PyObject **result)
52
53 This is the same as :c:func:`PyMapping_GetOptionalItem`, but *key* is
54 specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
55 rather than a :c:expr:`PyObject*`.
56
57 .. versionadded:: 3.13
58
59
60 .. c:function:: int PyMapping_SetItemString(PyObject *o, const char *key, PyObject *v)
61
62 This is the same as :c:func:`PyObject_SetItem`, but *key* is
63 specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
64 rather than a :c:expr:`PyObject*`.
65
66
67 .. c:function:: int PyMapping_DelItem(PyObject *o, PyObject *key)
68
69 This is an alias of :c:func:`PyObject_DelItem`.
70
71
72 .. c:function:: int PyMapping_DelItemString(PyObject *o, const char *key)
73
74 This is the same as :c:func:`PyObject_DelItem`, but *key* is
75 specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
76 rather than a :c:expr:`PyObject*`.
77
78
79 .. c:function:: int PyMapping_HasKeyWithError(PyObject *o, PyObject *key)
80
81 Return ``1`` if the mapping object has the key *key* and ``0`` otherwise.
82 This is equivalent to the Python expression ``key in o``.
83 On failure, return ``-1``.
84
85 .. versionadded:: 3.13
86
87
88 .. c:function:: int PyMapping_HasKeyStringWithError(PyObject *o, const char *key)
89
90 This is the same as :c:func:`PyMapping_HasKeyWithError`, but *key* is
91 specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
92 rather than a :c:expr:`PyObject*`.
93
94 .. versionadded:: 3.13
95
96
97 .. c:function:: int PyMapping_HasKey(PyObject *o, PyObject *key)
98
99 Return ``1`` if the mapping object has the key *key* and ``0`` otherwise.
100 This is equivalent to the Python expression ``key in o``.
101 This function always succeeds.
102
103 .. note::
104
105 Exceptions which occur when this calls the :meth:`~object.__getitem__`
106 method are silently ignored.
107 For proper error handling, use :c:func:`PyMapping_HasKeyWithError`,
108 :c:func:`PyMapping_GetOptionalItem` or :c:func:`PyObject_GetItem()` instead.
109
110
111 .. c:function:: int PyMapping_HasKeyString(PyObject *o, const char *key)
112
113 This is the same as :c:func:`PyMapping_HasKey`, but *key* is
114 specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
115 rather than a :c:expr:`PyObject*`.
116
117 .. note::
118
119 Exceptions that occur when this calls the :meth:`~object.__getitem__`
120 method or while creating the temporary :class:`str`
121 object are silently ignored.
122 For proper error handling, use :c:func:`PyMapping_HasKeyStringWithError`,
123 :c:func:`PyMapping_GetOptionalItemString` or
124 :c:func:`PyMapping_GetItemString` instead.
125
126
127 .. c:function:: PyObject* PyMapping_Keys(PyObject *o)
128
129 On success, return a list of the keys in object *o*. On failure, return
130 ``NULL``.
131
132 .. versionchanged:: 3.7
133 Previously, the function returned a list or a tuple.
134
135
136 .. c:function:: PyObject* PyMapping_Values(PyObject *o)
137
138 On success, return a list of the values in object *o*. On failure, return
139 ``NULL``.
140
141 .. versionchanged:: 3.7
142 Previously, the function returned a list or a tuple.
143
144
145 .. c:function:: PyObject* PyMapping_Items(PyObject *o)
146
147 On success, return a list of the items in object *o*, where each item is a
148 tuple containing a key-value pair. On failure, return ``NULL``.
149
150 .. versionchanged:: 3.7
151 Previously, the function returned a list or a tuple.