Debian Codegraph @

Doc/c-api/tuple.rst

raw
1 .. highlight:: c
2
3 .. _tupleobjects:
4
5 Tuple Objects
6 -------------
7
8 .. index:: pair: object; tuple
9
10
11 .. c:type:: PyTupleObject
12
13 This subtype of :c:type:`PyObject` represents a Python tuple object.
14
15
16 .. c:var:: PyTypeObject PyTuple_Type
17
18 This instance of :c:type:`PyTypeObject` represents the Python tuple type; it
19 is the same object as :class:`tuple` in the Python layer.
20
21
22 .. c:function:: int PyTuple_Check(PyObject *p)
23
24 Return true if *p* is a tuple object or an instance of a subtype of the
25 tuple type. This function always succeeds.
26
27
28 .. c:function:: 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. This function always succeeds.
32
33
34 .. c:function:: PyObject* PyTuple_New(Py_ssize_t len)
35
36 Return a new tuple object of size *len*,
37 or ``NULL`` with an exception set on failure.
38
39
40 .. c:function:: PyObject* PyTuple_Pack(Py_ssize_t n, ...)
41
42 Return a new tuple object of size *n*,
43 or ``NULL`` with an exception set 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
48 .. c:function:: Py_ssize_t PyTuple_Size(PyObject *p)
49
50 Take a pointer to a tuple object, and return the size of that tuple.
51 On error, return ``-1`` with an exception set.
52
53
54 .. c:function:: Py_ssize_t PyTuple_GET_SIZE(PyObject *p)
55
56 Like :c:func:`PyTuple_Size`, but without error checking.
57
58
59 .. c:function:: PyObject* PyTuple_GetItem(PyObject *p, Py_ssize_t pos)
60
61 Return the object at position *pos* in the tuple pointed to by *p*. If *pos* is
62 negative or out of bounds, return ``NULL`` and set an :exc:`IndexError` exception.
63
64 The returned reference is borrowed from the tuple *p*
65 (that is: it is only valid as long as you hold a reference to *p*).
66 To get a :term:`strong reference`, use
67 :c:func:`Py_NewRef(PyTuple_GetItem(...)) <Py_NewRef>`
68 or :c:func:`PySequence_GetItem`.
69
70
71 .. c:function:: PyObject* PyTuple_GET_ITEM(PyObject *p, Py_ssize_t pos)
72
73 Like :c:func:`PyTuple_GetItem`, but does no checking of its arguments.
74
75
76 .. c:function:: PyObject* PyTuple_GetSlice(PyObject *p, Py_ssize_t low, Py_ssize_t high)
77
78 Return the slice of the tuple pointed to by *p* between *low* and *high*,
79 or ``NULL`` with an exception set on failure.
80
81 This is the equivalent of the Python expression ``p[low:high]``.
82 Indexing from the end of the tuple is not supported.
83
84
85 .. c:function:: int PyTuple_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)
86
87 Insert a reference to object *o* at position *pos* of the tuple pointed to by
88 *p*. Return ``0`` on success. If *pos* is out of bounds, return ``-1``
89 and set an :exc:`IndexError` exception.
90
91 .. note::
92
93 This function "steals" a reference to *o* and discards a reference to
94 an item already in the tuple at the affected position.
95
96
97 .. c:function:: void PyTuple_SET_ITEM(PyObject *p, Py_ssize_t pos, PyObject *o)
98
99 Like :c:func:`PyTuple_SetItem`, but does no error checking, and should *only* be
100 used to fill in brand new tuples.
101
102 Bounds checking is performed as an assertion if Python is built in
103 :ref:`debug mode <debug-build>` or :option:`with assertions <--with-assertions>`.
104
105 .. note::
106
107 This function "steals" a reference to *o*, and, unlike
108 :c:func:`PyTuple_SetItem`, does *not* discard a reference to any item that
109 is being replaced; any reference in the tuple at position *pos* will be
110 leaked.
111
112
113 .. c:function:: int _PyTuple_Resize(PyObject **p, Py_ssize_t newsize)
114
115 Can be used to resize a tuple. *newsize* will be the new length of the tuple.
116 Because tuples are *supposed* to be immutable, this should only be used if there
117 is only one reference to the object. Do *not* use this if the tuple may already
118 be known to some other part of the code. The tuple will always grow or shrink
119 at the end. Think of this as destroying the old tuple and creating a new one,
120 only more efficiently. Returns ``0`` on success. Client code should never
121 assume that the resulting value of ``*p`` will be the same as before calling
122 this function. If the object referenced by ``*p`` is replaced, the original
123 ``*p`` is destroyed. On failure, returns ``-1`` and sets ``*p`` to ``NULL``, and
124 raises :exc:`MemoryError` or :exc:`SystemError`.
125
126
127 .. _struct-sequence-objects:
128
129 Struct Sequence Objects
130 -----------------------
131
132 Struct sequence objects are the C equivalent of :func:`~collections.namedtuple`
133 objects, i.e. a sequence whose items can also be accessed through attributes.
134 To create a struct sequence, you first have to create a specific struct sequence
135 type.
136
137 .. c:function:: PyTypeObject* PyStructSequence_NewType(PyStructSequence_Desc *desc)
138
139 Create a new struct sequence type from the data in *desc*, described below. Instances
140 of the resulting type can be created with :c:func:`PyStructSequence_New`.
141
142 Return ``NULL`` with an exception set on failure.
143
144
145 .. c:function:: void PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc)
146
147 Initializes a struct sequence type *type* from *desc* in place.
148
149
150 .. c:function:: int PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc)
151
152 Like :c:func:`PyStructSequence_InitType`, but returns ``0`` on success
153 and ``-1`` with an exception set on failure.
154
155 .. versionadded:: 3.4
156
157
158 .. c:type:: PyStructSequence_Desc
159
160 Contains the meta information of a struct sequence type to create.
161
162 .. c:member:: const char *name
163
164 Fully qualified name of the type; null-terminated UTF-8 encoded.
165 The name must contain the module name.
166
167 .. c:member:: const char *doc
168
169 Pointer to docstring for the type or ``NULL`` to omit.
170
171 .. c:member:: PyStructSequence_Field *fields
172
173 Pointer to ``NULL``-terminated array with field names of the new type.
174
175 .. c:member:: int n_in_sequence
176
177 Number of fields visible to the Python side (if used as tuple).
178
179
180 .. c:type:: PyStructSequence_Field
181
182 Describes a field of a struct sequence. As a struct sequence is modeled as a
183 tuple, all fields are typed as :c:expr:`PyObject*`. The index in the
184 :c:member:`~PyStructSequence_Desc.fields` array of
185 the :c:type:`PyStructSequence_Desc` determines which
186 field of the struct sequence is described.
187
188 .. c:member:: const char *name
189
190 Name for the field or ``NULL`` to end the list of named fields,
191 set to :c:data:`PyStructSequence_UnnamedField` to leave unnamed.
192
193 .. c:member:: const char *doc
194
195 Field docstring or ``NULL`` to omit.
196
197
198 .. c:var:: const char * const PyStructSequence_UnnamedField
199
200 Special value for a field name to leave it unnamed.
201
202 .. versionchanged:: 3.9
203 The type was changed from ``char *``.
204
205
206 .. c:function:: PyObject* PyStructSequence_New(PyTypeObject *type)
207
208 Creates an instance of *type*, which must have been created with
209 :c:func:`PyStructSequence_NewType`.
210
211 Return ``NULL`` with an exception set on failure.
212
213
214 .. c:function:: PyObject* PyStructSequence_GetItem(PyObject *p, Py_ssize_t pos)
215
216 Return the object at position *pos* in the struct sequence pointed to by *p*.
217
218 Bounds checking is performed as an assertion if Python is built in
219 :ref:`debug mode <debug-build>` or :option:`with assertions <--with-assertions>`.
220
221
222 .. c:function:: PyObject* PyStructSequence_GET_ITEM(PyObject *p, Py_ssize_t pos)
223
224 Alias to :c:func:`PyStructSequence_GetItem`.
225
226 .. versionchanged:: 3.13
227 Now implemented as an alias to :c:func:`PyStructSequence_GetItem`.
228
229
230 .. c:function:: void PyStructSequence_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)
231
232 Sets the field at index *pos* of the struct sequence *p* to value *o*. Like
233 :c:func:`PyTuple_SET_ITEM`, this should only be used to fill in brand new
234 instances.
235
236 Bounds checking is performed as an assertion if Python is built in
237 :ref:`debug mode <debug-build>` or :option:`with assertions <--with-assertions>`.
238
239 .. note::
240
241 This function "steals" a reference to *o*.
242
243
244 .. c:function:: void PyStructSequence_SET_ITEM(PyObject *p, Py_ssize_t *pos, PyObject *o)
245
246 Alias to :c:func:`PyStructSequence_SetItem`.
247
248 .. versionchanged:: 3.13
249 Now implemented as an alias to :c:func:`PyStructSequence_SetItem`.