/src/cpython/Objects/tupleobject.c
Line | Count | Source |
1 | | /* Tuple object implementation */ |
2 | | |
3 | | #include "Python.h" |
4 | | #include "pycore_abstract.h" // _PyIndex_Check() |
5 | | #include "pycore_ceval.h" // _PyEval_GetBuiltin() |
6 | | #include "pycore_freelist.h" // _Py_FREELIST_PUSH() |
7 | | #include "pycore_gc.h" // _PyObject_GC_IS_TRACKED() |
8 | | #include "pycore_list.h" // _Py_memory_repeat() |
9 | | #include "pycore_modsupport.h" // _PyArg_NoKwnames() |
10 | | #include "pycore_object.h" // _PyObject_GC_TRACK() |
11 | | #include "pycore_pyerrors.h" // _PyException_AddNote() |
12 | | #include "pycore_stackref.h" // PyStackRef_AsPyObjectSteal() |
13 | | #include "pycore_tuple.h" // _PyTupleIterObject |
14 | | |
15 | | |
16 | | /*[clinic input] |
17 | | class tuple "PyTupleObject *" "&PyTuple_Type" |
18 | | [clinic start generated code]*/ |
19 | | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=f051ba3cfdf9a189]*/ |
20 | | |
21 | | #include "clinic/tupleobject.c.h" |
22 | | |
23 | | |
24 | | static inline int maybe_freelist_push(PyTupleObject *); |
25 | | |
26 | | |
27 | | /* Allocate an uninitialized tuple object. Before making it public, following |
28 | | steps must be done: |
29 | | |
30 | | - Initialize its items. |
31 | | - Call _PyObject_GC_TRACK() on it. |
32 | | |
33 | | Because the empty tuple is always reused and it's already tracked by GC, |
34 | | this function must not be called with size == 0 (unless from PyTuple_New() |
35 | | which wraps this function). |
36 | | */ |
37 | | static PyTupleObject * |
38 | | tuple_alloc(Py_ssize_t size) |
39 | 562M | { |
40 | 562M | if (size < 0) { |
41 | 0 | PyErr_BadInternalCall(); |
42 | 0 | return NULL; |
43 | 0 | } |
44 | 562M | assert(size != 0); // The empty tuple is statically allocated. |
45 | 562M | Py_ssize_t index = size - 1; |
46 | 562M | if (index < PyTuple_MAXSAVESIZE) { |
47 | 537M | PyTupleObject *op = _Py_FREELIST_POP(PyTupleObject, tuples[index]); |
48 | 537M | if (op != NULL) { |
49 | 411M | _PyTuple_RESET_HASH_CACHE(op); |
50 | 411M | return op; |
51 | 411M | } |
52 | 537M | } |
53 | 151M | PyTupleObject *result = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size); |
54 | 151M | if (result != NULL) { |
55 | 151M | _PyTuple_RESET_HASH_CACHE(result); |
56 | 151M | } |
57 | 151M | return result; |
58 | 562M | } |
59 | | |
60 | | // The empty tuple singleton is not tracked by the GC. |
61 | | // It does not contain any Python object. |
62 | | // Note that tuple subclasses have their own empty instances. |
63 | | |
64 | | static inline PyObject * |
65 | | tuple_get_empty(void) |
66 | 80.0M | { |
67 | 80.0M | return (PyObject *)&_Py_SINGLETON(tuple_empty); |
68 | 80.0M | } |
69 | | |
70 | | PyObject * |
71 | | PyTuple_New(Py_ssize_t size) |
72 | 71.5M | { |
73 | 71.5M | PyTupleObject *op; |
74 | 71.5M | if (size == 0) { |
75 | 49.4k | return tuple_get_empty(); |
76 | 49.4k | } |
77 | 71.5M | op = tuple_alloc(size); |
78 | 71.5M | if (op == NULL) { |
79 | 0 | return NULL; |
80 | 0 | } |
81 | 1.85G | for (Py_ssize_t i = 0; i < size; i++) { |
82 | 1.78G | op->ob_item[i] = NULL; |
83 | 1.78G | } |
84 | 71.5M | _PyObject_GC_TRACK(op); |
85 | 71.5M | return (PyObject *) op; |
86 | 71.5M | } |
87 | | |
88 | | Py_ssize_t |
89 | | PyTuple_Size(PyObject *op) |
90 | 5.91M | { |
91 | 5.91M | if (!PyTuple_Check(op)) { |
92 | 0 | PyErr_BadInternalCall(); |
93 | 0 | return -1; |
94 | 0 | } |
95 | 5.91M | else |
96 | 5.91M | return Py_SIZE(op); |
97 | 5.91M | } |
98 | | |
99 | | PyObject * |
100 | | PyTuple_GetItem(PyObject *op, Py_ssize_t i) |
101 | 27.1M | { |
102 | 27.1M | if (!PyTuple_Check(op)) { |
103 | 0 | PyErr_BadInternalCall(); |
104 | 0 | return NULL; |
105 | 0 | } |
106 | 27.1M | if (i < 0 || i >= Py_SIZE(op)) { |
107 | 0 | PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
108 | 0 | return NULL; |
109 | 0 | } |
110 | 27.1M | return ((PyTupleObject *)op) -> ob_item[i]; |
111 | 27.1M | } |
112 | | |
113 | | int |
114 | | PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem) |
115 | 150k | { |
116 | 150k | PyObject **p; |
117 | 150k | if (!PyTuple_Check(op) || !_PyObject_IsUniquelyReferenced(op)) { |
118 | 0 | Py_XDECREF(newitem); |
119 | 0 | PyErr_BadInternalCall(); |
120 | 0 | return -1; |
121 | 0 | } |
122 | 150k | if (i < 0 || i >= Py_SIZE(op)) { |
123 | 0 | Py_XDECREF(newitem); |
124 | 0 | PyErr_SetString(PyExc_IndexError, |
125 | 0 | "tuple assignment index out of range"); |
126 | 0 | return -1; |
127 | 0 | } |
128 | 150k | p = ((PyTupleObject *)op) -> ob_item + i; |
129 | 150k | Py_XSETREF(*p, newitem); |
130 | 150k | return 0; |
131 | 150k | } |
132 | | |
133 | | void |
134 | | _PyTuple_MaybeUntrack(PyObject *op) |
135 | 283M | { |
136 | 283M | PyTupleObject *t; |
137 | 283M | Py_ssize_t i, n; |
138 | | |
139 | 283M | if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op)) |
140 | 0 | return; |
141 | 283M | t = (PyTupleObject *) op; |
142 | 283M | n = Py_SIZE(t); |
143 | 921M | for (i = 0; i < n; i++) { |
144 | 903M | PyObject *elt = PyTuple_GET_ITEM(t, i); |
145 | | /* Tuple with NULL elements aren't |
146 | | fully constructed, don't untrack |
147 | | them yet. */ |
148 | 903M | if (!elt || |
149 | 903M | _PyObject_GC_MAY_BE_TRACKED(elt)) |
150 | 265M | return; |
151 | 903M | } |
152 | 17.7M | _PyObject_GC_UNTRACK(op); |
153 | 17.7M | } |
154 | | |
155 | | /* Fast, but conservative check if an object maybe tracked |
156 | | May return true for an object that is not tracked, |
157 | | Will always return true for an object that is tracked. |
158 | | This is a temporary workaround until _PyObject_GC_IS_TRACKED |
159 | | becomes fast and safe to call on non-GC objects. |
160 | | */ |
161 | | static bool |
162 | | maybe_tracked(PyObject *ob) |
163 | 2.64G | { |
164 | 2.64G | return _PyType_IS_GC(Py_TYPE(ob)); |
165 | 2.64G | } |
166 | | |
167 | | PyObject * |
168 | | PyTuple_Pack(Py_ssize_t n, ...) |
169 | 1.14M | { |
170 | 1.14M | Py_ssize_t i; |
171 | 1.14M | PyObject *o; |
172 | 1.14M | PyObject **items; |
173 | 1.14M | va_list vargs; |
174 | 1.14M | bool track = false; |
175 | | |
176 | 1.14M | if (n == 0) { |
177 | 0 | return tuple_get_empty(); |
178 | 0 | } |
179 | | |
180 | 1.14M | va_start(vargs, n); |
181 | 1.14M | PyTupleObject *result = tuple_alloc(n); |
182 | 1.14M | if (result == NULL) { |
183 | 0 | va_end(vargs); |
184 | 0 | return NULL; |
185 | 0 | } |
186 | 1.14M | items = result->ob_item; |
187 | 4.35M | for (i = 0; i < n; i++) { |
188 | 3.21M | o = va_arg(vargs, PyObject *); |
189 | 3.21M | if (!track && maybe_tracked(o)) { |
190 | 892k | track = true; |
191 | 892k | } |
192 | 3.21M | items[i] = Py_NewRef(o); |
193 | 3.21M | } |
194 | 1.14M | va_end(vargs); |
195 | 1.14M | if (track) { |
196 | 892k | _PyObject_GC_TRACK(result); |
197 | 892k | } |
198 | 1.14M | return (PyObject *)result; |
199 | 1.14M | } |
200 | | |
201 | | PyObject * |
202 | | _PyTuple_FromPair(PyObject *first, PyObject *second) |
203 | 270k | { |
204 | 270k | assert(first != NULL); |
205 | 270k | assert(second != NULL); |
206 | | |
207 | 270k | return _PyTuple_FromPairSteal(Py_NewRef(first), Py_NewRef(second)); |
208 | 270k | } |
209 | | |
210 | | PyObject * |
211 | | _PyTuple_FromPairSteal(PyObject *first, PyObject *second) |
212 | 20.0M | { |
213 | 20.0M | assert(first != NULL); |
214 | 20.0M | assert(second != NULL); |
215 | | |
216 | 20.0M | PyTupleObject *op = tuple_alloc(2); |
217 | 20.0M | if (op == NULL) { |
218 | 0 | Py_DECREF(first); |
219 | 0 | Py_DECREF(second); |
220 | 0 | return NULL; |
221 | 0 | } |
222 | 20.0M | PyObject **items = op->ob_item; |
223 | 20.0M | items[0] = first; |
224 | 20.0M | items[1] = second; |
225 | 20.0M | if (maybe_tracked(first) || maybe_tracked(second)) { |
226 | 113k | _PyObject_GC_TRACK(op); |
227 | 113k | } |
228 | 20.0M | return (PyObject *)op; |
229 | 20.0M | } |
230 | | |
231 | | /* Methods */ |
232 | | |
233 | | /* |
234 | | Free of a tuple where all contents have been stolen and |
235 | | is now untracked by GC. This operation is thus non-escaping. |
236 | | */ |
237 | | void |
238 | | _PyStolenTuple_Free(PyObject *obj) |
239 | 0 | { |
240 | 0 | assert(PyTuple_CheckExact(obj)); |
241 | 0 | PyTupleObject *op = _PyTuple_CAST(obj); |
242 | 0 | assert(Py_SIZE(op) != 0); |
243 | 0 | assert(!_PyObject_GC_IS_TRACKED(obj)); |
244 | | // This will abort on the empty singleton (if there is one). |
245 | 0 | if (!maybe_freelist_push(op)) { |
246 | 0 | PyTuple_Type.tp_free((PyObject *)op); |
247 | 0 | } |
248 | 0 | } |
249 | | |
250 | | static void |
251 | | tuple_dealloc(PyObject *self) |
252 | 562M | { |
253 | 562M | PyTupleObject *op = _PyTuple_CAST(self); |
254 | 562M | if (Py_SIZE(op) == 0) { |
255 | | /* The empty tuple is statically allocated. */ |
256 | 8 | if (op == &_Py_SINGLETON(tuple_empty)) { |
257 | | #ifdef Py_DEBUG |
258 | | _Py_FatalRefcountError("deallocating the empty tuple singleton"); |
259 | | #else |
260 | 0 | return; |
261 | 0 | #endif |
262 | 0 | } |
263 | | #ifdef Py_DEBUG |
264 | | /* tuple subclasses have their own empty instances. */ |
265 | | assert(!PyTuple_CheckExact(op)); |
266 | | #endif |
267 | 8 | } |
268 | | |
269 | 562M | PyObject_GC_UnTrack(op); |
270 | | |
271 | 562M | Py_ssize_t i = Py_SIZE(op); |
272 | 6.58G | while (--i >= 0) { |
273 | 6.02G | Py_XDECREF(op->ob_item[i]); |
274 | 6.02G | } |
275 | | // This will abort on the empty singleton (if there is one). |
276 | 562M | if (!maybe_freelist_push(op)) { |
277 | 150M | Py_TYPE(op)->tp_free((PyObject *)op); |
278 | 150M | } |
279 | 562M | } |
280 | | |
281 | | static PyObject * |
282 | | tuple_repr(PyObject *self) |
283 | 43.3k | { |
284 | 43.3k | PyTupleObject *v = _PyTuple_CAST(self); |
285 | 43.3k | Py_ssize_t n = PyTuple_GET_SIZE(v); |
286 | 43.3k | if (n == 0) { |
287 | 0 | return PyUnicode_FromString("()"); |
288 | 0 | } |
289 | | |
290 | | /* While not mutable, it is still possible to end up with a cycle in a |
291 | | tuple through an object that stores itself within a tuple (and thus |
292 | | infinitely asks for the repr of itself). This should only be |
293 | | possible within a type. */ |
294 | 43.3k | int res = Py_ReprEnter((PyObject *)v); |
295 | 43.3k | if (res != 0) { |
296 | 0 | return res > 0 ? PyUnicode_FromString("(...)") : NULL; |
297 | 0 | } |
298 | | |
299 | 43.3k | Py_ssize_t prealloc; |
300 | 43.3k | if (n > 1) { |
301 | | // "(" + "1" + ", 2" * (len - 1) + ")" |
302 | 22.0k | prealloc = 1 + 1 + (2 + 1) * (n - 1) + 1; |
303 | 22.0k | } |
304 | 21.2k | else { |
305 | | // "(1,)" |
306 | 21.2k | prealloc = 4; |
307 | 21.2k | } |
308 | 43.3k | PyUnicodeWriter *writer = PyUnicodeWriter_Create(prealloc); |
309 | 43.3k | if (writer == NULL) { |
310 | 0 | goto error; |
311 | 0 | } |
312 | | |
313 | 43.3k | if (PyUnicodeWriter_WriteChar(writer, '(') < 0) { |
314 | 0 | goto error; |
315 | 0 | } |
316 | | |
317 | | /* Do repr() on each element. */ |
318 | 119k | for (Py_ssize_t i = 0; i < n; ++i) { |
319 | 75.9k | if (i > 0) { |
320 | 32.6k | if (PyUnicodeWriter_WriteChar(writer, ',') < 0) { |
321 | 0 | goto error; |
322 | 0 | } |
323 | 32.6k | if (PyUnicodeWriter_WriteChar(writer, ' ') < 0) { |
324 | 0 | goto error; |
325 | 0 | } |
326 | 32.6k | } |
327 | | |
328 | 75.9k | if (PyUnicodeWriter_WriteRepr(writer, v->ob_item[i]) < 0) { |
329 | 0 | goto error; |
330 | 0 | } |
331 | 75.9k | } |
332 | | |
333 | 43.3k | if (n == 1) { |
334 | 21.2k | if (PyUnicodeWriter_WriteChar(writer, ',') < 0) { |
335 | 0 | goto error; |
336 | 0 | } |
337 | 21.2k | } |
338 | 43.3k | if (PyUnicodeWriter_WriteChar(writer, ')') < 0) { |
339 | 0 | goto error; |
340 | 0 | } |
341 | | |
342 | 43.3k | Py_ReprLeave((PyObject *)v); |
343 | 43.3k | return PyUnicodeWriter_Finish(writer); |
344 | | |
345 | 0 | error: |
346 | 0 | PyUnicodeWriter_Discard(writer); |
347 | 0 | Py_ReprLeave((PyObject *)v); |
348 | 0 | return NULL; |
349 | 43.3k | } |
350 | | |
351 | | |
352 | | /* Hash for tuples. This is a slightly simplified version of the xxHash |
353 | | non-cryptographic hash: |
354 | | - we do not use any parallelism, there is only 1 accumulator. |
355 | | - we drop the final mixing since this is just a permutation of the |
356 | | output space: it does not help against collisions. |
357 | | - at the end, we mangle the length with a single constant. |
358 | | For the xxHash specification, see |
359 | | https://github.com/Cyan4973/xxHash/blob/master/doc/xxhash_spec.md |
360 | | |
361 | | The constants for the hash function are defined in pycore_tuple.h. |
362 | | |
363 | | If you update this code, update also frozendict_pair_hash() which copied |
364 | | this code. |
365 | | */ |
366 | | |
367 | | static Py_hash_t |
368 | | tuple_hash(PyObject *op) |
369 | 35.8M | { |
370 | 35.8M | PyTupleObject *v = _PyTuple_CAST(op); |
371 | | |
372 | 35.8M | Py_uhash_t acc = FT_ATOMIC_LOAD_SSIZE_RELAXED(v->ob_hash); |
373 | 35.8M | if (acc != (Py_uhash_t)-1) { |
374 | 4.98M | return acc; |
375 | 4.98M | } |
376 | | |
377 | 30.8M | Py_ssize_t len = Py_SIZE(v); |
378 | 30.8M | PyObject **item = v->ob_item; |
379 | | |
380 | | /* Hashing an item recurses back into tuple_hash() when that item is |
381 | | itself a tuple, so guard the descent to raise RecursionError rather |
382 | | than overflow the C stack on a deeply nested tuple. */ |
383 | 30.8M | if (_Py_EnterRecursiveCall(" while hashing a tuple")) { |
384 | 0 | return -1; |
385 | 0 | } |
386 | | |
387 | 30.8M | acc = _PyTuple_HASH_XXPRIME_5; |
388 | 759M | for (Py_ssize_t i = 0; i < len; i++) { |
389 | 728M | Py_uhash_t lane = PyObject_Hash(item[i]); |
390 | 728M | if (lane == (Py_uhash_t)-1) { |
391 | 0 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
392 | | /* Preserve the original error while providing context for |
393 | | the element that could not be hashed. */ |
394 | 0 | PyObject *exc = PyErr_GetRaisedException(); |
395 | 0 | PyObject *note = PyUnicode_FromFormat( |
396 | 0 | "when hashing tuple item %zd", i); |
397 | 0 | if (note != NULL) { |
398 | 0 | if (_PyException_AddNote(exc, note) < 0) { |
399 | 0 | PyErr_Clear(); |
400 | 0 | } |
401 | 0 | Py_DECREF(note); |
402 | 0 | } |
403 | 0 | else { |
404 | 0 | PyErr_Clear(); |
405 | 0 | } |
406 | 0 | PyErr_SetRaisedException(exc); |
407 | 0 | } |
408 | 0 | _Py_LeaveRecursiveCall(); |
409 | 0 | return -1; |
410 | 0 | } |
411 | 728M | acc += lane * _PyTuple_HASH_XXPRIME_2; |
412 | 728M | acc = _PyTuple_HASH_XXROTATE(acc); |
413 | 728M | acc *= _PyTuple_HASH_XXPRIME_1; |
414 | 728M | } |
415 | 30.8M | _Py_LeaveRecursiveCall(); |
416 | | |
417 | | /* Add input length, mangled to keep the historical value of hash(()). */ |
418 | 30.8M | acc += len ^ (_PyTuple_HASH_XXPRIME_5 ^ 3527539UL); |
419 | | |
420 | 30.8M | if (acc == (Py_uhash_t)-1) { |
421 | 0 | acc = 1546275796; |
422 | 0 | } |
423 | | |
424 | 30.8M | FT_ATOMIC_STORE_SSIZE_RELAXED(v->ob_hash, acc); |
425 | | |
426 | 30.8M | return acc; |
427 | 30.8M | } |
428 | | |
429 | | static Py_ssize_t |
430 | | tuple_length(PyObject *self) |
431 | 23.9M | { |
432 | 23.9M | PyTupleObject *a = _PyTuple_CAST(self); |
433 | 23.9M | return Py_SIZE(a); |
434 | 23.9M | } |
435 | | |
436 | | static int |
437 | | tuple_contains(PyObject *self, PyObject *el) |
438 | 70.1M | { |
439 | 70.1M | PyTupleObject *a = _PyTuple_CAST(self); |
440 | 70.1M | int cmp = 0; |
441 | 234M | for (Py_ssize_t i = 0; cmp == 0 && i < Py_SIZE(a); ++i) { |
442 | 164M | cmp = PyObject_RichCompareBool(PyTuple_GET_ITEM(a, i), el, Py_EQ); |
443 | 164M | } |
444 | 70.1M | return cmp; |
445 | 70.1M | } |
446 | | |
447 | | static PyObject * |
448 | | tuple_item(PyObject *op, Py_ssize_t i) |
449 | 25.2M | { |
450 | 25.2M | PyTupleObject *a = _PyTuple_CAST(op); |
451 | 25.2M | if (i < 0 || i >= Py_SIZE(a)) { |
452 | 37 | PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
453 | 37 | return NULL; |
454 | 37 | } |
455 | 25.2M | return Py_NewRef(a->ob_item[i]); |
456 | 25.2M | } |
457 | | |
458 | | PyObject * |
459 | | PyTuple_FromArray(PyObject *const *src, Py_ssize_t n) |
460 | 225M | { |
461 | 225M | if (n == 0) { |
462 | 79.8M | return tuple_get_empty(); |
463 | 79.8M | } |
464 | | |
465 | 146M | PyTupleObject *tuple = tuple_alloc(n); |
466 | 146M | if (tuple == NULL) { |
467 | 0 | return NULL; |
468 | 0 | } |
469 | 146M | PyObject **dst = tuple->ob_item; |
470 | 146M | bool track = false; |
471 | 2.34G | for (Py_ssize_t i = 0; i < n; i++) { |
472 | 2.20G | PyObject *item = src[i]; |
473 | 2.20G | if (!track && maybe_tracked(item)) { |
474 | 41.5M | track = true; |
475 | 41.5M | } |
476 | 2.20G | dst[i] = Py_NewRef(item); |
477 | 2.20G | } |
478 | 146M | if (track) { |
479 | 41.5M | _PyObject_GC_TRACK(tuple); |
480 | 41.5M | } |
481 | 146M | return (PyObject *)tuple; |
482 | 146M | } |
483 | | |
484 | | PyObject * |
485 | | _PyTuple_FromStackRefStealOnSuccess(const _PyStackRef *src, Py_ssize_t n) |
486 | 304M | { |
487 | 304M | if (n == 0) { |
488 | 0 | return tuple_get_empty(); |
489 | 0 | } |
490 | 304M | PyTupleObject *tuple = tuple_alloc(n); |
491 | 304M | if (tuple == NULL) { |
492 | 0 | return NULL; |
493 | 0 | } |
494 | 304M | PyObject **dst = tuple->ob_item; |
495 | 304M | bool track = false; |
496 | 922M | for (Py_ssize_t i = 0; i < n; i++) { |
497 | 618M | PyObject *item = PyStackRef_AsPyObjectSteal(src[i]); |
498 | 618M | if (!track && maybe_tracked(item)) { |
499 | 209M | track = true; |
500 | 209M | } |
501 | 618M | dst[i] = item; |
502 | 618M | } |
503 | 304M | if (track) { |
504 | 209M | _PyObject_GC_TRACK(tuple); |
505 | 209M | } |
506 | 304M | return (PyObject *)tuple; |
507 | 304M | } |
508 | | |
509 | | PyObject * |
510 | | _PyTuple_FromArraySteal(PyObject *const *src, Py_ssize_t n) |
511 | 5.45M | { |
512 | 5.45M | if (n == 0) { |
513 | 65 | return tuple_get_empty(); |
514 | 65 | } |
515 | 5.45M | PyTupleObject *tuple = tuple_alloc(n); |
516 | 5.45M | if (tuple == NULL) { |
517 | 0 | for (Py_ssize_t i = 0; i < n; i++) { |
518 | 0 | Py_DECREF(src[i]); |
519 | 0 | } |
520 | 0 | return NULL; |
521 | 0 | } |
522 | 5.45M | PyObject **dst = tuple->ob_item; |
523 | 31.0M | for (Py_ssize_t i = 0; i < n; i++) { |
524 | 25.5M | PyObject *item = src[i]; |
525 | 25.5M | dst[i] = item; |
526 | 25.5M | } |
527 | 5.45M | _PyObject_GC_TRACK(tuple); |
528 | 5.45M | return (PyObject *)tuple; |
529 | 5.45M | } |
530 | | |
531 | | static PyObject * |
532 | | tuple_slice(PyTupleObject *a, Py_ssize_t ilow, |
533 | | Py_ssize_t ihigh) |
534 | 406k | { |
535 | 406k | if (ilow < 0) |
536 | 0 | ilow = 0; |
537 | 406k | if (ihigh > Py_SIZE(a)) |
538 | 202 | ihigh = Py_SIZE(a); |
539 | 406k | if (ihigh < ilow) |
540 | 0 | ihigh = ilow; |
541 | 406k | if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) { |
542 | 0 | return Py_NewRef(a); |
543 | 0 | } |
544 | 406k | return PyTuple_FromArray(a->ob_item + ilow, ihigh - ilow); |
545 | 406k | } |
546 | | |
547 | | PyObject * |
548 | | _PyTuple_BinarySlice(PyObject *container, PyObject *start, PyObject *stop) |
549 | 16.8M | { |
550 | 16.8M | assert(PyTuple_CheckExact(container)); |
551 | 16.8M | Py_ssize_t len = Py_SIZE(container); |
552 | 16.8M | Py_ssize_t istart, istop; |
553 | 16.8M | if (!_PyEval_UnpackIndices(start, stop, len, &istart, &istop)) { |
554 | 0 | return NULL; |
555 | 0 | } |
556 | 16.8M | if (istart == 0 && istop == len) { |
557 | 1.11k | return Py_NewRef(container); |
558 | 1.11k | } |
559 | 16.8M | if (istop < istart) { |
560 | 0 | istop = istart; |
561 | 0 | } |
562 | 16.8M | return PyTuple_FromArray(((PyTupleObject *)container)->ob_item + istart, |
563 | 16.8M | istop - istart); |
564 | 16.8M | } |
565 | | |
566 | | PyObject * |
567 | | PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j) |
568 | 406k | { |
569 | 406k | if (op == NULL || !PyTuple_Check(op)) { |
570 | 0 | PyErr_BadInternalCall(); |
571 | 0 | return NULL; |
572 | 0 | } |
573 | 406k | return tuple_slice((PyTupleObject *)op, i, j); |
574 | 406k | } |
575 | | |
576 | | PyObject * |
577 | | _PyTuple_Concat(PyObject *aa, PyObject *bb) |
578 | 13.2M | { |
579 | 13.2M | PyTupleObject *a = _PyTuple_CAST(aa); |
580 | 13.2M | if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) { |
581 | 139k | return Py_NewRef(bb); |
582 | 139k | } |
583 | 13.1M | if (!PyTuple_Check(bb)) { |
584 | 0 | PyErr_Format(PyExc_TypeError, |
585 | 0 | "can only concatenate tuple (not \"%.200s\") to tuple", |
586 | 0 | Py_TYPE(bb)->tp_name); |
587 | 0 | return NULL; |
588 | 0 | } |
589 | 13.1M | PyTupleObject *b = (PyTupleObject *)bb; |
590 | | |
591 | 13.1M | if (Py_SIZE(b) == 0 && PyTuple_CheckExact(a)) { |
592 | 73.3k | return Py_NewRef(a); |
593 | 73.3k | } |
594 | 13.1M | assert((size_t)Py_SIZE(a) + (size_t)Py_SIZE(b) < PY_SSIZE_T_MAX); |
595 | 13.0M | Py_ssize_t size = Py_SIZE(a) + Py_SIZE(b); |
596 | 13.0M | if (size == 0) { |
597 | 0 | return tuple_get_empty(); |
598 | 0 | } |
599 | | |
600 | 13.0M | PyTupleObject *np = tuple_alloc(size); |
601 | 13.0M | if (np == NULL) { |
602 | 0 | return NULL; |
603 | 0 | } |
604 | | |
605 | 13.0M | PyObject **src = a->ob_item; |
606 | 13.0M | PyObject **dest = np->ob_item; |
607 | 710M | for (Py_ssize_t i = 0; i < Py_SIZE(a); i++) { |
608 | 697M | PyObject *v = src[i]; |
609 | 697M | dest[i] = Py_NewRef(v); |
610 | 697M | } |
611 | | |
612 | 13.0M | src = b->ob_item; |
613 | 13.0M | dest = np->ob_item + Py_SIZE(a); |
614 | 659M | for (Py_ssize_t i = 0; i < Py_SIZE(b); i++) { |
615 | 646M | PyObject *v = src[i]; |
616 | 646M | dest[i] = Py_NewRef(v); |
617 | 646M | } |
618 | | |
619 | 13.0M | _PyObject_GC_TRACK(np); |
620 | 13.0M | return (PyObject *)np; |
621 | 13.0M | } |
622 | | |
623 | | PyObject * |
624 | | _PyTuple_Repeat(PyObject *self, Py_ssize_t n) |
625 | 0 | { |
626 | 0 | PyTupleObject *a = _PyTuple_CAST(self); |
627 | 0 | const Py_ssize_t input_size = Py_SIZE(a); |
628 | 0 | if (input_size == 0 || n == 1) { |
629 | 0 | if (PyTuple_CheckExact(a)) { |
630 | | /* Since tuples are immutable, we can return a shared |
631 | | copy in this case */ |
632 | 0 | return Py_NewRef(a); |
633 | 0 | } |
634 | 0 | } |
635 | 0 | if (input_size == 0 || n <= 0) { |
636 | 0 | return tuple_get_empty(); |
637 | 0 | } |
638 | 0 | assert(n>0); |
639 | |
|
640 | 0 | if (input_size > PY_SSIZE_T_MAX / n) |
641 | 0 | return PyErr_NoMemory(); |
642 | 0 | Py_ssize_t output_size = input_size * n; |
643 | |
|
644 | 0 | PyTupleObject *np = tuple_alloc(output_size); |
645 | 0 | if (np == NULL) |
646 | 0 | return NULL; |
647 | | |
648 | 0 | PyObject **dest = np->ob_item; |
649 | 0 | if (input_size == 1) { |
650 | 0 | PyObject *elem = a->ob_item[0]; |
651 | 0 | _Py_RefcntAdd(elem, n); |
652 | 0 | PyObject **dest_end = dest + output_size; |
653 | 0 | while (dest < dest_end) { |
654 | 0 | *dest++ = elem; |
655 | 0 | } |
656 | 0 | } |
657 | 0 | else { |
658 | 0 | PyObject **src = a->ob_item; |
659 | 0 | PyObject **src_end = src + input_size; |
660 | 0 | while (src < src_end) { |
661 | 0 | _Py_RefcntAdd(*src, n); |
662 | 0 | *dest++ = *src++; |
663 | 0 | } |
664 | |
|
665 | 0 | _Py_memory_repeat((char *)np->ob_item, sizeof(PyObject *)*output_size, |
666 | 0 | sizeof(PyObject *)*input_size); |
667 | 0 | } |
668 | 0 | _PyObject_GC_TRACK(np); |
669 | 0 | return (PyObject *) np; |
670 | 0 | } |
671 | | |
672 | | /*[clinic input] |
673 | | tuple.index |
674 | | |
675 | | value: object |
676 | | start: slice_index(accept={int}) = 0 |
677 | | stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize |
678 | | / |
679 | | |
680 | | Return first index of value. |
681 | | |
682 | | Raises ValueError if the value is not present. |
683 | | [clinic start generated code]*/ |
684 | | |
685 | | static PyObject * |
686 | | tuple_index_impl(PyTupleObject *self, PyObject *value, Py_ssize_t start, |
687 | | Py_ssize_t stop) |
688 | | /*[clinic end generated code: output=07b6f9f3cb5c33eb input=fb39e9874a21fe3f]*/ |
689 | 724 | { |
690 | 724 | Py_ssize_t i; |
691 | | |
692 | 724 | if (start < 0) { |
693 | 0 | start += Py_SIZE(self); |
694 | 0 | if (start < 0) |
695 | 0 | start = 0; |
696 | 0 | } |
697 | 724 | if (stop < 0) { |
698 | 0 | stop += Py_SIZE(self); |
699 | 0 | } |
700 | 724 | else if (stop > Py_SIZE(self)) { |
701 | 724 | stop = Py_SIZE(self); |
702 | 724 | } |
703 | 932 | for (i = start; i < stop; i++) { |
704 | 372 | int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ); |
705 | 372 | if (cmp > 0) |
706 | 164 | return PyLong_FromSsize_t(i); |
707 | 208 | else if (cmp < 0) |
708 | 0 | return NULL; |
709 | 372 | } |
710 | 560 | PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple"); |
711 | 560 | return NULL; |
712 | 724 | } |
713 | | |
714 | | /*[clinic input] |
715 | | tuple.count |
716 | | |
717 | | value: object |
718 | | / |
719 | | |
720 | | Return number of occurrences of value. |
721 | | [clinic start generated code]*/ |
722 | | |
723 | | static PyObject * |
724 | | tuple_count_impl(PyTupleObject *self, PyObject *value) |
725 | | /*[clinic end generated code: output=cf02888d4bc15d7a input=531721aff65bd772]*/ |
726 | 0 | { |
727 | 0 | Py_ssize_t count = 0; |
728 | 0 | Py_ssize_t i; |
729 | |
|
730 | 0 | for (i = 0; i < Py_SIZE(self); i++) { |
731 | 0 | int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ); |
732 | 0 | if (cmp > 0) |
733 | 0 | count++; |
734 | 0 | else if (cmp < 0) |
735 | 0 | return NULL; |
736 | 0 | } |
737 | 0 | return PyLong_FromSsize_t(count); |
738 | 0 | } |
739 | | |
740 | | static int |
741 | | tuple_traverse(PyObject *self, visitproc visit, void *arg) |
742 | 568M | { |
743 | 568M | PyTupleObject *o = _PyTuple_CAST(self); |
744 | 2.87G | for (Py_ssize_t i = Py_SIZE(o); --i >= 0; ) { |
745 | 2.30G | Py_VISIT(o->ob_item[i]); |
746 | 2.30G | } |
747 | 568M | return 0; |
748 | 568M | } |
749 | | |
750 | | static PyObject * |
751 | | tuple_richcompare(PyObject *v, PyObject *w, int op) |
752 | 21.2M | { |
753 | 21.2M | PyTupleObject *vt, *wt; |
754 | 21.2M | Py_ssize_t i; |
755 | 21.2M | Py_ssize_t vlen, wlen; |
756 | | |
757 | 21.2M | if (!PyTuple_Check(v) || !PyTuple_Check(w)) |
758 | 8 | Py_RETURN_NOTIMPLEMENTED; |
759 | | |
760 | 21.2M | vt = (PyTupleObject *)v; |
761 | 21.2M | wt = (PyTupleObject *)w; |
762 | | |
763 | 21.2M | vlen = Py_SIZE(vt); |
764 | 21.2M | wlen = Py_SIZE(wt); |
765 | | |
766 | | /* Note: the corresponding code for lists has an "early out" test |
767 | | * here when op is EQ or NE and the lengths differ. That pays there, |
768 | | * but Tim was unable to find any real code where EQ/NE tuple |
769 | | * compares don't have the same length, so testing for it here would |
770 | | * have cost without benefit. |
771 | | */ |
772 | | |
773 | | /* Search for the first index where items are different. |
774 | | * Note that because tuples are immutable, it's safe to reuse |
775 | | * vlen and wlen across the comparison calls. |
776 | | */ |
777 | 65.7M | for (i = 0; i < vlen && i < wlen; i++) { |
778 | 45.7M | int k = PyObject_RichCompareBool(vt->ob_item[i], |
779 | 45.7M | wt->ob_item[i], Py_EQ); |
780 | 45.7M | if (k < 0) |
781 | 0 | return NULL; |
782 | 45.7M | if (!k) |
783 | 1.31M | break; |
784 | 45.7M | } |
785 | | |
786 | 21.2M | if (i >= vlen || i >= wlen) { |
787 | | /* No more items to compare -- compare sizes */ |
788 | 19.9M | Py_RETURN_RICHCOMPARE(vlen, wlen, op); |
789 | 19.9M | } |
790 | | |
791 | | /* We have an item that differs -- shortcuts for EQ/NE */ |
792 | 1.31M | if (op == Py_EQ) { |
793 | 247k | Py_RETURN_FALSE; |
794 | 247k | } |
795 | 1.06M | if (op == Py_NE) { |
796 | 121k | Py_RETURN_TRUE; |
797 | 121k | } |
798 | | |
799 | | /* Compare the final item again using the proper operator */ |
800 | 946k | return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op); |
801 | 1.06M | } |
802 | | |
803 | | static PyObject * |
804 | | tuple_subtype_new(PyTypeObject *type, PyObject *iterable); |
805 | | |
806 | | /*[clinic input] |
807 | | @vectorcall |
808 | | @classmethod |
809 | | tuple.__new__ as tuple_new |
810 | | iterable: object(c_default="NULL") = () |
811 | | / |
812 | | |
813 | | Built-in immutable sequence. |
814 | | |
815 | | If no argument is given, the constructor returns an empty tuple. |
816 | | If iterable is specified the tuple is initialized from iterable's items. |
817 | | |
818 | | If the argument is a tuple, the return value is the same object. |
819 | | [clinic start generated code]*/ |
820 | | |
821 | | static PyObject * |
822 | | tuple_new_impl(PyTypeObject *type, PyObject *iterable) |
823 | | /*[clinic end generated code: output=4546d9f0d469bce7 input=8fdda913493ebe48]*/ |
824 | 1.17M | { |
825 | 1.17M | if (type != &PyTuple_Type) |
826 | 586k | return tuple_subtype_new(type, iterable); |
827 | | |
828 | 586k | if (iterable == NULL) { |
829 | 0 | return tuple_get_empty(); |
830 | 0 | } |
831 | 586k | else { |
832 | 586k | return PySequence_Tuple(iterable); |
833 | 586k | } |
834 | 586k | } |
835 | | |
836 | | static PyObject * |
837 | | tuple_subtype_new(PyTypeObject *type, PyObject *iterable) |
838 | 586k | { |
839 | 586k | PyObject *tmp, *newobj, *item; |
840 | 586k | Py_ssize_t i, n; |
841 | | |
842 | 586k | assert(PyType_IsSubtype(type, &PyTuple_Type)); |
843 | | // tuple subclasses must implement the GC protocol |
844 | 586k | assert(_PyType_IS_GC(type)); |
845 | | |
846 | 586k | tmp = tuple_new_impl(&PyTuple_Type, iterable); |
847 | 586k | if (tmp == NULL) |
848 | 0 | return NULL; |
849 | 586k | assert(PyTuple_Check(tmp)); |
850 | | /* This may allocate an empty tuple that is not the global one. */ |
851 | 586k | newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp)); |
852 | 586k | if (newobj == NULL) { |
853 | 0 | Py_DECREF(tmp); |
854 | 0 | return NULL; |
855 | 0 | } |
856 | 2.26M | for (i = 0; i < n; i++) { |
857 | 1.67M | item = PyTuple_GET_ITEM(tmp, i); |
858 | 1.67M | PyTuple_SET_ITEM(newobj, i, Py_NewRef(item)); |
859 | 1.67M | } |
860 | 586k | Py_DECREF(tmp); |
861 | | |
862 | 586k | _PyTuple_RESET_HASH_CACHE(newobj); |
863 | | |
864 | | // Don't track if a subclass tp_alloc is PyType_GenericAlloc() |
865 | 586k | if (!_PyObject_GC_IS_TRACKED(newobj)) { |
866 | 0 | _PyObject_GC_TRACK(newobj); |
867 | 0 | } |
868 | 586k | return newobj; |
869 | 586k | } |
870 | | |
871 | | static PySequenceMethods tuple_as_sequence = { |
872 | | tuple_length, /* sq_length */ |
873 | | _PyTuple_Concat, /* sq_concat */ |
874 | | _PyTuple_Repeat, /* sq_repeat */ |
875 | | tuple_item, /* sq_item */ |
876 | | 0, /* sq_slice */ |
877 | | 0, /* sq_ass_item */ |
878 | | 0, /* sq_ass_slice */ |
879 | | tuple_contains, /* sq_contains */ |
880 | | }; |
881 | | |
882 | | static _PyObjectIndexPair |
883 | | tuple_iteritem(PyObject *obj, Py_ssize_t index) |
884 | 6.36k | { |
885 | 6.36k | if (index >= PyTuple_GET_SIZE(obj)) { |
886 | 459 | return (_PyObjectIndexPair) { .object = NULL, .index = index }; |
887 | 459 | } |
888 | 5.91k | PyObject *result = PyTuple_GET_ITEM(obj, index); |
889 | 5.91k | Py_INCREF(result); |
890 | 5.91k | return (_PyObjectIndexPair) { .object = result, .index = index + 1 }; |
891 | 6.36k | } |
892 | | |
893 | | static PyObject* |
894 | | tuple_subscript(PyObject *op, PyObject* item) |
895 | 8.55M | { |
896 | 8.55M | PyTupleObject *self = _PyTuple_CAST(op); |
897 | 8.55M | if (_PyIndex_Check(item)) { |
898 | 7.34M | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
899 | 7.34M | if (i == -1 && PyErr_Occurred()) |
900 | 0 | return NULL; |
901 | 7.34M | if (i < 0) |
902 | 7.33M | i += PyTuple_GET_SIZE(self); |
903 | 7.34M | return tuple_item(op, i); |
904 | 7.34M | } |
905 | 1.21M | else if (PySlice_Check(item)) { |
906 | 1.21M | Py_ssize_t start, stop, step, slicelength, i; |
907 | 1.21M | size_t cur; |
908 | 1.21M | PyObject* it; |
909 | 1.21M | PyObject **src, **dest; |
910 | | |
911 | 1.21M | if (PySlice_Unpack(item, &start, &stop, &step) < 0) { |
912 | 0 | return NULL; |
913 | 0 | } |
914 | 1.21M | slicelength = PySlice_AdjustIndices(PyTuple_GET_SIZE(self), &start, |
915 | 1.21M | &stop, step); |
916 | | |
917 | 1.21M | if (slicelength <= 0) { |
918 | 164k | return tuple_get_empty(); |
919 | 164k | } |
920 | 1.05M | else if (start == 0 && step == 1 && |
921 | 635 | slicelength == PyTuple_GET_SIZE(self) && |
922 | 0 | PyTuple_CheckExact(self)) { |
923 | 0 | return Py_NewRef(self); |
924 | 0 | } |
925 | 1.05M | else { |
926 | 1.05M | PyTupleObject* result = tuple_alloc(slicelength); |
927 | 1.05M | if (!result) return NULL; |
928 | | |
929 | 1.05M | src = self->ob_item; |
930 | 1.05M | dest = result->ob_item; |
931 | 8.43M | for (cur = start, i = 0; i < slicelength; |
932 | 7.37M | cur += step, i++) { |
933 | 7.37M | it = Py_NewRef(src[cur]); |
934 | 7.37M | dest[i] = it; |
935 | 7.37M | } |
936 | | |
937 | 1.05M | _PyObject_GC_TRACK(result); |
938 | 1.05M | return (PyObject *)result; |
939 | 1.05M | } |
940 | 1.21M | } |
941 | 0 | else { |
942 | 0 | PyErr_Format(PyExc_TypeError, |
943 | 0 | "tuple indices must be integers or slices, not %.200s", |
944 | 0 | Py_TYPE(item)->tp_name); |
945 | 0 | return NULL; |
946 | 0 | } |
947 | 8.55M | } |
948 | | |
949 | | /*[clinic input] |
950 | | tuple.__getnewargs__ |
951 | | [clinic start generated code]*/ |
952 | | |
953 | | static PyObject * |
954 | | tuple___getnewargs___impl(PyTupleObject *self) |
955 | | /*[clinic end generated code: output=25e06e3ee56027e2 input=1aeb4b286a21639a]*/ |
956 | 0 | { |
957 | 0 | return Py_BuildValue("(N)", tuple_slice(self, 0, Py_SIZE(self))); |
958 | 0 | } |
959 | | |
960 | | |
961 | | PyDoc_STRVAR(tuple_class_getitem_doc, |
962 | | "Tuples are generic over the types of their contents.\n\n\ |
963 | | For example, use ``tuple[int, str]`` for a pair whose first element\n\ |
964 | | is an int and second element is a string.\n\n\ |
965 | | Tuples also support the form ``tuple[T, ...]`` to indicate\n\ |
966 | | an arbitrary length tuple of elements of type T."); |
967 | | |
968 | | static PyMethodDef tuple_methods[] = { |
969 | | TUPLE___GETNEWARGS___METHODDEF |
970 | | TUPLE_INDEX_METHODDEF |
971 | | TUPLE_COUNT_METHODDEF |
972 | | {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, tuple_class_getitem_doc}, |
973 | | {NULL, NULL} /* sentinel */ |
974 | | }; |
975 | | |
976 | | static PyMappingMethods tuple_as_mapping = { |
977 | | tuple_length, |
978 | | tuple_subscript, |
979 | | 0 |
980 | | }; |
981 | | |
982 | | static PyObject *tuple_iter(PyObject *seq); |
983 | | |
984 | | PyTypeObject PyTuple_Type = { |
985 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
986 | | "tuple", |
987 | | sizeof(PyTupleObject) - sizeof(PyObject *), |
988 | | sizeof(PyObject *), |
989 | | tuple_dealloc, /* tp_dealloc */ |
990 | | 0, /* tp_vectorcall_offset */ |
991 | | 0, /* tp_getattr */ |
992 | | 0, /* tp_setattr */ |
993 | | 0, /* tp_as_async */ |
994 | | tuple_repr, /* tp_repr */ |
995 | | 0, /* tp_as_number */ |
996 | | &tuple_as_sequence, /* tp_as_sequence */ |
997 | | &tuple_as_mapping, /* tp_as_mapping */ |
998 | | tuple_hash, /* tp_hash */ |
999 | | 0, /* tp_call */ |
1000 | | 0, /* tp_str */ |
1001 | | PyObject_GenericGetAttr, /* tp_getattro */ |
1002 | | 0, /* tp_setattro */ |
1003 | | 0, /* tp_as_buffer */ |
1004 | | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
1005 | | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS | |
1006 | | _Py_TPFLAGS_MATCH_SELF | Py_TPFLAGS_SEQUENCE, /* tp_flags */ |
1007 | | tuple_new__doc__, /* tp_doc */ |
1008 | | tuple_traverse, /* tp_traverse */ |
1009 | | 0, /* tp_clear */ |
1010 | | tuple_richcompare, /* tp_richcompare */ |
1011 | | 0, /* tp_weaklistoffset */ |
1012 | | tuple_iter, /* tp_iter */ |
1013 | | 0, /* tp_iternext */ |
1014 | | tuple_methods, /* tp_methods */ |
1015 | | 0, /* tp_members */ |
1016 | | 0, /* tp_getset */ |
1017 | | 0, /* tp_base */ |
1018 | | 0, /* tp_dict */ |
1019 | | 0, /* tp_descr_get */ |
1020 | | 0, /* tp_descr_set */ |
1021 | | 0, /* tp_dictoffset */ |
1022 | | 0, /* tp_init */ |
1023 | | 0, /* tp_alloc */ |
1024 | | tuple_new, /* tp_new */ |
1025 | | PyObject_GC_Del, /* tp_free */ |
1026 | | .tp_vectorcall = tuple_vectorcall, |
1027 | | .tp_version_tag = _Py_TYPE_VERSION_TUPLE, |
1028 | | ._tp_iteritem = tuple_iteritem, |
1029 | | }; |
1030 | | |
1031 | | /* The following function breaks the notion that tuples are immutable: |
1032 | | it changes the size of a tuple. We get away with this only if there |
1033 | | is only one module referencing the object. You can also think of it |
1034 | | as creating a new tuple object and destroying the old one, only more |
1035 | | efficiently. In any case, don't use this if the tuple may already be |
1036 | | known to some other part of the code. */ |
1037 | | |
1038 | | int |
1039 | | _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize) |
1040 | 806 | { |
1041 | 806 | PyTupleObject *v; |
1042 | 806 | PyTupleObject *sv; |
1043 | 806 | Py_ssize_t i; |
1044 | 806 | Py_ssize_t oldsize; |
1045 | | |
1046 | 806 | v = (PyTupleObject *) *pv; |
1047 | 806 | if (v == NULL || !Py_IS_TYPE(v, &PyTuple_Type) || newsize < 0 |
1048 | 806 | || (Py_SIZE(v) != 0 && !_PyObject_IsUniquelyReferenced(*pv)) |
1049 | | // gh-161226: Disallow resizing a tuple if the hash value is cached |
1050 | 806 | || (Py_SIZE(v) != 0 && v->ob_hash != -1)) |
1051 | 0 | { |
1052 | 0 | *pv = 0; |
1053 | 0 | Py_XDECREF(v); |
1054 | 0 | PyErr_BadInternalCall(); |
1055 | 0 | return -1; |
1056 | 0 | } |
1057 | | |
1058 | 806 | oldsize = Py_SIZE(v); |
1059 | 806 | if (oldsize == newsize) { |
1060 | 689 | return 0; |
1061 | 689 | } |
1062 | 117 | if (newsize == 0) { |
1063 | 76 | Py_DECREF(v); |
1064 | 76 | *pv = tuple_get_empty(); |
1065 | 76 | return 0; |
1066 | 76 | } |
1067 | 41 | if (oldsize == 0) { |
1068 | | #ifdef Py_DEBUG |
1069 | | assert(v == &_Py_SINGLETON(tuple_empty)); |
1070 | | #endif |
1071 | | /* The empty tuple is statically allocated so we never |
1072 | | resize it in-place. */ |
1073 | 0 | Py_DECREF(v); |
1074 | 0 | *pv = PyTuple_New(newsize); |
1075 | 0 | return *pv == NULL ? -1 : 0; |
1076 | 0 | } |
1077 | | |
1078 | 41 | if (_PyObject_GC_IS_TRACKED(v)) { |
1079 | 41 | _PyObject_GC_UNTRACK(v); |
1080 | 41 | } |
1081 | | #ifdef Py_TRACE_REFS |
1082 | | _Py_ForgetReference((PyObject *) v); |
1083 | | #endif |
1084 | | /* DECREF items deleted by shrinkage */ |
1085 | 156 | for (i = newsize; i < oldsize; i++) { |
1086 | 115 | Py_CLEAR(v->ob_item[i]); |
1087 | 115 | } |
1088 | 41 | _PyReftracerTrack((PyObject *)v, PyRefTracer_DESTROY); |
1089 | 41 | sv = PyObject_GC_Resize(PyTupleObject, v, newsize); |
1090 | 41 | if (sv == NULL) { |
1091 | 0 | *pv = NULL; |
1092 | | #ifdef Py_REF_DEBUG |
1093 | | _Py_DecRefTotal(_PyThreadState_GET()); |
1094 | | #endif |
1095 | | /* The failed reallocation leaves the original items owned by v. */ |
1096 | 0 | i = oldsize; |
1097 | 0 | while (--i >= 0) { |
1098 | 0 | Py_XDECREF(v->ob_item[i]); |
1099 | 0 | } |
1100 | 0 | PyObject_GC_Del(v); |
1101 | 0 | return -1; |
1102 | 0 | } |
1103 | 41 | _Py_NewReferenceNoTotal((PyObject *) sv); |
1104 | | /* Zero out items added by growing */ |
1105 | 41 | if (newsize > oldsize) |
1106 | 0 | memset(&sv->ob_item[oldsize], 0, |
1107 | 0 | sizeof(*sv->ob_item) * (newsize - oldsize)); |
1108 | 41 | *pv = (PyObject *) sv; |
1109 | 41 | _PyObject_GC_TRACK(sv); |
1110 | 41 | return 0; |
1111 | 41 | } |
1112 | | |
1113 | | /*********************** Tuple Iterator **************************/ |
1114 | | |
1115 | 1.64G | #define _PyTupleIterObject_CAST(op) ((_PyTupleIterObject *)(op)) |
1116 | | |
1117 | | static void |
1118 | | tupleiter_dealloc(PyObject *self) |
1119 | 9.43M | { |
1120 | 9.43M | _PyTupleIterObject *it = _PyTupleIterObject_CAST(self); |
1121 | 9.43M | _PyObject_GC_UNTRACK(it); |
1122 | 9.43M | Py_XDECREF(it->it_seq); |
1123 | 9.43M | assert(Py_IS_TYPE(self, &PyTupleIter_Type)); |
1124 | 9.43M | _Py_FREELIST_FREE(tuple_iters, it, PyObject_GC_Del); |
1125 | 9.43M | } |
1126 | | |
1127 | | static int |
1128 | | tupleiter_traverse(PyObject *self, visitproc visit, void *arg) |
1129 | 10.5M | { |
1130 | 10.5M | _PyTupleIterObject *it = _PyTupleIterObject_CAST(self); |
1131 | 10.5M | Py_VISIT(it->it_seq); |
1132 | 10.5M | return 0; |
1133 | 10.5M | } |
1134 | | |
1135 | | static PyObject * |
1136 | | tupleiter_next(PyObject *self) |
1137 | 1.62G | { |
1138 | 1.62G | _PyTupleIterObject *it = _PyTupleIterObject_CAST(self); |
1139 | 1.62G | PyTupleObject *seq; |
1140 | 1.62G | PyObject *item; |
1141 | | |
1142 | 1.62G | assert(it != NULL); |
1143 | 1.62G | seq = it->it_seq; |
1144 | 1.62G | #ifndef Py_GIL_DISABLED |
1145 | 1.62G | if (seq == NULL) |
1146 | 0 | return NULL; |
1147 | 1.62G | #endif |
1148 | 1.62G | assert(PyTuple_Check(seq)); |
1149 | | |
1150 | 1.62G | Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index); |
1151 | 1.62G | if (index < PyTuple_GET_SIZE(seq)) { |
1152 | 1.62G | FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, index + 1); |
1153 | 1.62G | item = PyTuple_GET_ITEM(seq, index); |
1154 | 1.62G | return Py_NewRef(item); |
1155 | 1.62G | } |
1156 | | |
1157 | 4.04M | #ifndef Py_GIL_DISABLED |
1158 | 4.04M | it->it_seq = NULL; |
1159 | 4.04M | Py_DECREF(seq); |
1160 | 4.04M | #endif |
1161 | 4.04M | return NULL; |
1162 | 1.62G | } |
1163 | | |
1164 | | static PyObject * |
1165 | | tupleiter_len(PyObject *self, PyObject *Py_UNUSED(ignored)) |
1166 | 0 | { |
1167 | 0 | _PyTupleIterObject *it = _PyTupleIterObject_CAST(self); |
1168 | 0 | Py_ssize_t len = 0; |
1169 | | #ifdef Py_GIL_DISABLED |
1170 | | Py_ssize_t idx = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index); |
1171 | | Py_ssize_t seq_len = PyTuple_GET_SIZE(it->it_seq); |
1172 | | if (idx < seq_len) |
1173 | | len = seq_len - idx; |
1174 | | #else |
1175 | 0 | if (it->it_seq) |
1176 | 0 | len = PyTuple_GET_SIZE(it->it_seq) - it->it_index; |
1177 | 0 | #endif |
1178 | 0 | return PyLong_FromSsize_t(len); |
1179 | 0 | } |
1180 | | |
1181 | | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
1182 | | |
1183 | | static PyObject * |
1184 | | tupleiter_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) |
1185 | 0 | { |
1186 | 0 | PyObject *iter = _PyEval_GetBuiltin(&_Py_ID(iter)); |
1187 | | |
1188 | | /* _PyEval_GetBuiltin can invoke arbitrary code, |
1189 | | * call must be before access of iterator pointers. |
1190 | | * see issue #101765 */ |
1191 | 0 | _PyTupleIterObject *it = _PyTupleIterObject_CAST(self); |
1192 | |
|
1193 | | #ifdef Py_GIL_DISABLED |
1194 | | Py_ssize_t idx = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index); |
1195 | | if (idx < PyTuple_GET_SIZE(it->it_seq)) |
1196 | | return Py_BuildValue("N(O)n", iter, it->it_seq, idx); |
1197 | | #else |
1198 | 0 | if (it->it_seq) |
1199 | 0 | return Py_BuildValue("N(O)n", iter, it->it_seq, it->it_index); |
1200 | 0 | #endif |
1201 | 0 | return Py_BuildValue("N(())", iter); |
1202 | 0 | } |
1203 | | |
1204 | | static PyObject * |
1205 | | tupleiter_setstate(PyObject *self, PyObject *state) |
1206 | 0 | { |
1207 | 0 | _PyTupleIterObject *it = _PyTupleIterObject_CAST(self); |
1208 | 0 | Py_ssize_t index = PyLong_AsSsize_t(state); |
1209 | 0 | if (index == -1 && PyErr_Occurred()) |
1210 | 0 | return NULL; |
1211 | 0 | if (it->it_seq != NULL) { |
1212 | 0 | if (index < 0) |
1213 | 0 | index = 0; |
1214 | 0 | else if (index > PyTuple_GET_SIZE(it->it_seq)) |
1215 | 0 | index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */ |
1216 | 0 | FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, index); |
1217 | 0 | } |
1218 | 0 | Py_RETURN_NONE; |
1219 | 0 | } |
1220 | | |
1221 | | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
1222 | | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
1223 | | |
1224 | | static PyMethodDef tupleiter_methods[] = { |
1225 | | {"__length_hint__", tupleiter_len, METH_NOARGS, length_hint_doc}, |
1226 | | {"__reduce__", tupleiter_reduce, METH_NOARGS, reduce_doc}, |
1227 | | {"__setstate__", tupleiter_setstate, METH_O, setstate_doc}, |
1228 | | {NULL, NULL, 0, NULL} /* sentinel */ |
1229 | | }; |
1230 | | |
1231 | | PyTypeObject PyTupleIter_Type = { |
1232 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
1233 | | "tuple_iterator", /* tp_name */ |
1234 | | sizeof(_PyTupleIterObject), /* tp_basicsize */ |
1235 | | 0, /* tp_itemsize */ |
1236 | | /* methods */ |
1237 | | tupleiter_dealloc, /* tp_dealloc */ |
1238 | | 0, /* tp_vectorcall_offset */ |
1239 | | 0, /* tp_getattr */ |
1240 | | 0, /* tp_setattr */ |
1241 | | 0, /* tp_as_async */ |
1242 | | 0, /* tp_repr */ |
1243 | | 0, /* tp_as_number */ |
1244 | | 0, /* tp_as_sequence */ |
1245 | | 0, /* tp_as_mapping */ |
1246 | | 0, /* tp_hash */ |
1247 | | 0, /* tp_call */ |
1248 | | 0, /* tp_str */ |
1249 | | PyObject_GenericGetAttr, /* tp_getattro */ |
1250 | | 0, /* tp_setattro */ |
1251 | | 0, /* tp_as_buffer */ |
1252 | | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
1253 | | 0, /* tp_doc */ |
1254 | | tupleiter_traverse, /* tp_traverse */ |
1255 | | 0, /* tp_clear */ |
1256 | | 0, /* tp_richcompare */ |
1257 | | 0, /* tp_weaklistoffset */ |
1258 | | PyObject_SelfIter, /* tp_iter */ |
1259 | | tupleiter_next, /* tp_iternext */ |
1260 | | tupleiter_methods, /* tp_methods */ |
1261 | | 0, |
1262 | | }; |
1263 | | |
1264 | | static PyObject * |
1265 | | tuple_iter(PyObject *seq) |
1266 | 9.43M | { |
1267 | 9.43M | if (!PyTuple_Check(seq)) { |
1268 | 0 | PyErr_BadInternalCall(); |
1269 | 0 | return NULL; |
1270 | 0 | } |
1271 | 9.43M | _PyTupleIterObject *it = _Py_FREELIST_POP(_PyTupleIterObject, tuple_iters); |
1272 | 9.43M | if (it == NULL) { |
1273 | 5.38M | it = PyObject_GC_New(_PyTupleIterObject, &PyTupleIter_Type); |
1274 | 5.38M | if (it == NULL) |
1275 | 0 | return NULL; |
1276 | 5.38M | } |
1277 | 9.43M | it->it_index = 0; |
1278 | 9.43M | it->it_seq = (PyTupleObject *)Py_NewRef(seq); |
1279 | 9.43M | _PyObject_GC_TRACK(it); |
1280 | 9.43M | return (PyObject *)it; |
1281 | 9.43M | } |
1282 | | |
1283 | | |
1284 | | /************* |
1285 | | * freelists * |
1286 | | *************/ |
1287 | | |
1288 | | static inline int |
1289 | | maybe_freelist_push(PyTupleObject *op) |
1290 | 562M | { |
1291 | 562M | if (!Py_IS_TYPE(op, &PyTuple_Type)) { |
1292 | 583k | return 0; |
1293 | 583k | } |
1294 | 561M | Py_ssize_t index = Py_SIZE(op) - 1; |
1295 | 561M | if (index < PyTuple_MAXSAVESIZE) { |
1296 | 537M | return _Py_FREELIST_PUSH(tuples[index], op, Py_tuple_MAXFREELIST); |
1297 | 537M | } |
1298 | 24.4M | return 0; |
1299 | 561M | } |
1300 | | |
1301 | | /* Print summary info about the state of the optimized allocator */ |
1302 | | void |
1303 | | _PyTuple_DebugMallocStats(FILE *out) |
1304 | 0 | { |
1305 | 0 | PyTypeObject *type = &PyTuple_Type; |
1306 | 0 | for (int i = 0; i < PyTuple_MAXSAVESIZE; i++) { |
1307 | 0 | int len = i + 1; |
1308 | 0 | char buf[128]; |
1309 | 0 | PyOS_snprintf(buf, sizeof(buf), |
1310 | 0 | "free %d-sized PyTupleObject", len); |
1311 | 0 | size_t size = _PyType_PreHeaderSize(type) + _PyObject_VAR_SIZE(type, len); |
1312 | 0 | _PyDebugAllocatorStats(out, buf, _Py_FREELIST_SIZE(tuples[i]), size); |
1313 | 0 | } |
1314 | 0 | } |