blob: 612acc754e30288c2900f60ab263470f9cc7ac01 [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
Georg Brandlab32fec2010-11-26 08:49:15 +000018 This instance of :ctype:`PyTypeObject` represents the Python tuple type; it
19 is the same object as :class:`tuple` in the Python layer.
Georg Brandl54a3faa2008-01-20 09:30:57 +000020
21
22.. cfunction:: int PyTuple_Check(PyObject *p)
23
24 Return true if *p* is a tuple object or an instance of a subtype of the tuple
25 type.
26
27
28.. cfunction:: int PyTuple_CheckExact(PyObject *p)
29
30 Return true if *p* is a tuple object, but not an instance of a subtype of the
31 tuple type.
32
33
34.. cfunction:: PyObject* PyTuple_New(Py_ssize_t len)
35
36 Return a new tuple object of size *len*, or *NULL* on failure.
37
38
39.. cfunction:: PyObject* PyTuple_Pack(Py_ssize_t n, ...)
40
41 Return a new tuple object of size *n*, or *NULL* on failure. The tuple values
42 are initialized to the subsequent *n* C arguments pointing to Python objects.
43 ``PyTuple_Pack(2, a, b)`` is equivalent to ``Py_BuildValue("(OO)", a, b)``.
44
45
46.. cfunction:: Py_ssize_t PyTuple_Size(PyObject *p)
47
48 Take a pointer to a tuple object, and return the size of that tuple.
49
50
51.. cfunction:: Py_ssize_t PyTuple_GET_SIZE(PyObject *p)
52
53 Return the size of the tuple *p*, which must be non-*NULL* and point to a tuple;
54 no error checking is performed.
55
56
57.. cfunction:: PyObject* PyTuple_GetItem(PyObject *p, Py_ssize_t pos)
58
59 Return the object at position *pos* in the tuple pointed to by *p*. If *pos* is
60 out of bounds, return *NULL* and sets an :exc:`IndexError` exception.
61
62
63.. cfunction:: PyObject* PyTuple_GET_ITEM(PyObject *p, Py_ssize_t pos)
64
65 Like :cfunc:`PyTuple_GetItem`, but does no checking of its arguments.
66
67
68.. cfunction:: PyObject* PyTuple_GetSlice(PyObject *p, Py_ssize_t low, Py_ssize_t high)
69
70 Take a slice of the tuple pointed to by *p* from *low* to *high* and return it
71 as a new tuple.
72
73
74.. cfunction:: int PyTuple_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)
75
76 Insert a reference to object *o* at position *pos* of the tuple pointed to by
77 *p*. Return ``0`` on success.
78
79 .. note::
80
81 This function "steals" a reference to *o*.
82
83
84.. cfunction:: void PyTuple_SET_ITEM(PyObject *p, Py_ssize_t pos, PyObject *o)
85
86 Like :cfunc:`PyTuple_SetItem`, but does no error checking, and should *only* be
87 used to fill in brand new tuples.
88
89 .. note::
90
91 This function "steals" a reference to *o*.
92
93
94.. cfunction:: int _PyTuple_Resize(PyObject **p, Py_ssize_t newsize)
95
96 Can be used to resize a tuple. *newsize* will be the new length of the tuple.
97 Because tuples are *supposed* to be immutable, this should only be used if there
98 is only one reference to the object. Do *not* use this if the tuple may already
99 be known to some other part of the code. The tuple will always grow or shrink
100 at the end. Think of this as destroying the old tuple and creating a new one,
101 only more efficiently. Returns ``0`` on success. Client code should never
102 assume that the resulting value of ``*p`` will be the same as before calling
103 this function. If the object referenced by ``*p`` is replaced, the original
104 ``*p`` is destroyed. On failure, returns ``-1`` and sets ``*p`` to *NULL*, and
105 raises :exc:`MemoryError` or :exc:`SystemError`.
Christian Heimesa156e092008-02-16 07:38:31 +0000106
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000107
Georg Brandlc5605df2009-08-13 08:26:44 +0000108.. cfunction:: int PyTuple_ClearFreeList()
Christian Heimesa156e092008-02-16 07:38:31 +0000109
110 Clear the free list. Return the total number of freed items.