Stéphane Wirtel | cbb6484 | 2019-05-17 11:55:34 +0200 | [diff] [blame] | 1 | .. highlight:: c |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 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 |
Aveheuzed | 06ca2a2 | 2019-11-21 02:19:00 +0100 | [diff] [blame] | 17 | determine what type of keys it supports. This function always succeeds. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 18 | |
| 19 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 20 | .. c:function:: Py_ssize_t PyMapping_Size(PyObject *o) |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 21 | Py_ssize_t PyMapping_Length(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 22 | |
| 23 | .. index:: builtin: len |
| 24 | |
Serhiy Storchaka | f5b1183 | 2018-05-22 11:02:44 +0300 | [diff] [blame] | 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)``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 27 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 28 | |
Serhiy Storchaka | f5b1183 | 2018-05-22 11:02:44 +0300 | [diff] [blame] | 29 | .. c:function:: PyObject* PyMapping_GetItemString(PyObject *o, const char *key) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 30 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 31 | Return element of *o* corresponding to the string *key* or ``NULL`` on failure. |
Serhiy Storchaka | f5b1183 | 2018-05-22 11:02:44 +0300 | [diff] [blame] | 32 | 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 Nanjekye | e1e8000 | 2020-01-29 07:20:53 -0400 | [diff] [blame^] | 40 | See also :c:func:`PyObject_SetItem`. This function *does not* steal a |
| 41 | reference to *v*. |
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 | |
Serhiy Storchaka | 3fcc1e0 | 2018-12-18 13:57:17 +0200 | [diff] [blame] | 63 | 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 Storchaka | f5b1183 | 2018-05-22 11:02:44 +0300 | [diff] [blame] | 67 | |
| 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 73 | |
Serhiy Storchaka | 3fcc1e0 | 2018-12-18 13:57:17 +0200 | [diff] [blame] | 74 | 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 78 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 79 | .. c:function:: PyObject* PyMapping_Keys(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 80 | |
Oren Milman | 0ccc0f6 | 2017-10-08 11:17:46 +0300 | [diff] [blame] | 81 | On success, return a list of the keys in object *o*. On failure, return |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 82 | ``NULL``. |
Oren Milman | 0ccc0f6 | 2017-10-08 11:17:46 +0300 | [diff] [blame] | 83 | |
| 84 | .. versionchanged:: 3.7 |
| 85 | Previously, the function returned a list or a tuple. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 86 | |
| 87 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 88 | .. c:function:: PyObject* PyMapping_Values(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 89 | |
Oren Milman | 0ccc0f6 | 2017-10-08 11:17:46 +0300 | [diff] [blame] | 90 | On success, return a list of the values in object *o*. On failure, return |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 91 | ``NULL``. |
Oren Milman | 0ccc0f6 | 2017-10-08 11:17:46 +0300 | [diff] [blame] | 92 | |
| 93 | .. versionchanged:: 3.7 |
| 94 | Previously, the function returned a list or a tuple. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 95 | |
| 96 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 97 | .. c:function:: PyObject* PyMapping_Items(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 98 | |
Oren Milman | 0ccc0f6 | 2017-10-08 11:17:46 +0300 | [diff] [blame] | 99 | On success, return a list of the items in object *o*, where each item is a |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 100 | tuple containing a key-value pair. On failure, return ``NULL``. |
Oren Milman | 0ccc0f6 | 2017-10-08 11:17:46 +0300 | [diff] [blame] | 101 | |
| 102 | .. versionchanged:: 3.7 |
| 103 | Previously, the function returned a list or a tuple. |