1.. highlight:: c 2 3.. _mapping: 4 5Mapping Protocol 6================ 7 8See 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 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 type of keys it 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:: builtin: 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 Return element of *o* corresponding to the string *key* or ``NULL`` on failure. 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``. 40 See also :c:func:`PyObject_SetItem`. 41 42 43.. c:function:: int PyMapping_DelItem(PyObject *o, PyObject *key) 44 45 Remove the mapping for the object *key* from the object *o*. Return ``-1`` 46 on failure. This is equivalent to the Python statement ``del o[key]``. 47 This is an alias of :c:func:`PyObject_DelItem`. 48 49 50.. c:function:: int PyMapping_DelItemString(PyObject *o, const char *key) 51 52 Remove the mapping for the string *key* from the object *o*. Return ``-1`` 53 on failure. This is equivalent to the Python statement ``del o[key]``. 54 55 56.. c:function:: int PyMapping_HasKey(PyObject *o, PyObject *key) 57 58 Return ``1`` if the mapping object has the key *key* and ``0`` otherwise. 59 This is equivalent to the Python expression ``key in o``. 60 This function always succeeds. 61 62 Note that exceptions which occur while calling the :meth:`__getitem__` 63 method will get suppressed. 64 To get error reporting use :c:func:`PyObject_GetItem()` instead. 65 66 67.. c:function:: int PyMapping_HasKeyString(PyObject *o, const char *key) 68 69 Return ``1`` if the mapping object has the key *key* and ``0`` otherwise. 70 This is equivalent to the Python expression ``key in o``. 71 This function always succeeds. 72 73 Note that exceptions which occur while calling the :meth:`__getitem__` 74 method and creating a temporary string object will get suppressed. 75 To get error reporting use :c:func:`PyMapping_GetItemString()` instead. 76 77 78.. c:function:: PyObject* PyMapping_Keys(PyObject *o) 79 80 On success, return a list of the keys in object *o*. On failure, return 81 ``NULL``. 82 83 .. versionchanged:: 3.7 84 Previously, the function returned a list or a tuple. 85 86 87.. c:function:: PyObject* PyMapping_Values(PyObject *o) 88 89 On success, return a list of the values in object *o*. On failure, return 90 ``NULL``. 91 92 .. versionchanged:: 3.7 93 Previously, the function returned a list or a tuple. 94 95 96.. c:function:: PyObject* PyMapping_Items(PyObject *o) 97 98 On success, return a list of the items in object *o*, where each item is a 99 tuple containing a key-value pair. On failure, return ``NULL``. 100 101 .. versionchanged:: 3.7 102 Previously, the function returned a list or a tuple. 103