Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _mapping: |
| 4 | |
| 5 | Mapping Protocol |
| 6 | ================ |
| 7 | |
Serhiy Storchaka | f5b1183 | 2018-05-22 11:02:44 +0300 | [diff] [blame^] | 8 | See also :c:func:`PyObject_GetItem`, :c:func:`PyObject_SetItem` and |
| 9 | :c:func:`PyObject_DelItem`. |
| 10 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 11 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 12 | .. c:function:: int PyMapping_Check(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 13 | |
Serhiy Storchaka | f5b1183 | 2018-05-22 11:02:44 +0300 | [diff] [blame^] | 14 | 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 19 | |
| 20 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 21 | .. c:function:: Py_ssize_t PyMapping_Size(PyObject *o) |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 22 | Py_ssize_t PyMapping_Length(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 23 | |
| 24 | .. index:: builtin: len |
| 25 | |
Serhiy Storchaka | f5b1183 | 2018-05-22 11:02:44 +0300 | [diff] [blame^] | 26 | 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 28 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 29 | |
Serhiy Storchaka | f5b1183 | 2018-05-22 11:02:44 +0300 | [diff] [blame^] | 30 | .. c:function:: PyObject* PyMapping_GetItemString(PyObject *o, const char *key) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 31 | |
Serhiy Storchaka | f5b1183 | 2018-05-22 11:02:44 +0300 | [diff] [blame^] | 32 | 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 42 | |
| 43 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 44 | .. c:function:: int PyMapping_DelItem(PyObject *o, PyObject *key) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 45 | |
Serhiy Storchaka | f5b1183 | 2018-05-22 11:02:44 +0300 | [diff] [blame^] | 46 | 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 49 | |
| 50 | |
Serhiy Storchaka | f5b1183 | 2018-05-22 11:02:44 +0300 | [diff] [blame^] | 51 | .. c:function:: int PyMapping_DelItemString(PyObject *o, const char *key) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 52 | |
Serhiy Storchaka | f5b1183 | 2018-05-22 11:02:44 +0300 | [diff] [blame^] | 53 | 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 55 | |
| 56 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 57 | .. c:function:: int PyMapping_HasKey(PyObject *o, PyObject *key) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 58 | |
Serhiy Storchaka | f5b1183 | 2018-05-22 11:02:44 +0300 | [diff] [blame^] | 59 | 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 69 | |
| 70 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 71 | .. c:function:: PyObject* PyMapping_Keys(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 72 | |
Oren Milman | 0ccc0f6 | 2017-10-08 11:17:46 +0300 | [diff] [blame] | 73 | 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 78 | |
| 79 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 80 | .. c:function:: PyObject* PyMapping_Values(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 81 | |
Oren Milman | 0ccc0f6 | 2017-10-08 11:17:46 +0300 | [diff] [blame] | 82 | 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 87 | |
| 88 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 89 | .. c:function:: PyObject* PyMapping_Items(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 90 | |
Oren Milman | 0ccc0f6 | 2017-10-08 11:17:46 +0300 | [diff] [blame] | 91 | 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. |