blob: 77e6e8b588c2ab6b785b1d9d7959d28b57d0e3ec [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
11.. ctype:: PyTupleObject
12
13 This subtype of :ctype:`PyObject` represents a Python tuple object.
14
15
16.. cvar:: PyTypeObject PyTuple_Type
17
18 .. index:: single: TupleType (in module types)
19
20 This instance of :ctype:`PyTypeObject` represents the Python tuple type; it is
21 the same object as ``tuple`` and ``types.TupleType`` in the Python layer..
22
23
24.. cfunction:: 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
30.. cfunction:: int PyTuple_CheckExact(PyObject *p)
31
32 Return true if *p* is a tuple object, but not an instance of a subtype of the
33 tuple type.
34
35
36.. cfunction:: PyObject* PyTuple_New(Py_ssize_t len)
37
38 Return a new tuple object of size *len*, or *NULL* on failure.
39
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000040 .. versionchanged:: 2.5
41 This function used an :ctype:`int` type for *len*. This might require
42 changes in your code for properly supporting 64-bit systems.
43
Georg Brandl54a3faa2008-01-20 09:30:57 +000044
45.. cfunction:: PyObject* PyTuple_Pack(Py_ssize_t n, ...)
46
47 Return a new tuple object of size *n*, or *NULL* on failure. The tuple values
48 are initialized to the subsequent *n* C arguments pointing to Python objects.
49 ``PyTuple_Pack(2, a, b)`` is equivalent to ``Py_BuildValue("(OO)", a, b)``.
50
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000051 .. versionchanged:: 2.5
52 This function used an :ctype:`int` type for *n*. This might require
53 changes in your code for properly supporting 64-bit systems.
54
Georg Brandl54a3faa2008-01-20 09:30:57 +000055
56.. cfunction:: Py_ssize_t PyTuple_Size(PyObject *p)
57
58 Take a pointer to a tuple object, and return the size of that tuple.
59
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000060 .. versionchanged:: 2.5
61 This function returned an :ctype:`int` type. This might require changes
62 in your code for properly supporting 64-bit systems.
63
Georg Brandl54a3faa2008-01-20 09:30:57 +000064
65.. cfunction:: Py_ssize_t PyTuple_GET_SIZE(PyObject *p)
66
67 Return the size of the tuple *p*, which must be non-*NULL* and point to a tuple;
68 no error checking is performed.
69
70
71.. cfunction:: PyObject* PyTuple_GetItem(PyObject *p, Py_ssize_t pos)
72
73 Return the object at position *pos* in the tuple pointed to by *p*. If *pos* is
74 out of bounds, return *NULL* and sets an :exc:`IndexError` exception.
75
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000076 .. versionchanged:: 2.5
77 This function used an :ctype:`int` type for *pos*. This might require
78 changes in your code for properly supporting 64-bit systems.
79
Georg Brandl54a3faa2008-01-20 09:30:57 +000080
81.. cfunction:: PyObject* PyTuple_GET_ITEM(PyObject *p, Py_ssize_t pos)
82
83 Like :cfunc:`PyTuple_GetItem`, but does no checking of its arguments.
84
85
86.. cfunction:: PyObject* PyTuple_GetSlice(PyObject *p, Py_ssize_t low, Py_ssize_t high)
87
88 Take a slice of the tuple pointed to by *p* from *low* to *high* and return it
89 as a new tuple.
90
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000091 .. versionchanged:: 2.5
92 This function used an :ctype:`int` type for *low* and *high*. This might
93 require changes in your code for properly supporting 64-bit systems.
94
Georg Brandl54a3faa2008-01-20 09:30:57 +000095
96.. cfunction:: int PyTuple_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)
97
98 Insert a reference to object *o* at position *pos* of the tuple pointed to by
99 *p*. Return ``0`` on success.
100
101 .. note::
102
103 This function "steals" a reference to *o*.
104
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000105 .. versionchanged:: 2.5
106 This function used an :ctype:`int` type for *pos*. This might require
107 changes in your code for properly supporting 64-bit systems.
108
Georg Brandl54a3faa2008-01-20 09:30:57 +0000109
110.. cfunction:: void PyTuple_SET_ITEM(PyObject *p, Py_ssize_t pos, PyObject *o)
111
112 Like :cfunc:`PyTuple_SetItem`, but does no error checking, and should *only* be
113 used to fill in brand new tuples.
114
115 .. note::
116
117 This function "steals" a reference to *o*.
118
119
120.. cfunction:: int _PyTuple_Resize(PyObject **p, Py_ssize_t newsize)
121
122 Can be used to resize a tuple. *newsize* will be the new length of the tuple.
123 Because tuples are *supposed* to be immutable, this should only be used if there
124 is only one reference to the object. Do *not* use this if the tuple may already
125 be known to some other part of the code. The tuple will always grow or shrink
126 at the end. Think of this as destroying the old tuple and creating a new one,
127 only more efficiently. Returns ``0`` on success. Client code should never
128 assume that the resulting value of ``*p`` will be the same as before calling
129 this function. If the object referenced by ``*p`` is replaced, the original
130 ``*p`` is destroyed. On failure, returns ``-1`` and sets ``*p`` to *NULL*, and
131 raises :exc:`MemoryError` or :exc:`SystemError`.
Christian Heimesa156e092008-02-16 07:38:31 +0000132
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000133 .. versionchanged:: 2.5
134 This function used an :ctype:`int` type for *newsize*. This might
135 require changes in your code for properly supporting 64-bit systems.
136
137
Christian Heimes7131fd92008-02-19 14:21:46 +0000138.. cfunction:: int PyTuple_ClearFreeList(void)
Christian Heimesa156e092008-02-16 07:38:31 +0000139
140 Clear the free list. Return the total number of freed items.