8
.. index:: object: tuple
11
.. ctype:: PyTupleObject
13
This subtype of :ctype:`PyObject` represents a Python tuple object.
16
.. cvar:: PyTypeObject PyTuple_Type
18
.. index:: single: TupleType (in module types)
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..
24
.. cfunction:: int PyTuple_Check(PyObject *p)
26
Return true if *p* is a tuple object or an instance of a subtype of the tuple
30
.. cfunction:: int PyTuple_CheckExact(PyObject *p)
32
Return true if *p* is a tuple object, but not an instance of a subtype of the
36
.. cfunction:: PyObject* PyTuple_New(Py_ssize_t len)
38
Return a new tuple object of size *len*, or *NULL* on failure.
41
.. cfunction:: PyObject* PyTuple_Pack(Py_ssize_t n, ...)
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)``.
48
.. cfunction:: Py_ssize_t PyTuple_Size(PyObject *p)
50
Take a pointer to a tuple object, and return the size of that tuple.
53
.. cfunction:: Py_ssize_t PyTuple_GET_SIZE(PyObject *p)
55
Return the size of the tuple *p*, which must be non-*NULL* and point to a tuple;
56
no error checking is performed.
59
.. cfunction:: PyObject* PyTuple_GetItem(PyObject *p, Py_ssize_t pos)
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.
65
.. cfunction:: PyObject* PyTuple_GET_ITEM(PyObject *p, Py_ssize_t pos)
67
Like :cfunc:`PyTuple_GetItem`, but does no checking of its arguments.
70
.. cfunction:: PyObject* PyTuple_GetSlice(PyObject *p, Py_ssize_t low, Py_ssize_t high)
72
Take a slice of the tuple pointed to by *p* from *low* to *high* and return it
76
.. cfunction:: int PyTuple_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)
78
Insert a reference to object *o* at position *pos* of the tuple pointed to by
79
*p*. Return ``0`` on success.
83
This function "steals" a reference to *o*.
86
.. cfunction:: void PyTuple_SET_ITEM(PyObject *p, Py_ssize_t pos, PyObject *o)
88
Like :cfunc:`PyTuple_SetItem`, but does no error checking, and should *only* be
89
used to fill in brand new tuples.
93
This function "steals" a reference to *o*.
96
.. cfunction:: int _PyTuple_Resize(PyObject **p, Py_ssize_t newsize)
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`.
109
.. cfunction:: int PyTuple_ClearFreeList(void)
111
Clear the free list. Return the total number of freed items.