Unverified Kaydet (Commit) 93e9fb56 authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka Kaydeden (comit) GitHub

[3.6] bpo-5945: Improve mappings and sequences C API docs. (GH-7029). (GH-7049)

(cherry picked from commit f5b11836)
üst 1b48b9cf
...@@ -5,11 +5,17 @@ ...@@ -5,11 +5,17 @@
Mapping Protocol Mapping Protocol
================ ================
See also :c:func:`PyObject_GetItem`, :c:func:`PyObject_SetItem` and
:c:func:`PyObject_DelItem`.
.. c:function:: int PyMapping_Check(PyObject *o) .. c:function:: int PyMapping_Check(PyObject *o)
Return ``1`` if the object provides mapping protocol, and ``0`` otherwise. This Return ``1`` if the object provides mapping protocol or supports slicing,
function always succeeds. and ``0`` otherwise. Note that it returns ``1`` for Python classes with
a :meth:`__getitem__` method since in general case it is impossible to
determine what the type of keys it supports. This function always
succeeds.
.. c:function:: Py_ssize_t PyMapping_Size(PyObject *o) .. c:function:: Py_ssize_t PyMapping_Size(PyObject *o)
...@@ -17,35 +23,49 @@ Mapping Protocol ...@@ -17,35 +23,49 @@ Mapping Protocol
.. index:: builtin: len .. index:: builtin: len
Returns the number of keys in object *o* on success, and ``-1`` on failure. For Returns the number of keys in object *o* on success, and ``-1`` on failure.
objects that do not provide mapping protocol, this is equivalent to the Python This is equivalent to the Python expression ``len(o)``.
expression ``len(o)``.
.. c:function:: int PyMapping_DelItemString(PyObject *o, const char *key) .. c:function:: PyObject* PyMapping_GetItemString(PyObject *o, const char *key)
Return element of *o* corresponding to the string *key* or *NULL* on failure.
This is the equivalent of the Python expression ``o[key]``.
See also :c:func:`PyObject_GetItem`.
Remove the mapping for object *key* from the object *o*. Return ``-1`` on .. c:function:: int PyMapping_SetItemString(PyObject *o, const char *key, PyObject *v)
failure. This is equivalent to the Python statement ``del o[key]``.
Map the string *key* to the value *v* in object *o*. Returns ``-1`` on
failure. This is the equivalent of the Python statement ``o[key] = v``.
See also :c:func:`PyObject_SetItem`.
.. c:function:: int PyMapping_DelItem(PyObject *o, PyObject *key) .. c:function:: int PyMapping_DelItem(PyObject *o, PyObject *key)
Remove the mapping for object *key* from the object *o*. Return ``-1`` on Remove the mapping for the object *key* from the object *o*. Return ``-1``
failure. This is equivalent to the Python statement ``del o[key]``. on failure. This is equivalent to the Python statement ``del o[key]``.
This is an alias of :c:func:`PyObject_DelItem`.
.. c:function:: int PyMapping_HasKeyString(PyObject *o, const char *key) .. c:function:: int PyMapping_DelItemString(PyObject *o, const char *key)
On success, return ``1`` if the mapping object has the key *key* and ``0`` Remove the mapping for the string *key* from the object *o*. Return ``-1``
otherwise. This is equivalent to the Python expression ``key in o``. on failure. This is equivalent to the Python statement ``del o[key]``.
This function always succeeds.
.. c:function:: int PyMapping_HasKey(PyObject *o, PyObject *key) .. c:function:: int PyMapping_HasKey(PyObject *o, PyObject *key)
Return ``1`` if the mapping object has the key *key* and ``0`` otherwise. This Return ``1`` if the mapping object has the key *key* and ``0`` otherwise.
is equivalent to the Python expression ``key in o``. This function always This is equivalent to the Python expression ``key in o``.
succeeds. This function always succeeds.
.. c:function:: int PyMapping_HasKeyString(PyObject *o, const char *key)
Return ``1`` if the mapping object has the key *key* and ``0`` otherwise.
This is equivalent to the Python expression ``key in o``.
This function always succeeds.
.. c:function:: PyObject* PyMapping_Keys(PyObject *o) .. c:function:: PyObject* PyMapping_Keys(PyObject *o)
...@@ -64,15 +84,3 @@ Mapping Protocol ...@@ -64,15 +84,3 @@ Mapping Protocol
On success, return a list or tuple of the items in object *o*, where each item On success, return a list or tuple of the items in object *o*, where each item
is a tuple containing a key-value pair. On failure, return *NULL*. is a tuple containing a key-value pair. On failure, return *NULL*.
.. c:function:: PyObject* PyMapping_GetItemString(PyObject *o, const char *key)
Return element of *o* corresponding to the object *key* or *NULL* on failure.
This is the equivalent of the Python expression ``o[key]``.
.. c:function:: int PyMapping_SetItemString(PyObject *o, const char *key, PyObject *v)
Map the object *key* to the value *v* in object *o*. Returns ``-1`` on failure.
This is the equivalent of the Python statement ``o[key] = v``.
...@@ -360,8 +360,8 @@ Object Protocol ...@@ -360,8 +360,8 @@ Object Protocol
parameters must be non-*NULL*. parameters must be non-*NULL*.
.. c:function:: Py_ssize_t PyObject_Length(PyObject *o) .. c:function:: Py_ssize_t PyObject_Size(PyObject *o)
Py_ssize_t PyObject_Size(PyObject *o) Py_ssize_t PyObject_Length(PyObject *o)
.. index:: builtin: len .. index:: builtin: len
...@@ -395,8 +395,8 @@ Object Protocol ...@@ -395,8 +395,8 @@ Object Protocol
.. c:function:: int PyObject_DelItem(PyObject *o, PyObject *key) .. c:function:: int PyObject_DelItem(PyObject *o, PyObject *key)
Delete the mapping for *key* from *o*. Returns ``-1`` on failure. This is the Remove the mapping for the object *key* from the object *o*. Return ``-1``
equivalent of the Python statement ``del o[key]``. on failure. This is equivalent to the Python statement ``del o[key]``.
.. c:function:: PyObject* PyObject_Dir(PyObject *o) .. c:function:: PyObject* PyObject_Dir(PyObject *o)
......
...@@ -9,7 +9,10 @@ Sequence Protocol ...@@ -9,7 +9,10 @@ Sequence Protocol
.. c:function:: int PySequence_Check(PyObject *o) .. c:function:: int PySequence_Check(PyObject *o)
Return ``1`` if the object provides sequence protocol, and ``0`` otherwise. Return ``1`` if the object provides sequence protocol, and ``0`` otherwise.
This function always succeeds. Note that it returns ``1`` for Python classes with a :meth:`__getitem__`
method unless they are :class:`dict` subclasses since in general case it
is impossible to determine what the type of keys it supports. This
function always succeeds.
.. c:function:: Py_ssize_t PySequence_Size(PyObject *o) .. c:function:: Py_ssize_t PySequence_Size(PyObject *o)
...@@ -119,18 +122,27 @@ Sequence Protocol ...@@ -119,18 +122,27 @@ Sequence Protocol
.. index:: builtin: tuple .. index:: builtin: tuple
Return a tuple object with the same contents as the arbitrary sequence *o* or Return a tuple object with the same contents as the sequence or iterable *o*,
*NULL* on failure. If *o* is a tuple, a new reference will be returned, or *NULL* on failure. If *o* is a tuple, a new reference will be returned,
otherwise a tuple will be constructed with the appropriate contents. This is otherwise a tuple will be constructed with the appropriate contents. This is
equivalent to the Python expression ``tuple(o)``. equivalent to the Python expression ``tuple(o)``.
.. c:function:: PyObject* PySequence_Fast(PyObject *o, const char *m) .. c:function:: PyObject* PySequence_Fast(PyObject *o, const char *m)
Return the sequence *o* as a list, unless it is already a tuple or list, in Return the sequence or iterable *o* as a list, unless it is already a tuple or list, in
which case *o* is returned. Use :c:func:`PySequence_Fast_GET_ITEM` to access which case *o* is returned. Use :c:func:`PySequence_Fast_GET_ITEM` to access
the members of the result. Returns *NULL* on failure. If the object is not the members of the result. Returns *NULL* on failure. If the object is not
a sequence, raises :exc:`TypeError` with *m* as the message text. a sequence or iterable, raises :exc:`TypeError` with *m* as the message text.
.. c:function:: Py_ssize_t PySequence_Fast_GET_SIZE(PyObject *o)
Returns the length of *o*, assuming that *o* was returned by
:c:func:`PySequence_Fast` and that *o* is not *NULL*. The size can also be
gotten by calling :c:func:`PySequence_Size` on *o*, but
:c:func:`PySequence_Fast_GET_SIZE` is faster because it can assume *o* is a list
or tuple.
.. c:function:: PyObject* PySequence_Fast_GET_ITEM(PyObject *o, Py_ssize_t i) .. c:function:: PyObject* PySequence_Fast_GET_ITEM(PyObject *o, Py_ssize_t i)
...@@ -155,12 +167,3 @@ Sequence Protocol ...@@ -155,12 +167,3 @@ Sequence Protocol
:c:func:`PySequence_GetItem` but without checking that :c:func:`PySequence_GetItem` but without checking that
:c:func:`PySequence_Check` on *o* is true and without adjustment for negative :c:func:`PySequence_Check` on *o* is true and without adjustment for negative
indices. indices.
.. c:function:: Py_ssize_t PySequence_Fast_GET_SIZE(PyObject *o)
Returns the length of *o*, assuming that *o* was returned by
:c:func:`PySequence_Fast` and that *o* is not *NULL*. The size can also be
gotten by calling :c:func:`PySequence_Size` on *o*, but
:c:func:`PySequence_Fast_GET_SIZE` is faster because it can assume *o* is a list
or tuple.
...@@ -1151,21 +1151,24 @@ Mapping Object Structures ...@@ -1151,21 +1151,24 @@ Mapping Object Structures
.. c:member:: lenfunc PyMappingMethods.mp_length .. c:member:: lenfunc PyMappingMethods.mp_length
This function is used by :c:func:`PyMapping_Length` and This function is used by :c:func:`PyMapping_Size` and
:c:func:`PyObject_Size`, and has the same signature. This slot may be set to :c:func:`PyObject_Size`, and has the same signature. This slot may be set to
*NULL* if the object has no defined length. *NULL* if the object has no defined length.
.. c:member:: binaryfunc PyMappingMethods.mp_subscript .. c:member:: binaryfunc PyMappingMethods.mp_subscript
This function is used by :c:func:`PyObject_GetItem` and has the same This function is used by :c:func:`PyObject_GetItem` and
signature. This slot must be filled for the :c:func:`PyMapping_Check` :c:func:`PySequence_GetSlice`, and has the same signature as
function to return ``1``, it can be *NULL* otherwise. :c:func:`!PyObject_GetItem`. This slot must be filled for the
:c:func:`PyMapping_Check` function to return ``1``, it can be *NULL*
otherwise.
.. c:member:: objobjargproc PyMappingMethods.mp_ass_subscript .. c:member:: objobjargproc PyMappingMethods.mp_ass_subscript
This function is used by :c:func:`PyObject_SetItem` and This function is used by :c:func:`PyObject_SetItem`,
:c:func:`PyObject_DelItem`. It has the same signature as :c:func:`PyObject_DelItem`, :c:func:`PyObject_SetSlice` and
:c:func:`PyObject_SetItem`, but *v* can also be set to *NULL* to delete :c:func:`PyObject_DelSlice`. It has the same signature as
:c:func:`!PyObject_SetItem`, but *v* can also be set to *NULL* to delete
an item. If this slot is *NULL*, the object does not support item an item. If this slot is *NULL*, the object does not support item
assignment and deletion. assignment and deletion.
...@@ -1185,26 +1188,29 @@ Sequence Object Structures ...@@ -1185,26 +1188,29 @@ Sequence Object Structures
.. c:member:: lenfunc PySequenceMethods.sq_length .. c:member:: lenfunc PySequenceMethods.sq_length
This function is used by :c:func:`PySequence_Size` and :c:func:`PyObject_Size`, This function is used by :c:func:`PySequence_Size` and
and has the same signature. :c:func:`PyObject_Size`, and has the same signature. It is also used for
handling negative indices via the :c:member:`~PySequenceMethods.sq_item`
and the :c:member:`~PySequenceMethods.sq_ass_item` slots.
.. c:member:: binaryfunc PySequenceMethods.sq_concat .. c:member:: binaryfunc PySequenceMethods.sq_concat
This function is used by :c:func:`PySequence_Concat` and has the same This function is used by :c:func:`PySequence_Concat` and has the same
signature. It is also used by the ``+`` operator, after trying the numeric signature. It is also used by the ``+`` operator, after trying the numeric
addition via the :c:member:`~PyTypeObject.tp_as_number.nb_add` slot. addition via the :c:member:`~PyNumberMethods.nb_add` slot.
.. c:member:: ssizeargfunc PySequenceMethods.sq_repeat .. c:member:: ssizeargfunc PySequenceMethods.sq_repeat
This function is used by :c:func:`PySequence_Repeat` and has the same This function is used by :c:func:`PySequence_Repeat` and has the same
signature. It is also used by the ``*`` operator, after trying numeric signature. It is also used by the ``*`` operator, after trying numeric
multiplication via the :c:member:`~PyTypeObject.tp_as_number.nb_multiply` multiplication via the :c:member:`~PyNumberMethods.nb_multiply` slot.
slot.
.. c:member:: ssizeargfunc PySequenceMethods.sq_item .. c:member:: ssizeargfunc PySequenceMethods.sq_item
This function is used by :c:func:`PySequence_GetItem` and has the same This function is used by :c:func:`PySequence_GetItem` and has the same
signature. This slot must be filled for the :c:func:`PySequence_Check` signature. It is also used by :c:func:`PyObject_GetItem`, after trying
the subscription via the :c:member:`~PyMappingMethods.mp_subscript` slot.
This slot must be filled for the :c:func:`PySequence_Check`
function to return ``1``, it can be *NULL* otherwise. function to return ``1``, it can be *NULL* otherwise.
Negative indexes are handled as follows: if the :attr:`sq_length` slot is Negative indexes are handled as follows: if the :attr:`sq_length` slot is
...@@ -1215,28 +1221,36 @@ Sequence Object Structures ...@@ -1215,28 +1221,36 @@ Sequence Object Structures
.. c:member:: ssizeobjargproc PySequenceMethods.sq_ass_item .. c:member:: ssizeobjargproc PySequenceMethods.sq_ass_item
This function is used by :c:func:`PySequence_SetItem` and has the same This function is used by :c:func:`PySequence_SetItem` and has the same
signature. This slot may be left to *NULL* if the object does not support signature. It is also used by :c:func:`PyObject_SetItem` and
:c:func:`PyObject_DelItem`, after trying the item assignment and deletion
via the :c:member:`~PyMappingMethods.mp_ass_subscript` slot.
This slot may be left to *NULL* if the object does not support
item assignment and deletion. item assignment and deletion.
.. c:member:: objobjproc PySequenceMethods.sq_contains .. c:member:: objobjproc PySequenceMethods.sq_contains
This function may be used by :c:func:`PySequence_Contains` and has the same This function may be used by :c:func:`PySequence_Contains` and has the same
signature. This slot may be left to *NULL*, in this case signature. This slot may be left to *NULL*, in this case
:c:func:`PySequence_Contains` simply traverses the sequence until it finds a :c:func:`!PySequence_Contains` simply traverses the sequence until it
match. finds a match.
.. c:member:: binaryfunc PySequenceMethods.sq_inplace_concat .. c:member:: binaryfunc PySequenceMethods.sq_inplace_concat
This function is used by :c:func:`PySequence_InPlaceConcat` and has the same This function is used by :c:func:`PySequence_InPlaceConcat` and has the same
signature. It should modify its first operand, and return it. signature. It should modify its first operand, and return it. This slot
may be left to *NULL*, in this case :c:func:`!PySequence_InPlaceConcat`
will fall back to :c:func:`PySequence_Concat`. It is also used by the
augmented assignment ``+=``, after trying numeric inplace addition
via the :c:member:`~PyNumberMethods.nb_inplace_add` slot.
.. c:member:: ssizeargfunc PySequenceMethods.sq_inplace_repeat .. c:member:: ssizeargfunc PySequenceMethods.sq_inplace_repeat
This function is used by :c:func:`PySequence_InPlaceRepeat` and has the same This function is used by :c:func:`PySequence_InPlaceRepeat` and has the same
signature. It should modify its first operand, and return it. signature. It should modify its first operand, and return it. This slot
may be left to *NULL*, in this case :c:func:`!PySequence_InPlaceRepeat`
.. XXX need to explain precedence between mapping and sequence will fall back to :c:func:`PySequence_Repeat`. It is also used by the
.. XXX explains when to implement the sq_inplace_* slots augmented assignment ``*=``, after trying numeric inplace multiplication
via the :c:member:`~PyNumberMethods.nb_inplace_multiply` slot.
.. _buffer-structs: .. _buffer-structs:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment