blob: 16de45fb31c4316067f2ddbdbea0db88b1225bb7 [file] [log] [blame]
Georg Brandlf6842722008-01-19 22:08:21 +00001.. highlightlang:: c
2
3.. _tupleobjects:
4
5Tuple Objects
6-------------
7
8.. index:: object: tuple
9
10
Sandro Tosi98ed08f2012-01-14 16:42:02 +010011.. c:type:: PyTupleObject
Georg Brandlf6842722008-01-19 22:08:21 +000012
Sandro Tosi98ed08f2012-01-14 16:42:02 +010013 This subtype of :c:type:`PyObject` represents a Python tuple object.
Georg Brandlf6842722008-01-19 22:08:21 +000014
15
Sandro Tosi98ed08f2012-01-14 16:42:02 +010016.. c:var:: PyTypeObject PyTuple_Type
Georg Brandlf6842722008-01-19 22:08:21 +000017
18 .. index:: single: TupleType (in module types)
19
Sandro Tosi98ed08f2012-01-14 16:42:02 +010020 This instance of :c:type:`PyTypeObject` represents the Python tuple type; it is
Georg Brandlf6842722008-01-19 22:08:21 +000021 the same object as ``tuple`` and ``types.TupleType`` in the Python layer..
22
23
Sandro Tosi98ed08f2012-01-14 16:42:02 +010024.. c:function:: int PyTuple_Check(PyObject *p)
Georg Brandlf6842722008-01-19 22:08:21 +000025
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
Sandro Tosi98ed08f2012-01-14 16:42:02 +010033.. c:function:: int PyTuple_CheckExact(PyObject *p)
Georg Brandlf6842722008-01-19 22:08:21 +000034
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
Sandro Tosi98ed08f2012-01-14 16:42:02 +010041.. c:function:: PyObject* PyTuple_New(Py_ssize_t len)
Georg Brandlf6842722008-01-19 22:08:21 +000042
43 Return a new tuple object of size *len*, or *NULL* on failure.
44
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000045 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +010046 This function used an :c:type:`int` type for *len*. This might require
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000047 changes in your code for properly supporting 64-bit systems.
48
Georg Brandlf6842722008-01-19 22:08:21 +000049
Sandro Tosi98ed08f2012-01-14 16:42:02 +010050.. c:function:: PyObject* PyTuple_Pack(Py_ssize_t n, ...)
Georg Brandlf6842722008-01-19 22:08:21 +000051
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
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000058 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +010059 This function used an :c:type:`int` type for *n*. This might require
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000060 changes in your code for properly supporting 64-bit systems.
61
Georg Brandlf6842722008-01-19 22:08:21 +000062
Sandro Tosi98ed08f2012-01-14 16:42:02 +010063.. c:function:: Py_ssize_t PyTuple_Size(PyObject *p)
Georg Brandlf6842722008-01-19 22:08:21 +000064
65 Take a pointer to a tuple object, and return the size of that tuple.
66
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000067 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +010068 This function returned an :c:type:`int` type. This might require changes
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000069 in your code for properly supporting 64-bit systems.
70
Georg Brandlf6842722008-01-19 22:08:21 +000071
Sandro Tosi98ed08f2012-01-14 16:42:02 +010072.. c:function:: Py_ssize_t PyTuple_GET_SIZE(PyObject *p)
Georg Brandlf6842722008-01-19 22:08:21 +000073
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
Jeroen Ruigrok van der Wervenacc73b72009-04-25 20:58:35 +000077 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +010078 This function returned an :c:type:`int` type. This might require changes
Jeroen Ruigrok van der Wervenacc73b72009-04-25 20:58:35 +000079 in your code for properly supporting 64-bit systems.
80
Georg Brandlf6842722008-01-19 22:08:21 +000081
Sandro Tosi98ed08f2012-01-14 16:42:02 +010082.. c:function:: PyObject* PyTuple_GetItem(PyObject *p, Py_ssize_t pos)
Georg Brandlf6842722008-01-19 22:08:21 +000083
84 Return the object at position *pos* in the tuple pointed to by *p*. If *pos* is
85 out of bounds, return *NULL* and sets an :exc:`IndexError` exception.
86
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000087 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +010088 This function used an :c:type:`int` type for *pos*. This might require
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000089 changes in your code for properly supporting 64-bit systems.
90
Georg Brandlf6842722008-01-19 22:08:21 +000091
Sandro Tosi98ed08f2012-01-14 16:42:02 +010092.. c:function:: PyObject* PyTuple_GET_ITEM(PyObject *p, Py_ssize_t pos)
Georg Brandlf6842722008-01-19 22:08:21 +000093
Sandro Tosi98ed08f2012-01-14 16:42:02 +010094 Like :c:func:`PyTuple_GetItem`, but does no checking of its arguments.
Georg Brandlf6842722008-01-19 22:08:21 +000095
Jeroen Ruigrok van der Wervenacc73b72009-04-25 20:58:35 +000096 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +010097 This function used an :c:type:`int` type for *pos*. This might require
Jeroen Ruigrok van der Wervenacc73b72009-04-25 20:58:35 +000098 changes in your code for properly supporting 64-bit systems.
99
Georg Brandlf6842722008-01-19 22:08:21 +0000100
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100101.. c:function:: PyObject* PyTuple_GetSlice(PyObject *p, Py_ssize_t low, Py_ssize_t high)
Georg Brandlf6842722008-01-19 22:08:21 +0000102
103 Take a slice of the tuple pointed to by *p* from *low* to *high* and return it
104 as a new tuple.
105
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000106 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100107 This function used an :c:type:`int` type for *low* and *high*. This might
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000108 require changes in your code for properly supporting 64-bit systems.
109
Georg Brandlf6842722008-01-19 22:08:21 +0000110
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100111.. c:function:: int PyTuple_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000112
113 Insert a reference to object *o* at position *pos* of the tuple pointed to by
114 *p*. Return ``0`` on success.
115
116 .. note::
117
118 This function "steals" a reference to *o*.
119
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000120 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100121 This function used an :c:type:`int` type for *pos*. This might require
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000122 changes in your code for properly supporting 64-bit systems.
123
Georg Brandlf6842722008-01-19 22:08:21 +0000124
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100125.. c:function:: void PyTuple_SET_ITEM(PyObject *p, Py_ssize_t pos, PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000126
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100127 Like :c:func:`PyTuple_SetItem`, but does no error checking, and should *only* be
Georg Brandlf6842722008-01-19 22:08:21 +0000128 used to fill in brand new tuples.
129
130 .. note::
131
132 This function "steals" a reference to *o*.
133
Jeroen Ruigrok van der Wervenacc73b72009-04-25 20:58:35 +0000134 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100135 This function used an :c:type:`int` type for *pos*. This might require
Jeroen Ruigrok van der Wervenacc73b72009-04-25 20:58:35 +0000136 changes in your code for properly supporting 64-bit systems.
137
Georg Brandlf6842722008-01-19 22:08:21 +0000138
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100139.. c:function:: int _PyTuple_Resize(PyObject **p, Py_ssize_t newsize)
Georg Brandlf6842722008-01-19 22:08:21 +0000140
141 Can be used to resize a tuple. *newsize* will be the new length of the tuple.
142 Because tuples are *supposed* to be immutable, this should only be used if there
143 is only one reference to the object. Do *not* use this if the tuple may already
144 be known to some other part of the code. The tuple will always grow or shrink
145 at the end. Think of this as destroying the old tuple and creating a new one,
146 only more efficiently. Returns ``0`` on success. Client code should never
147 assume that the resulting value of ``*p`` will be the same as before calling
148 this function. If the object referenced by ``*p`` is replaced, the original
149 ``*p`` is destroyed. On failure, returns ``-1`` and sets ``*p`` to *NULL*, and
150 raises :exc:`MemoryError` or :exc:`SystemError`.
151
152 .. versionchanged:: 2.2
153 Removed unused third parameter, *last_is_sticky*.
Christian Heimes3b718a72008-02-14 12:47:33 +0000154
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000155 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100156 This function used an :c:type:`int` type for *newsize*. This might
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000157 require changes in your code for properly supporting 64-bit systems.
158
Christian Heimes3b718a72008-02-14 12:47:33 +0000159
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100160.. c:function:: int PyTuple_ClearFreeList()
Christian Heimes3b718a72008-02-14 12:47:33 +0000161
162 Clear the free list. Return the total number of freed items.
163
164 .. versionadded:: 2.6