Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _mapping: |
| 4 | |
| 5 | Mapping Protocol |
| 6 | ================ |
| 7 | |
| 8 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 9 | .. c:function:: int PyMapping_Check(PyObject *o) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 10 | |
| 11 | Return ``1`` if the object provides mapping protocol, and ``0`` otherwise. This |
| 12 | function always succeeds. |
| 13 | |
| 14 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 15 | .. c:function:: Py_ssize_t PyMapping_Size(PyObject *o) |
Jeroen Ruigrok van der Werven | 089c5cd | 2009-04-25 17:59:03 +0000 | [diff] [blame] | 16 | Py_ssize_t PyMapping_Length(PyObject *o) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +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 | |
Jeroen Ruigrok van der Werven | 089c5cd | 2009-04-25 17:59:03 +0000 | [diff] [blame] | 24 | .. versionchanged:: 2.5 |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 25 | These functions returned an :c:type:`int` type. This might require |
Jeroen Ruigrok van der Werven | 089c5cd | 2009-04-25 17:59:03 +0000 | [diff] [blame] | 26 | changes in your code for properly supporting 64-bit systems. |
| 27 | |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 28 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 29 | .. c:function:: int PyMapping_DelItemString(PyObject *o, char *key) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 30 | |
| 31 | Remove the mapping for object *key* from the object *o*. Return ``-1`` on |
| 32 | failure. This is equivalent to the Python statement ``del o[key]``. |
| 33 | |
| 34 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 35 | .. c:function:: int PyMapping_DelItem(PyObject *o, PyObject *key) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 36 | |
| 37 | Remove the mapping for object *key* from the object *o*. Return ``-1`` on |
| 38 | failure. This is equivalent to the Python statement ``del o[key]``. |
| 39 | |
| 40 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 41 | .. c:function:: int PyMapping_HasKeyString(PyObject *o, char *key) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 42 | |
| 43 | On success, return ``1`` if the mapping object has the key *key* and ``0`` |
Georg Brandl | 8ca6c84 | 2008-03-28 12:22:12 +0000 | [diff] [blame] | 44 | otherwise. This is equivalent to ``o[key]``, returning ``True`` on success |
| 45 | and ``False`` on an exception. This function always succeeds. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 46 | |
| 47 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 48 | .. c:function:: int PyMapping_HasKey(PyObject *o, PyObject *key) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 49 | |
Georg Brandl | 8ca6c84 | 2008-03-28 12:22:12 +0000 | [diff] [blame] | 50 | Return ``1`` if the mapping object has the key *key* and ``0`` otherwise. |
| 51 | This is equivalent to ``o[key]``, returning ``True`` on success and ``False`` |
| 52 | on an exception. This function always succeeds. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 53 | |
| 54 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 55 | .. c:function:: PyObject* PyMapping_Keys(PyObject *o) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 56 | |
| 57 | On success, return a list of the keys in object *o*. On failure, return *NULL*. |
| 58 | This is equivalent to the Python expression ``o.keys()``. |
| 59 | |
| 60 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 61 | .. c:function:: PyObject* PyMapping_Values(PyObject *o) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 62 | |
| 63 | On success, return a list of the values in object *o*. On failure, return |
| 64 | *NULL*. This is equivalent to the Python expression ``o.values()``. |
| 65 | |
| 66 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 67 | .. c:function:: PyObject* PyMapping_Items(PyObject *o) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 68 | |
| 69 | On success, return a list of the items in object *o*, where each item is a tuple |
| 70 | containing a key-value pair. On failure, return *NULL*. This is equivalent to |
| 71 | the Python expression ``o.items()``. |
| 72 | |
| 73 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 74 | .. c:function:: PyObject* PyMapping_GetItemString(PyObject *o, char *key) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 75 | |
| 76 | Return element of *o* corresponding to the object *key* or *NULL* on failure. |
| 77 | This is the equivalent of the Python expression ``o[key]``. |
| 78 | |
| 79 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 80 | .. c:function:: int PyMapping_SetItemString(PyObject *o, char *key, PyObject *v) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 81 | |
| 82 | Map the object *key* to the value *v* in object *o*. Returns ``-1`` on failure. |
| 83 | This is the equivalent of the Python statement ``o[key] = v``. |