blob: 7f6a79c296c2254fd918c6086a0f3f5d2aa196c1 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _tupleobjects:
4
5Tuple Objects
6-------------
7
8.. index:: object: tuple
9
10
Georg Brandl60203b42010-10-06 10:11:56 +000011.. c:type:: PyTupleObject
Georg Brandl54a3faa2008-01-20 09:30:57 +000012
Georg Brandl60203b42010-10-06 10:11:56 +000013 This subtype of :c:type:`PyObject` represents a Python tuple object.
Georg Brandl54a3faa2008-01-20 09:30:57 +000014
15
Georg Brandl60203b42010-10-06 10:11:56 +000016.. c:var:: PyTypeObject PyTuple_Type
Georg Brandl54a3faa2008-01-20 09:30:57 +000017
18 .. index:: single: TupleType (in module types)
19
Georg Brandl60203b42010-10-06 10:11:56 +000020 This instance of :c:type:`PyTypeObject` represents the Python tuple type; it is
Georg Brandl54a3faa2008-01-20 09:30:57 +000021 the same object as ``tuple`` and ``types.TupleType`` in the Python layer..
22
23
Georg Brandl60203b42010-10-06 10:11:56 +000024.. c:function:: int PyTuple_Check(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000025
26 Return true if *p* is a tuple object or an instance of a subtype of the tuple
27 type.
28
29
Georg Brandl60203b42010-10-06 10:11:56 +000030.. c:function:: int PyTuple_CheckExact(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000031
32 Return true if *p* is a tuple object, but not an instance of a subtype of the
33 tuple type.
34
35
Georg Brandl60203b42010-10-06 10:11:56 +000036.. c:function:: PyObject* PyTuple_New(Py_ssize_t len)
Georg Brandl54a3faa2008-01-20 09:30:57 +000037
38 Return a new tuple object of size *len*, or *NULL* on failure.
39
40
Georg Brandl60203b42010-10-06 10:11:56 +000041.. c:function:: PyObject* PyTuple_Pack(Py_ssize_t n, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +000042
43 Return a new tuple object of size *n*, or *NULL* on failure. The tuple values
44 are initialized to the subsequent *n* C arguments pointing to Python objects.
45 ``PyTuple_Pack(2, a, b)`` is equivalent to ``Py_BuildValue("(OO)", a, b)``.
46
47
Georg Brandl60203b42010-10-06 10:11:56 +000048.. c:function:: Py_ssize_t PyTuple_Size(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000049
50 Take a pointer to a tuple object, and return the size of that tuple.
51
52
Georg Brandl60203b42010-10-06 10:11:56 +000053.. c:function:: Py_ssize_t PyTuple_GET_SIZE(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000054
55 Return the size of the tuple *p*, which must be non-*NULL* and point to a tuple;
56 no error checking is performed.
57
58
Georg Brandl60203b42010-10-06 10:11:56 +000059.. c:function:: PyObject* PyTuple_GetItem(PyObject *p, Py_ssize_t pos)
Georg Brandl54a3faa2008-01-20 09:30:57 +000060
61 Return the object at position *pos* in the tuple pointed to by *p*. If *pos* is
62 out of bounds, return *NULL* and sets an :exc:`IndexError` exception.
63
64
Georg Brandl60203b42010-10-06 10:11:56 +000065.. c:function:: PyObject* PyTuple_GET_ITEM(PyObject *p, Py_ssize_t pos)
Georg Brandl54a3faa2008-01-20 09:30:57 +000066
Georg Brandl60203b42010-10-06 10:11:56 +000067 Like :c:func:`PyTuple_GetItem`, but does no checking of its arguments.
Georg Brandl54a3faa2008-01-20 09:30:57 +000068
69
Georg Brandl60203b42010-10-06 10:11:56 +000070.. c:function:: PyObject* PyTuple_GetSlice(PyObject *p, Py_ssize_t low, Py_ssize_t high)
Georg Brandl54a3faa2008-01-20 09:30:57 +000071
72 Take a slice of the tuple pointed to by *p* from *low* to *high* and return it
73 as a new tuple.
74
75
Georg Brandl60203b42010-10-06 10:11:56 +000076.. c:function:: int PyTuple_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000077
78 Insert a reference to object *o* at position *pos* of the tuple pointed to by
79 *p*. Return ``0`` on success.
80
81 .. note::
82
83 This function "steals" a reference to *o*.
84
85
Georg Brandl60203b42010-10-06 10:11:56 +000086.. c:function:: void PyTuple_SET_ITEM(PyObject *p, Py_ssize_t pos, PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000087
Georg Brandl60203b42010-10-06 10:11:56 +000088 Like :c:func:`PyTuple_SetItem`, but does no error checking, and should *only* be
Georg Brandl54a3faa2008-01-20 09:30:57 +000089 used to fill in brand new tuples.
90
91 .. note::
92
93 This function "steals" a reference to *o*.
94
95
Georg Brandl60203b42010-10-06 10:11:56 +000096.. c:function:: int _PyTuple_Resize(PyObject **p, Py_ssize_t newsize)
Georg Brandl54a3faa2008-01-20 09:30:57 +000097
98 Can be used to resize a tuple. *newsize* will be the new length of the tuple.
99 Because tuples are *supposed* to be immutable, this should only be used if there
100 is only one reference to the object. Do *not* use this if the tuple may already
101 be known to some other part of the code. The tuple will always grow or shrink
102 at the end. Think of this as destroying the old tuple and creating a new one,
103 only more efficiently. Returns ``0`` on success. Client code should never
104 assume that the resulting value of ``*p`` will be the same as before calling
105 this function. If the object referenced by ``*p`` is replaced, the original
106 ``*p`` is destroyed. On failure, returns ``-1`` and sets ``*p`` to *NULL*, and
107 raises :exc:`MemoryError` or :exc:`SystemError`.
Christian Heimesa156e092008-02-16 07:38:31 +0000108
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000109
Georg Brandl60203b42010-10-06 10:11:56 +0000110.. c:function:: int PyTuple_ClearFreeList()
Christian Heimesa156e092008-02-16 07:38:31 +0000111
112 Clear the free list. Return the total number of freed items.