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