| 1 |
.. highlightlang:: c |
| 2 |
|
| 3 |
.. _tupleobjects: |
| 4 |
|
| 5 |
Tuple Objects |
| 6 |
------------- |
| 7 |
|
| 8 |
.. index:: object: tuple |
| 9 |
|
| 10 |
|
| 11 |
.. c:type:: PyTupleObject |
| 12 |
|
| 13 |
This subtype of :c:type:`PyObject` represents a Python tuple object. |
| 14 |
|
| 15 |
|
| 16 |
.. c:var:: PyTypeObject PyTuple_Type |
| 17 |
|
| 18 |
.. index:: single: TupleType (in module types) |
| 19 |
|
| 20 |
This instance of :c:type:`PyTypeObject` represents the Python tuple type; it is |
| 21 |
the same object as ``tuple`` and ``types.TupleType`` in the Python layer.. |
| 22 |
|
| 23 |
|
| 24 |
.. c:function:: int PyTuple_Check(PyObject *p) |
| 25 |
|
| 26 |
Return true if *p* is a tuple object or an instance of a subtype of the tuple |
| 27 |
type. |
| 28 |
|
| 29 |
.. versionchanged:: 2.2 |
| 30 |
Allowed subtypes to be accepted. |
| 31 |
|
| 32 |
|
| 33 |
.. c:function:: int PyTuple_CheckExact(PyObject *p) |
| 34 |
|
| 35 |
Return true if *p* is a tuple object, but not an instance of a subtype of the |
| 36 |
tuple type. |
| 37 |
|
| 38 |
.. versionadded:: 2.2 |
| 39 |
|
| 40 |
|
| 41 |
.. c:function:: PyObject* PyTuple_New(Py_ssize_t len) |
| 42 |
|
| 43 |
Return a new tuple object of size *len*, or *NULL* on failure. |
| 44 |
|
| 45 |
.. versionchanged:: 2.5 |
| 46 |
This function used an :c:type:`int` type for *len*. This might require |
| 47 |
changes in your code for properly supporting 64-bit systems. |
| 48 |
|
| 49 |
|
| 50 |
.. c:function:: PyObject* PyTuple_Pack(Py_ssize_t n, ...) |
| 51 |
|
| 52 |
Return a new tuple object of size *n*, or *NULL* on failure. The tuple values |
| 53 |
are initialized to the subsequent *n* C arguments pointing to Python objects. |
| 54 |
``PyTuple_Pack(2, a, b)`` is equivalent to ``Py_BuildValue("(OO)", a, b)``. |
| 55 |
|
| 56 |
.. versionadded:: 2.4 |
| 57 |
|
| 58 |
.. versionchanged:: 2.5 |
| 59 |
This function used an :c:type:`int` type for *n*. This might require |
| 60 |
changes in your code for properly supporting 64-bit systems. |
| 61 |
|
| 62 |
|
| 63 |
.. c:function:: Py_ssize_t PyTuple_Size(PyObject *p) |
| 64 |
|
| 65 |
Take a pointer to a tuple object, and return the size of that tuple. |
| 66 |
|
| 67 |
.. versionchanged:: 2.5 |
| 68 |
This function returned an :c:type:`int` type. This might require changes |
| 69 |
in your code for properly supporting 64-bit systems. |
| 70 |
|
| 71 |
|
| 72 |
.. c:function:: Py_ssize_t PyTuple_GET_SIZE(PyObject *p) |
| 73 |
|
| 74 |
Return the size of the tuple *p*, which must be non-*NULL* and point to a tuple; |
| 75 |
no error checking is performed. |
| 76 |
|
| 77 |
.. versionchanged:: 2.5 |
| 78 |
This function returned an :c:type:`int` type. This might require changes |
| 79 |
in your code for properly supporting 64-bit systems. |
| 80 |
|
| 81 |
|
| 82 |
.. c:function:: PyObject* PyTuple_GetItem(PyObject *p, Py_ssize_t pos) |
| 83 |
|
| 84 |
Return the object at position *pos* in the tuple pointed to by *p*. If *pos* is |
| 85 |
out of bounds, return *NULL* and set an :exc:`IndexError` exception. |
| 86 |
|
| 87 |
.. versionchanged:: 2.5 |
| 88 |
This function used an :c:type:`int` type for *pos*. This might require |
| 89 |
changes in your code for properly supporting 64-bit systems. |
| 90 |
|
| 91 |
|
| 92 |
.. c:function:: PyObject* PyTuple_GET_ITEM(PyObject *p, Py_ssize_t pos) |
| 93 |
|
| 94 |
Like :c:func:`PyTuple_GetItem`, but does no checking of its arguments. |
| 95 |
|
| 96 |
.. versionchanged:: 2.5 |
| 97 |
This function used an :c:type:`int` type for *pos*. This might require |
| 98 |
changes in your code for properly supporting 64-bit systems. |
| 99 |
|
| 100 |
|
| 101 |
.. c:function:: PyObject* PyTuple_GetSlice(PyObject *p, Py_ssize_t low, Py_ssize_t high) |
| 102 |
|
| 103 |
Return the slice of the tuple pointed to by *p* between *low* and *high*, |
| 104 |
or *NULL* on failure. This is the equivalent of the Python expression |
| 105 |
``p[low:high]``. Indexing from the end of the list is not supported. |
| 106 |
|
| 107 |
.. versionchanged:: 2.5 |
| 108 |
This function used an :c:type:`int` type for *low* and *high*. This might |
| 109 |
require changes in your code for properly supporting 64-bit systems. |
| 110 |
|
| 111 |
|
| 112 |
.. c:function:: int PyTuple_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o) |
| 113 |
|
| 114 |
Insert a reference to object *o* at position *pos* of the tuple pointed to by |
| 115 |
*p*. Return ``0`` on success. If *pos* is out of bounds, return ``-1`` |
| 116 |
and set an :exc:`IndexError` exception. |
| 117 |
|
| 118 |
.. note:: |
| 119 |
|
| 120 |
This function "steals" a reference to *o* and discards a reference to |
| 121 |
an item already in the tuple at the affected position. |
| 122 |
|
| 123 |
.. versionchanged:: 2.5 |
| 124 |
This function used an :c:type:`int` type for *pos*. This might require |
| 125 |
changes in your code for properly supporting 64-bit systems. |
| 126 |
|
| 127 |
|
| 128 |
.. c:function:: void PyTuple_SET_ITEM(PyObject *p, Py_ssize_t pos, PyObject *o) |
| 129 |
|
| 130 |
Like :c:func:`PyTuple_SetItem`, but does no error checking, and should *only* be |
| 131 |
used to fill in brand new tuples. |
| 132 |
|
| 133 |
.. note:: |
| 134 |
|
| 135 |
This macro "steals" a reference to *o*, and, unlike |
| 136 |
:c:func:`PyTuple_SetItem`, does *not* discard a reference to any item that |
| 137 |
is being replaced; any reference in the tuple at position *pos* will be |
| 138 |
leaked. |
| 139 |
|
| 140 |
.. versionchanged:: 2.5 |
| 141 |
This function used an :c:type:`int` type for *pos*. This might require |
| 142 |
changes in your code for properly supporting 64-bit systems. |
| 143 |
|
| 144 |
|
| 145 |
.. c:function:: int _PyTuple_Resize(PyObject **p, Py_ssize_t newsize) |
| 146 |
|
| 147 |
Can be used to resize a tuple. *newsize* will be the new length of the tuple. |
| 148 |
Because tuples are *supposed* to be immutable, this should only be used if there |
| 149 |
is only one reference to the object. Do *not* use this if the tuple may already |
| 150 |
be known to some other part of the code. The tuple will always grow or shrink |
| 151 |
at the end. Think of this as destroying the old tuple and creating a new one, |
| 152 |
only more efficiently. Returns ``0`` on success. Client code should never |
| 153 |
assume that the resulting value of ``*p`` will be the same as before calling |
| 154 |
this function. If the object referenced by ``*p`` is replaced, the original |
| 155 |
``*p`` is destroyed. On failure, returns ``-1`` and sets ``*p`` to *NULL*, and |
| 156 |
raises :exc:`MemoryError` or :exc:`SystemError`. |
| 157 |
|
| 158 |
.. versionchanged:: 2.2 |
| 159 |
Removed unused third parameter, *last_is_sticky*. |
| 160 |
|
| 161 |
.. versionchanged:: 2.5 |
| 162 |
This function used an :c:type:`int` type for *newsize*. This might |
| 163 |
require changes in your code for properly supporting 64-bit systems. |
| 164 |
|
| 165 |
|
| 166 |
.. c:function:: int PyTuple_ClearFreeList() |
| 167 |
|
| 168 |
Clear the free list. Return the total number of freed items. |
| 169 |
|
| 170 |
.. versionadded:: 2.6 |