blob: c2c22e50e23763cd2c0df63d74efc04ddcafccbc [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _mapping:
4
5Mapping Protocol
6================
7
8
9.. cfunction:: int PyMapping_Check(PyObject *o)
10
11 Return ``1`` if the object provides mapping protocol, and ``0`` otherwise. This
12 function always succeeds.
13
14
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000015.. cfunction:: Py_ssize_t PyMapping_Size(PyObject *o)
16 Py_ssize_t PyMapping_Length(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +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 Wervenbd875522009-04-26 21:06:15 +000024 .. versionchanged:: 2.5
25 These functions returned an :ctype:`int` type. This might require
26 changes in your code for properly supporting 64-bit systems.
27
Georg Brandl54a3faa2008-01-20 09:30:57 +000028
29.. cfunction:: int PyMapping_DelItemString(PyObject *o, char *key)
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
35.. cfunction:: int PyMapping_DelItem(PyObject *o, PyObject *key)
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
41.. cfunction:: int PyMapping_HasKeyString(PyObject *o, char *key)
42
43 On success, return ``1`` if the mapping object has the key *key* and ``0``
44 otherwise. This is equivalent to the Python expression ``key in o``.
45 This function always succeeds.
46
47
48.. cfunction:: int PyMapping_HasKey(PyObject *o, PyObject *key)
49
50 Return ``1`` if the mapping object has the key *key* and ``0`` otherwise. This
51 is equivalent to the Python expression ``key in o``. This function always
52 succeeds.
53
54
55.. cfunction:: PyObject* PyMapping_Keys(PyObject *o)
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
61.. cfunction:: PyObject* PyMapping_Values(PyObject *o)
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
67.. cfunction:: PyObject* PyMapping_Items(PyObject *o)
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
74.. cfunction:: PyObject* PyMapping_GetItemString(PyObject *o, char *key)
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
80.. cfunction:: int PyMapping_SetItemString(PyObject *o, char *key, PyObject *v)
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``.