blob: 7a83a5022c12034f36863ba2e8680a8b4e5a7254 [file] [log] [blame]
Georg Brandlf6842722008-01-19 22:08:21 +00001.. highlightlang:: c
2
3.. _mapping:
4
5Mapping Protocol
6================
7
8
Sandro Tosi98ed08f2012-01-14 16:42:02 +01009.. c:function:: int PyMapping_Check(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +000010
11 Return ``1`` if the object provides mapping protocol, and ``0`` otherwise. This
12 function always succeeds.
13
14
Sandro Tosi98ed08f2012-01-14 16:42:02 +010015.. c:function:: Py_ssize_t PyMapping_Size(PyObject *o)
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000016 Py_ssize_t PyMapping_Length(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +000017
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 Werven089c5cd2009-04-25 17:59:03 +000024 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +010025 These functions returned an :c:type:`int` type. This might require
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000026 changes in your code for properly supporting 64-bit systems.
27
Georg Brandlf6842722008-01-19 22:08:21 +000028
Sandro Tosi98ed08f2012-01-14 16:42:02 +010029.. c:function:: int PyMapping_DelItemString(PyObject *o, char *key)
Georg Brandlf6842722008-01-19 22:08:21 +000030
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 Tosi98ed08f2012-01-14 16:42:02 +010035.. c:function:: int PyMapping_DelItem(PyObject *o, PyObject *key)
Georg Brandlf6842722008-01-19 22:08:21 +000036
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 Tosi98ed08f2012-01-14 16:42:02 +010041.. c:function:: int PyMapping_HasKeyString(PyObject *o, char *key)
Georg Brandlf6842722008-01-19 22:08:21 +000042
43 On success, return ``1`` if the mapping object has the key *key* and ``0``
Georg Brandl8ca6c842008-03-28 12:22:12 +000044 otherwise. This is equivalent to ``o[key]``, returning ``True`` on success
45 and ``False`` on an exception. This function always succeeds.
Georg Brandlf6842722008-01-19 22:08:21 +000046
47
Sandro Tosi98ed08f2012-01-14 16:42:02 +010048.. c:function:: int PyMapping_HasKey(PyObject *o, PyObject *key)
Georg Brandlf6842722008-01-19 22:08:21 +000049
Georg Brandl8ca6c842008-03-28 12:22:12 +000050 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 Brandlf6842722008-01-19 22:08:21 +000053
54
Sandro Tosi98ed08f2012-01-14 16:42:02 +010055.. c:function:: PyObject* PyMapping_Keys(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +000056
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 Tosi98ed08f2012-01-14 16:42:02 +010061.. c:function:: PyObject* PyMapping_Values(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +000062
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 Tosi98ed08f2012-01-14 16:42:02 +010067.. c:function:: PyObject* PyMapping_Items(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +000068
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 Tosi98ed08f2012-01-14 16:42:02 +010074.. c:function:: PyObject* PyMapping_GetItemString(PyObject *o, char *key)
Georg Brandlf6842722008-01-19 22:08:21 +000075
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 Tosi98ed08f2012-01-14 16:42:02 +010080.. c:function:: int PyMapping_SetItemString(PyObject *o, char *key, PyObject *v)
Georg Brandlf6842722008-01-19 22:08:21 +000081
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``.