| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c | 
 | 2 |  | 
 | 3 | .. _mapping: | 
 | 4 |  | 
 | 5 | Mapping Protocol | 
 | 6 | ================ | 
 | 7 |  | 
 | 8 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 9 | .. c:function:: int PyMapping_Check(PyObject *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 10 |  | 
 | 11 |    Return ``1`` if the object provides mapping protocol, and ``0`` otherwise.  This | 
 | 12 |    function always succeeds. | 
 | 13 |  | 
 | 14 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 15 | .. c:function:: Py_ssize_t PyMapping_Size(PyObject *o) | 
| Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 16 |                Py_ssize_t PyMapping_Length(PyObject *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 17 |  | 
 | 18 |    .. index:: builtin: len | 
 | 19 |  | 
 | 20 |    Returns the number of keys in object *o* on success, and ``-1`` on failure.  For | 
 | 21 |    objects that do not provide mapping protocol, this is equivalent to the Python | 
 | 22 |    expression ``len(o)``. | 
 | 23 |  | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 24 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 25 | .. c:function:: int PyMapping_DelItemString(PyObject *o, char *key) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 26 |  | 
 | 27 |    Remove the mapping for object *key* from the object *o*. Return ``-1`` on | 
 | 28 |    failure.  This is equivalent to the Python statement ``del o[key]``. | 
 | 29 |  | 
 | 30 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 31 | .. c:function:: int PyMapping_DelItem(PyObject *o, PyObject *key) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 32 |  | 
 | 33 |    Remove the mapping for object *key* from the object *o*. Return ``-1`` on | 
 | 34 |    failure.  This is equivalent to the Python statement ``del o[key]``. | 
 | 35 |  | 
 | 36 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 37 | .. c:function:: int PyMapping_HasKeyString(PyObject *o, char *key) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 38 |  | 
 | 39 |    On success, return ``1`` if the mapping object has the key *key* and ``0`` | 
 | 40 |    otherwise.  This is equivalent to the Python expression ``key in o``. | 
 | 41 |    This function always succeeds. | 
 | 42 |  | 
 | 43 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 44 | .. c:function:: int PyMapping_HasKey(PyObject *o, PyObject *key) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 45 |  | 
 | 46 |    Return ``1`` if the mapping object has the key *key* and ``0`` otherwise.  This | 
 | 47 |    is equivalent to the Python expression ``key in o``.  This function always | 
 | 48 |    succeeds. | 
 | 49 |  | 
 | 50 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 51 | .. c:function:: PyObject* PyMapping_Keys(PyObject *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 52 |  | 
 | 53 |    On success, return a list of the keys in object *o*.  On failure, return *NULL*. | 
| Ezio Melotti | 8f7649e | 2009-09-13 04:48:45 +0000 | [diff] [blame] | 54 |    This is equivalent to the Python expression ``list(o.keys())``. | 
| 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:: PyObject* PyMapping_Values(PyObject *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 58 |  | 
 | 59 |    On success, return a list of the values in object *o*.  On failure, return | 
| Ezio Melotti | 8f7649e | 2009-09-13 04:48:45 +0000 | [diff] [blame] | 60 |    *NULL*. This is equivalent to the Python expression ``list(o.values())``. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 61 |  | 
 | 62 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 63 | .. c:function:: PyObject* PyMapping_Items(PyObject *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 64 |  | 
 | 65 |    On success, return a list of the items in object *o*, where each item is a tuple | 
 | 66 |    containing a key-value pair.  On failure, return *NULL*. This is equivalent to | 
| Ezio Melotti | 8f7649e | 2009-09-13 04:48:45 +0000 | [diff] [blame] | 67 |    the Python expression ``list(o.items())``. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 68 |  | 
 | 69 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 70 | .. c:function:: PyObject* PyMapping_GetItemString(PyObject *o, char *key) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 71 |  | 
 | 72 |    Return element of *o* corresponding to the object *key* or *NULL* on failure. | 
 | 73 |    This is the equivalent of the Python expression ``o[key]``. | 
 | 74 |  | 
 | 75 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 76 | .. c:function:: int PyMapping_SetItemString(PyObject *o, char *key, PyObject *v) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 77 |  | 
 | 78 |    Map the object *key* to the value *v* in object *o*. Returns ``-1`` on failure. | 
 | 79 |    This is the equivalent of the Python statement ``o[key] = v``. |