1 /* List object implementation */
2
3 #include "Python.h"
4 #include "pycore_abstract.h" // _PyIndex_Check()
5 #include "pycore_ceval.h" // _PyEval_GetBuiltin()
6 #include "pycore_dict.h" // _PyDictViewObject
7 #include "pycore_pyatomic_ft_wrappers.h"
8 #include "pycore_interp.h" // PyInterpreterState.list
9 #include "pycore_list.h" // struct _Py_list_freelist, _PyListIterObject
10 #include "pycore_long.h" // _PyLong_DigitCount
11 #include "pycore_modsupport.h" // _PyArg_NoKwnames()
12 #include "pycore_object.h" // _PyObject_GC_TRACK(), _PyDebugAllocatorStats()
13 #include "pycore_tuple.h" // _PyTuple_FromArray()
14 #include "pycore_setobject.h" // _PySet_NextEntry()
15 #include <stddef.h>
16
17 /*[clinic input]
18 class list "PyListObject *" "&PyList_Type"
19 [clinic start generated code]*/
20 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=f9b222678f9f71e0]*/
21
22 #include "clinic/listobject.c.h"
23
24 _Py_DECLARE_STR(list_err, "list index out of range");
25
26 #ifdef WITH_FREELISTS
27 static struct _Py_list_freelist *
get_list_freelist(void)28 get_list_freelist(void)
29 {
30 struct _Py_object_freelists *freelists = _Py_object_freelists_GET();
31 assert(freelists != NULL);
32 return &freelists->lists;
33 }
34 #endif
35
36 #ifdef Py_GIL_DISABLED
37 typedef struct {
38 Py_ssize_t allocated;
39 PyObject *ob_item[];
40 } _PyListArray;
41
42 static _PyListArray *
list_allocate_array(size_t capacity)43 list_allocate_array(size_t capacity)
44 {
45 if (capacity > PY_SSIZE_T_MAX/sizeof(PyObject*) - 1) {
46 return NULL;
47 }
48 _PyListArray *array = PyMem_Malloc(sizeof(_PyListArray) + capacity * sizeof(PyObject *));
49 if (array == NULL) {
50 return NULL;
51 }
52 array->allocated = capacity;
53 return array;
54 }
55
56 static Py_ssize_t
list_capacity(PyObject ** items)57 list_capacity(PyObject **items)
58 {
59 _PyListArray *array = _Py_CONTAINER_OF(items, _PyListArray, ob_item);
60 return array->allocated;
61 }
62 #endif
63
64 static void
free_list_items(PyObject ** items,bool use_qsbr)65 free_list_items(PyObject** items, bool use_qsbr)
66 {
67 #ifdef Py_GIL_DISABLED
68 _PyListArray *array = _Py_CONTAINER_OF(items, _PyListArray, ob_item);
69 if (use_qsbr) {
70 _PyMem_FreeDelayed(array);
71 }
72 else {
73 PyMem_Free(array);
74 }
75 #else
76 PyMem_Free(items);
77 #endif
78 }
79
80 static void
ensure_shared_on_resize(PyListObject * self)81 ensure_shared_on_resize(PyListObject *self)
82 {
83 #ifdef Py_GIL_DISABLED
84 // Ensure that the list array is freed using QSBR if we are not the
85 // owning thread.
86 if (!_Py_IsOwnedByCurrentThread((PyObject *)self) &&
87 !_PyObject_GC_IS_SHARED(self))
88 {
89 _PyObject_GC_SET_SHARED(self);
90 }
91 #endif
92 }
93
94 /* Ensure ob_item has room for at least newsize elements, and set
95 * ob_size to newsize. If newsize > ob_size on entry, the content
96 * of the new slots at exit is undefined heap trash; it's the caller's
97 * responsibility to overwrite them with sane values.
98 * The number of allocated elements may grow, shrink, or stay the same.
99 * Failure is impossible if newsize <= self.allocated on entry, although
100 * that partly relies on an assumption that the system realloc() never
101 * fails when passed a number of bytes <= the number of bytes last
102 * allocated (the C standard doesn't guarantee this, but it's hard to
103 * imagine a realloc implementation where it wouldn't be true).
104 * Note that self->ob_item may change, and even if newsize is less
105 * than ob_size on entry.
106 */
107 static int
list_resize(PyListObject * self,Py_ssize_t newsize)108 list_resize(PyListObject *self, Py_ssize_t newsize)
109 {
110 size_t new_allocated, target_bytes;
111 Py_ssize_t allocated = self->allocated;
112
113 /* Bypass realloc() when a previous overallocation is large enough
114 to accommodate the newsize. If the newsize falls lower than half
115 the allocated size, then proceed with the realloc() to shrink the list.
116 */
117 if (allocated >= newsize && newsize >= (allocated >> 1)) {
118 assert(self->ob_item != NULL || newsize == 0);
119 Py_SET_SIZE(self, newsize);
120 return 0;
121 }
122
123 /* This over-allocates proportional to the list size, making room
124 * for additional growth. The over-allocation is mild, but is
125 * enough to give linear-time amortized behavior over a long
126 * sequence of appends() in the presence of a poorly-performing
127 * system realloc().
128 * Add padding to make the allocated size multiple of 4.
129 * The growth pattern is: 0, 4, 8, 16, 24, 32, 40, 52, 64, 76, ...
130 * Note: new_allocated won't overflow because the largest possible value
131 * is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
132 */
133 new_allocated = ((size_t)newsize + (newsize >> 3) + 6) & ~(size_t)3;
134 /* Do not overallocate if the new size is closer to overallocated size
135 * than to the old size.
136 */
137 if (newsize - Py_SIZE(self) > (Py_ssize_t)(new_allocated - newsize))
138 new_allocated = ((size_t)newsize + 3) & ~(size_t)3;
139
140 if (newsize == 0)
141 new_allocated = 0;
142
143 ensure_shared_on_resize(self);
144
145 #ifdef Py_GIL_DISABLED
146 _PyListArray *array = list_allocate_array(new_allocated);
147 if (array == NULL) {
148 PyErr_NoMemory();
149 return -1;
150 }
151 PyObject **old_items = self->ob_item;
152 if (self->ob_item) {
153 if (new_allocated < (size_t)allocated) {
154 target_bytes = new_allocated * sizeof(PyObject*);
155 }
156 else {
157 target_bytes = allocated * sizeof(PyObject*);
158 }
159 memcpy(array->ob_item, self->ob_item, target_bytes);
160 }
161 if (new_allocated > (size_t)allocated) {
162 memset(array->ob_item + allocated, 0, sizeof(PyObject *) * (new_allocated - allocated));
163 }
164 _Py_atomic_store_ptr_release(&self->ob_item, &array->ob_item);
165 self->allocated = new_allocated;
166 Py_SET_SIZE(self, newsize);
167 if (old_items != NULL) {
168 free_list_items(old_items, _PyObject_GC_IS_SHARED(self));
169 }
170 #else
171 PyObject **items;
172 if (new_allocated <= (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
173 target_bytes = new_allocated * sizeof(PyObject *);
174 items = (PyObject **)PyMem_Realloc(self->ob_item, target_bytes);
175 }
176 else {
177 // integer overflow
178 items = NULL;
179 }
180 if (items == NULL) {
181 PyErr_NoMemory();
182 return -1;
183 }
184 self->ob_item = items;
185 Py_SET_SIZE(self, newsize);
186 self->allocated = new_allocated;
187 #endif
188 return 0;
189 }
190
191 static int
list_preallocate_exact(PyListObject * self,Py_ssize_t size)192 list_preallocate_exact(PyListObject *self, Py_ssize_t size)
193 {
194 PyObject **items;
195 assert(self->ob_item == NULL);
196 assert(size > 0);
197
198 /* Since the Python memory allocator has granularity of 16 bytes on 64-bit
199 * platforms (8 on 32-bit), there is no benefit of allocating space for
200 * the odd number of items, and there is no drawback of rounding the
201 * allocated size up to the nearest even number.
202 */
203 size = (size + 1) & ~(size_t)1;
204 #ifdef Py_GIL_DISABLED
205 _PyListArray *array = list_allocate_array(size);
206 if (array == NULL) {
207 PyErr_NoMemory();
208 return -1;
209 }
210 items = array->ob_item;
211 memset(items, 0, size * sizeof(PyObject *));
212 #else
213 items = PyMem_New(PyObject*, size);
214 if (items == NULL) {
215 PyErr_NoMemory();
216 return -1;
217 }
218 #endif
219 FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item, items);
220 self->allocated = size;
221 return 0;
222 }
223
224 void
_PyList_ClearFreeList(struct _Py_object_freelists * freelists,int is_finalization)225 _PyList_ClearFreeList(struct _Py_object_freelists *freelists, int is_finalization)
226 {
227 #ifdef WITH_FREELISTS
228 struct _Py_list_freelist *state = &freelists->lists;
229 while (state->numfree > 0) {
230 PyListObject *op = state->items[--state->numfree];
231 assert(PyList_CheckExact(op));
232 PyObject_GC_Del(op);
233 }
234 if (is_finalization) {
235 state->numfree = -1;
236 }
237 #endif
238 }
239
240 /* Print summary info about the state of the optimized allocator */
241 void
_PyList_DebugMallocStats(FILE * out)242 _PyList_DebugMallocStats(FILE *out)
243 {
244 #ifdef WITH_FREELISTS
245 struct _Py_list_freelist *list_freelist = get_list_freelist();
246 _PyDebugAllocatorStats(out,
247 "free PyListObject",
248 list_freelist->numfree, sizeof(PyListObject));
249 #endif
250 }
251
252 PyObject *
PyList_New(Py_ssize_t size)253 PyList_New(Py_ssize_t size)
254 {
255 PyListObject *op;
256
257 if (size < 0) {
258 PyErr_BadInternalCall();
259 return NULL;
260 }
261
262 #ifdef WITH_FREELISTS
263 struct _Py_list_freelist *list_freelist = get_list_freelist();
264 if (PyList_MAXFREELIST && list_freelist->numfree > 0) {
265 list_freelist->numfree--;
266 op = list_freelist->items[list_freelist->numfree];
267 OBJECT_STAT_INC(from_freelist);
268 _Py_NewReference((PyObject *)op);
269 }
270 else
271 #endif
272 {
273 op = PyObject_GC_New(PyListObject, &PyList_Type);
274 if (op == NULL) {
275 return NULL;
276 }
277 }
278 if (size <= 0) {
279 op->ob_item = NULL;
280 }
281 else {
282 #ifdef Py_GIL_DISABLED
283 _PyListArray *array = list_allocate_array(size);
284 if (array == NULL) {
285 Py_DECREF(op);
286 return PyErr_NoMemory();
287 }
288 memset(&array->ob_item, 0, size * sizeof(PyObject *));
289 op->ob_item = array->ob_item;
290 #else
291 op->ob_item = (PyObject **) PyMem_Calloc(size, sizeof(PyObject *));
292 #endif
293 if (op->ob_item == NULL) {
294 Py_DECREF(op);
295 return PyErr_NoMemory();
296 }
297 }
298 Py_SET_SIZE(op, size);
299 op->allocated = size;
300 _PyObject_GC_TRACK(op);
301 return (PyObject *) op;
302 }
303
304 static PyObject *
list_new_prealloc(Py_ssize_t size)305 list_new_prealloc(Py_ssize_t size)
306 {
307 assert(size > 0);
308 PyListObject *op = (PyListObject *) PyList_New(0);
309 if (op == NULL) {
310 return NULL;
311 }
312 assert(op->ob_item == NULL);
313 #ifdef Py_GIL_DISABLED
314 _PyListArray *array = list_allocate_array(size);
315 if (array == NULL) {
316 Py_DECREF(op);
317 return PyErr_NoMemory();
318 }
319 op->ob_item = array->ob_item;
320 #else
321 op->ob_item = PyMem_New(PyObject *, size);
322 if (op->ob_item == NULL) {
323 Py_DECREF(op);
324 return PyErr_NoMemory();
325 }
326 #endif
327 op->allocated = size;
328 return (PyObject *) op;
329 }
330
331 Py_ssize_t
PyList_Size(PyObject * op)332 PyList_Size(PyObject *op)
333 {
334 if (!PyList_Check(op)) {
335 PyErr_BadInternalCall();
336 return -1;
337 }
338 else {
339 return PyList_GET_SIZE(op);
340 }
341 }
342
343 static inline int
valid_index(Py_ssize_t i,Py_ssize_t limit)344 valid_index(Py_ssize_t i, Py_ssize_t limit)
345 {
346 /* The cast to size_t lets us use just a single comparison
347 to check whether i is in the range: 0 <= i < limit.
348
349 See: Section 14.2 "Bounds Checking" in the Agner Fog
350 optimization manual found at:
351 https://www.agner.org/optimize/optimizing_cpp.pdf
352 */
353 return (size_t) i < (size_t) limit;
354 }
355
356 #ifdef Py_GIL_DISABLED
357
358 static PyObject *
list_item_impl(PyListObject * self,Py_ssize_t idx)359 list_item_impl(PyListObject *self, Py_ssize_t idx)
360 {
361 PyObject *item = NULL;
362 Py_BEGIN_CRITICAL_SECTION(self);
363 if (!_PyObject_GC_IS_SHARED(self)) {
364 _PyObject_GC_SET_SHARED(self);
365 }
366 Py_ssize_t size = Py_SIZE(self);
367 if (!valid_index(idx, size)) {
368 goto exit;
369 }
370 #ifdef Py_GIL_DISABLED
371 item = _Py_NewRefWithLock(self->ob_item[idx]);
372 #else
373 item = Py_NewRef(self->ob_item[idx]);
374 #endif
375 exit:
376 Py_END_CRITICAL_SECTION();
377 return item;
378 }
379
380 static inline PyObject*
list_get_item_ref(PyListObject * op,Py_ssize_t i)381 list_get_item_ref(PyListObject *op, Py_ssize_t i)
382 {
383 if (!_Py_IsOwnedByCurrentThread((PyObject *)op) && !_PyObject_GC_IS_SHARED(op)) {
384 return list_item_impl(op, i);
385 }
386 // Need atomic operation for the getting size.
387 Py_ssize_t size = PyList_GET_SIZE(op);
388 if (!valid_index(i, size)) {
389 return NULL;
390 }
391 PyObject **ob_item = _Py_atomic_load_ptr(&op->ob_item);
392 if (ob_item == NULL) {
393 return NULL;
394 }
395 Py_ssize_t cap = list_capacity(ob_item);
396 assert(cap != -1 && cap >= size);
397 if (!valid_index(i, cap)) {
398 return NULL;
399 }
400 PyObject *item = _Py_TryXGetRef(&ob_item[i]);
401 if (item == NULL) {
402 return list_item_impl(op, i);
403 }
404 return item;
405 }
406 #else
407 static inline PyObject*
list_get_item_ref(PyListObject * op,Py_ssize_t i)408 list_get_item_ref(PyListObject *op, Py_ssize_t i)
409 {
410 if (!valid_index(i, Py_SIZE(op))) {
411 return NULL;
412 }
413 return Py_NewRef(PyList_GET_ITEM(op, i));
414 }
415 #endif
416
417 PyObject *
PyList_GetItem(PyObject * op,Py_ssize_t i)418 PyList_GetItem(PyObject *op, Py_ssize_t i)
419 {
420 if (!PyList_Check(op)) {
421 PyErr_BadInternalCall();
422 return NULL;
423 }
424 if (!valid_index(i, Py_SIZE(op))) {
425 _Py_DECLARE_STR(list_err, "list index out of range");
426 PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
427 return NULL;
428 }
429 return ((PyListObject *)op) -> ob_item[i];
430 }
431
432 PyObject *
PyList_GetItemRef(PyObject * op,Py_ssize_t i)433 PyList_GetItemRef(PyObject *op, Py_ssize_t i)
434 {
435 if (!PyList_Check(op)) {
436 PyErr_SetString(PyExc_TypeError, "expected a list");
437 return NULL;
438 }
439 PyObject *item = list_get_item_ref((PyListObject *)op, i);
440 if (item == NULL) {
441 _Py_DECLARE_STR(list_err, "list index out of range");
442 PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
443 return NULL;
444 }
445 return item;
446 }
447
448 int
PyList_SetItem(PyObject * op,Py_ssize_t i,PyObject * newitem)449 PyList_SetItem(PyObject *op, Py_ssize_t i,
450 PyObject *newitem)
451 {
452 PyObject **p;
453 if (!PyList_Check(op)) {
454 Py_XDECREF(newitem);
455 PyErr_BadInternalCall();
456 return -1;
457 }
458 int ret;
459 PyListObject *self = ((PyListObject *)op);
460 Py_BEGIN_CRITICAL_SECTION(self);
461 if (!valid_index(i, Py_SIZE(self))) {
462 Py_XDECREF(newitem);
463 PyErr_SetString(PyExc_IndexError,
464 "list assignment index out of range");
465 ret = -1;
466 goto end;
467 }
468 p = self->ob_item + i;
469 Py_XSETREF(*p, newitem);
470 ret = 0;
471 end:;
472 Py_END_CRITICAL_SECTION();
473 return ret;
474 }
475
476 static int
ins1(PyListObject * self,Py_ssize_t where,PyObject * v)477 ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
478 {
479 Py_ssize_t i, n = Py_SIZE(self);
480 PyObject **items;
481 if (v == NULL) {
482 PyErr_BadInternalCall();
483 return -1;
484 }
485
486 assert((size_t)n + 1 < PY_SSIZE_T_MAX);
487 if (list_resize(self, n+1) < 0)
488 return -1;
489
490 if (where < 0) {
491 where += n;
492 if (where < 0)
493 where = 0;
494 }
495 if (where > n)
496 where = n;
497 items = self->ob_item;
498 for (i = n; --i >= where; )
499 items[i+1] = items[i];
500 items[where] = Py_NewRef(v);
501 return 0;
502 }
503
504 int
PyList_Insert(PyObject * op,Py_ssize_t where,PyObject * newitem)505 PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
506 {
507 if (!PyList_Check(op)) {
508 PyErr_BadInternalCall();
509 return -1;
510 }
511 PyListObject *self = (PyListObject *)op;
512 int err;
513 Py_BEGIN_CRITICAL_SECTION(self);
514 err = ins1(self, where, newitem);
515 Py_END_CRITICAL_SECTION();
516 return err;
517 }
518
519 /* internal, used by _PyList_AppendTakeRef */
520 int
_PyList_AppendTakeRefListResize(PyListObject * self,PyObject * newitem)521 _PyList_AppendTakeRefListResize(PyListObject *self, PyObject *newitem)
522 {
523 Py_ssize_t len = Py_SIZE(self);
524 assert(self->allocated == -1 || self->allocated == len);
525 if (list_resize(self, len + 1) < 0) {
526 Py_DECREF(newitem);
527 return -1;
528 }
529 FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item[len], newitem);
530 return 0;
531 }
532
533 int
PyList_Append(PyObject * op,PyObject * newitem)534 PyList_Append(PyObject *op, PyObject *newitem)
535 {
536 if (PyList_Check(op) && (newitem != NULL)) {
537 int ret;
538 Py_BEGIN_CRITICAL_SECTION(op);
539 ret = _PyList_AppendTakeRef((PyListObject *)op, Py_NewRef(newitem));
540 Py_END_CRITICAL_SECTION();
541 return ret;
542 }
543 PyErr_BadInternalCall();
544 return -1;
545 }
546
547 /* Methods */
548
549 static void
list_dealloc(PyObject * self)550 list_dealloc(PyObject *self)
551 {
552 PyListObject *op = (PyListObject *)self;
553 Py_ssize_t i;
554 PyObject_GC_UnTrack(op);
555 Py_TRASHCAN_BEGIN(op, list_dealloc)
556 if (op->ob_item != NULL) {
557 /* Do it backwards, for Christian Tismer.
558 There's a simple test case where somehow this reduces
559 thrashing when a *very* large list is created and
560 immediately deleted. */
561 i = Py_SIZE(op);
562 while (--i >= 0) {
563 Py_XDECREF(op->ob_item[i]);
564 }
565 free_list_items(op->ob_item, false);
566 }
567 #ifdef WITH_FREELISTS
568 struct _Py_list_freelist *list_freelist = get_list_freelist();
569 if (list_freelist->numfree < PyList_MAXFREELIST && list_freelist->numfree >= 0 && PyList_CheckExact(op)) {
570 list_freelist->items[list_freelist->numfree++] = op;
571 OBJECT_STAT_INC(to_freelist);
572 }
573 else
574 #endif
575 {
576 Py_TYPE(op)->tp_free((PyObject *)op);
577 }
578 Py_TRASHCAN_END
579 }
580
581 static PyObject *
list_repr_impl(PyListObject * v)582 list_repr_impl(PyListObject *v)
583 {
584 PyObject *s;
585 _PyUnicodeWriter writer;
586 Py_ssize_t i = Py_ReprEnter((PyObject*)v);
587 if (i != 0) {
588 return i > 0 ? PyUnicode_FromString("[...]") : NULL;
589 }
590
591 _PyUnicodeWriter_Init(&writer);
592 writer.overallocate = 1;
593 /* "[" + "1" + ", 2" * (len - 1) + "]" */
594 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
595
596 if (_PyUnicodeWriter_WriteChar(&writer, '[') < 0)
597 goto error;
598
599 /* Do repr() on each element. Note that this may mutate the list,
600 so must refetch the list size on each iteration. */
601 for (i = 0; i < Py_SIZE(v); ++i) {
602 if (i > 0) {
603 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
604 goto error;
605 }
606
607 s = PyObject_Repr(v->ob_item[i]);
608 if (s == NULL)
609 goto error;
610
611 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
612 Py_DECREF(s);
613 goto error;
614 }
615 Py_DECREF(s);
616 }
617
618 writer.overallocate = 0;
619 if (_PyUnicodeWriter_WriteChar(&writer, ']') < 0)
620 goto error;
621
622 Py_ReprLeave((PyObject *)v);
623 return _PyUnicodeWriter_Finish(&writer);
624
625 error:
626 _PyUnicodeWriter_Dealloc(&writer);
627 Py_ReprLeave((PyObject *)v);
628 return NULL;
629 }
630
631 static PyObject *
list_repr(PyObject * self)632 list_repr(PyObject *self)
633 {
634 if (PyList_GET_SIZE(self) == 0) {
635 return PyUnicode_FromString("[]");
636 }
637 PyListObject *v = (PyListObject *)self;
638 PyObject *ret = NULL;
639 Py_BEGIN_CRITICAL_SECTION(v);
640 ret = list_repr_impl(v);
641 Py_END_CRITICAL_SECTION();
642 return ret;
643 }
644
645 static Py_ssize_t
list_length(PyObject * a)646 list_length(PyObject *a)
647 {
648 return PyList_GET_SIZE(a);
649 }
650
651 static int
list_contains(PyObject * aa,PyObject * el)652 list_contains(PyObject *aa, PyObject *el)
653 {
654
655 for (Py_ssize_t i = 0; ; i++) {
656 PyObject *item = list_get_item_ref((PyListObject *)aa, i);
657 if (item == NULL) {
658 // out-of-bounds
659 return 0;
660 }
661 int cmp = PyObject_RichCompareBool(item, el, Py_EQ);
662 Py_DECREF(item);
663 if (cmp != 0) {
664 return cmp;
665 }
666 }
667 return 0;
668 }
669
670 static PyObject *
list_item(PyObject * aa,Py_ssize_t i)671 list_item(PyObject *aa, Py_ssize_t i)
672 {
673 PyListObject *a = (PyListObject *)aa;
674 if (!valid_index(i, PyList_GET_SIZE(a))) {
675 PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
676 return NULL;
677 }
678 PyObject *item;
679 #ifdef Py_GIL_DISABLED
680 item = list_get_item_ref(a, i);
681 if (item == NULL) {
682 PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
683 return NULL;
684 }
685 #else
686 item = Py_NewRef(a->ob_item[i]);
687 #endif
688 return item;
689 }
690
691 static PyObject *
list_slice_lock_held(PyListObject * a,Py_ssize_t ilow,Py_ssize_t ihigh)692 list_slice_lock_held(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
693 {
694 PyListObject *np;
695 PyObject **src, **dest;
696 Py_ssize_t i, len;
697 len = ihigh - ilow;
698 if (len <= 0) {
699 return PyList_New(0);
700 }
701 np = (PyListObject *) list_new_prealloc(len);
702 if (np == NULL)
703 return NULL;
704
705 src = a->ob_item + ilow;
706 dest = np->ob_item;
707 for (i = 0; i < len; i++) {
708 PyObject *v = src[i];
709 dest[i] = Py_NewRef(v);
710 }
711 Py_SET_SIZE(np, len);
712 return (PyObject *)np;
713 }
714
715 PyObject *
PyList_GetSlice(PyObject * a,Py_ssize_t ilow,Py_ssize_t ihigh)716 PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
717 {
718 if (!PyList_Check(a)) {
719 PyErr_BadInternalCall();
720 return NULL;
721 }
722 PyObject *ret;
723 Py_BEGIN_CRITICAL_SECTION(a);
724 if (ilow < 0) {
725 ilow = 0;
726 }
727 else if (ilow > Py_SIZE(a)) {
728 ilow = Py_SIZE(a);
729 }
730 if (ihigh < ilow) {
731 ihigh = ilow;
732 }
733 else if (ihigh > Py_SIZE(a)) {
734 ihigh = Py_SIZE(a);
735 }
736 ret = list_slice_lock_held((PyListObject *)a, ilow, ihigh);
737 Py_END_CRITICAL_SECTION();
738 return ret;
739 }
740
741 static PyObject *
list_concat_lock_held(PyListObject * a,PyListObject * b)742 list_concat_lock_held(PyListObject *a, PyListObject *b)
743 {
744 Py_ssize_t size;
745 Py_ssize_t i;
746 PyObject **src, **dest;
747 PyListObject *np;
748 assert((size_t)Py_SIZE(a) + (size_t)Py_SIZE(b) < PY_SSIZE_T_MAX);
749 size = Py_SIZE(a) + Py_SIZE(b);
750 if (size == 0) {
751 return PyList_New(0);
752 }
753 np = (PyListObject *) list_new_prealloc(size);
754 if (np == NULL) {
755 return NULL;
756 }
757 src = a->ob_item;
758 dest = np->ob_item;
759 for (i = 0; i < Py_SIZE(a); i++) {
760 PyObject *v = src[i];
761 dest[i] = Py_NewRef(v);
762 }
763 src = b->ob_item;
764 dest = np->ob_item + Py_SIZE(a);
765 for (i = 0; i < Py_SIZE(b); i++) {
766 PyObject *v = src[i];
767 dest[i] = Py_NewRef(v);
768 }
769 Py_SET_SIZE(np, size);
770 return (PyObject *)np;
771 }
772
773 static PyObject *
list_concat(PyObject * aa,PyObject * bb)774 list_concat(PyObject *aa, PyObject *bb)
775 {
776 if (!PyList_Check(bb)) {
777 PyErr_Format(PyExc_TypeError,
778 "can only concatenate list (not \"%.200s\") to list",
779 Py_TYPE(bb)->tp_name);
780 return NULL;
781 }
782 PyListObject *a = (PyListObject *)aa;
783 PyListObject *b = (PyListObject *)bb;
784 PyObject *ret;
785 Py_BEGIN_CRITICAL_SECTION2(a, b);
786 ret = list_concat_lock_held(a, b);
787 Py_END_CRITICAL_SECTION2();
788 return ret;
789 }
790
791 static PyObject *
list_repeat_lock_held(PyListObject * a,Py_ssize_t n)792 list_repeat_lock_held(PyListObject *a, Py_ssize_t n)
793 {
794 const Py_ssize_t input_size = Py_SIZE(a);
795 if (input_size == 0 || n <= 0)
796 return PyList_New(0);
797 assert(n > 0);
798
799 if (input_size > PY_SSIZE_T_MAX / n)
800 return PyErr_NoMemory();
801 Py_ssize_t output_size = input_size * n;
802
803 PyListObject *np = (PyListObject *) list_new_prealloc(output_size);
804 if (np == NULL)
805 return NULL;
806
807 PyObject **dest = np->ob_item;
808 if (input_size == 1) {
809 PyObject *elem = a->ob_item[0];
810 _Py_RefcntAdd(elem, n);
811 PyObject **dest_end = dest + output_size;
812 while (dest < dest_end) {
813 *dest++ = elem;
814 }
815 }
816 else {
817 PyObject **src = a->ob_item;
818 PyObject **src_end = src + input_size;
819 while (src < src_end) {
820 _Py_RefcntAdd(*src, n);
821 *dest++ = *src++;
822 }
823
824 _Py_memory_repeat((char *)np->ob_item, sizeof(PyObject *)*output_size,
825 sizeof(PyObject *)*input_size);
826 }
827
828 Py_SET_SIZE(np, output_size);
829 return (PyObject *) np;
830 }
831
832 static PyObject *
list_repeat(PyObject * aa,Py_ssize_t n)833 list_repeat(PyObject *aa, Py_ssize_t n)
834 {
835 PyObject *ret;
836 PyListObject *a = (PyListObject *)aa;
837 Py_BEGIN_CRITICAL_SECTION(a);
838 ret = list_repeat_lock_held(a, n);
839 Py_END_CRITICAL_SECTION();
840 return ret;
841 }
842
843 static void
list_clear_impl(PyListObject * a,bool is_resize)844 list_clear_impl(PyListObject *a, bool is_resize)
845 {
846 PyObject **items = a->ob_item;
847 if (items == NULL) {
848 return;
849 }
850
851 /* Because XDECREF can recursively invoke operations on
852 this list, we make it empty first. */
853 Py_ssize_t i = Py_SIZE(a);
854 Py_SET_SIZE(a, 0);
855 FT_ATOMIC_STORE_PTR_RELEASE(a->ob_item, NULL);
856 a->allocated = 0;
857 while (--i >= 0) {
858 Py_XDECREF(items[i]);
859 }
860 #ifdef Py_GIL_DISABLED
861 if (is_resize) {
862 ensure_shared_on_resize(a);
863 }
864 bool use_qsbr = is_resize && _PyObject_GC_IS_SHARED(a);
865 #else
866 bool use_qsbr = false;
867 #endif
868 free_list_items(items, use_qsbr);
869 // Note that there is no guarantee that the list is actually empty
870 // at this point, because XDECREF may have populated it indirectly again!
871 }
872
873 static void
list_clear(PyListObject * a)874 list_clear(PyListObject *a)
875 {
876 list_clear_impl(a, true);
877 }
878
879 static int
list_clear_slot(PyObject * self)880 list_clear_slot(PyObject *self)
881 {
882 list_clear_impl((PyListObject *)self, false);
883 return 0;
884 }
885
886 /* a[ilow:ihigh] = v if v != NULL.
887 * del a[ilow:ihigh] if v == NULL.
888 *
889 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
890 * guaranteed the call cannot fail.
891 */
892 static int
list_ass_slice_lock_held(PyListObject * a,Py_ssize_t ilow,Py_ssize_t ihigh,PyObject * v)893 list_ass_slice_lock_held(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
894 {
895 /* Because [X]DECREF can recursively invoke list operations on
896 this list, we must postpone all [X]DECREF activity until
897 after the list is back in its canonical shape. Therefore
898 we must allocate an additional array, 'recycle', into which
899 we temporarily copy the items that are deleted from the
900 list. :-( */
901 PyObject *recycle_on_stack[8];
902 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
903 PyObject **item;
904 PyObject **vitem = NULL;
905 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
906 Py_ssize_t n; /* # of elements in replacement list */
907 Py_ssize_t norig; /* # of elements in list getting replaced */
908 Py_ssize_t d; /* Change in size */
909 Py_ssize_t k;
910 size_t s;
911 int result = -1; /* guilty until proved innocent */
912 #define b ((PyListObject *)v)
913 if (v == NULL)
914 n = 0;
915 else {
916 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
917 if(v_as_SF == NULL)
918 goto Error;
919 n = PySequence_Fast_GET_SIZE(v_as_SF);
920 vitem = PySequence_Fast_ITEMS(v_as_SF);
921 }
922 if (ilow < 0)
923 ilow = 0;
924 else if (ilow > Py_SIZE(a))
925 ilow = Py_SIZE(a);
926
927 if (ihigh < ilow)
928 ihigh = ilow;
929 else if (ihigh > Py_SIZE(a))
930 ihigh = Py_SIZE(a);
931
932 norig = ihigh - ilow;
933 assert(norig >= 0);
934 d = n - norig;
935 if (Py_SIZE(a) + d == 0) {
936 Py_XDECREF(v_as_SF);
937 list_clear(a);
938 return 0;
939 }
940 item = a->ob_item;
941 /* recycle the items that we are about to remove */
942 s = norig * sizeof(PyObject *);
943 /* If norig == 0, item might be NULL, in which case we may not memcpy from it. */
944 if (s) {
945 if (s > sizeof(recycle_on_stack)) {
946 recycle = (PyObject **)PyMem_Malloc(s);
947 if (recycle == NULL) {
948 PyErr_NoMemory();
949 goto Error;
950 }
951 }
952 memcpy(recycle, &item[ilow], s);
953 }
954
955 if (d < 0) { /* Delete -d items */
956 Py_ssize_t tail;
957 tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
958 memmove(&item[ihigh+d], &item[ihigh], tail);
959 if (list_resize(a, Py_SIZE(a) + d) < 0) {
960 memmove(&item[ihigh], &item[ihigh+d], tail);
961 memcpy(&item[ilow], recycle, s);
962 goto Error;
963 }
964 item = a->ob_item;
965 }
966 else if (d > 0) { /* Insert d items */
967 k = Py_SIZE(a);
968 if (list_resize(a, k+d) < 0)
969 goto Error;
970 item = a->ob_item;
971 memmove(&item[ihigh+d], &item[ihigh],
972 (k - ihigh)*sizeof(PyObject *));
973 }
974 for (k = 0; k < n; k++, ilow++) {
975 PyObject *w = vitem[k];
976 item[ilow] = Py_XNewRef(w);
977 }
978 for (k = norig - 1; k >= 0; --k)
979 Py_XDECREF(recycle[k]);
980 result = 0;
981 Error:
982 if (recycle != recycle_on_stack)
983 PyMem_Free(recycle);
984 Py_XDECREF(v_as_SF);
985 return result;
986 #undef b
987 }
988
989 static int
list_ass_slice(PyListObject * a,Py_ssize_t ilow,Py_ssize_t ihigh,PyObject * v)990 list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
991 {
992 int ret;
993 if (a == (PyListObject *)v) {
994 Py_BEGIN_CRITICAL_SECTION(a);
995 Py_ssize_t n = PyList_GET_SIZE(a);
996 PyObject *copy = list_slice_lock_held(a, 0, n);
997 if (copy == NULL) {
998 return -1;
999 }
1000 ret = list_ass_slice_lock_held(a, ilow, ihigh, copy);
1001 Py_DECREF(copy);
1002 Py_END_CRITICAL_SECTION();
1003 }
1004 else if (v != NULL && PyList_CheckExact(v)) {
1005 Py_BEGIN_CRITICAL_SECTION2(a, v);
1006 ret = list_ass_slice_lock_held(a, ilow, ihigh, v);
1007 Py_END_CRITICAL_SECTION2();
1008 }
1009 else {
1010 Py_BEGIN_CRITICAL_SECTION(a);
1011 ret = list_ass_slice_lock_held(a, ilow, ihigh, v);
1012 Py_END_CRITICAL_SECTION();
1013 }
1014 return ret;
1015 }
1016
1017 int
PyList_SetSlice(PyObject * a,Py_ssize_t ilow,Py_ssize_t ihigh,PyObject * v)1018 PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
1019 {
1020 if (!PyList_Check(a)) {
1021 PyErr_BadInternalCall();
1022 return -1;
1023 }
1024 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
1025 }
1026
1027 static int
list_inplace_repeat_lock_held(PyListObject * self,Py_ssize_t n)1028 list_inplace_repeat_lock_held(PyListObject *self, Py_ssize_t n)
1029 {
1030 Py_ssize_t input_size = PyList_GET_SIZE(self);
1031 if (input_size == 0 || n == 1) {
1032 return 0;
1033 }
1034
1035 if (n < 1) {
1036 list_clear(self);
1037 return 0;
1038 }
1039
1040 if (input_size > PY_SSIZE_T_MAX / n) {
1041 PyErr_NoMemory();
1042 return -1;
1043 }
1044 Py_ssize_t output_size = input_size * n;
1045
1046 if (list_resize(self, output_size) < 0) {
1047 return -1;
1048 }
1049
1050 PyObject **items = self->ob_item;
1051 for (Py_ssize_t j = 0; j < input_size; j++) {
1052 _Py_RefcntAdd(items[j], n-1);
1053 }
1054 _Py_memory_repeat((char *)items, sizeof(PyObject *)*output_size,
1055 sizeof(PyObject *)*input_size);
1056 return 0;
1057 }
1058
1059 static PyObject *
list_inplace_repeat(PyObject * _self,Py_ssize_t n)1060 list_inplace_repeat(PyObject *_self, Py_ssize_t n)
1061 {
1062 PyObject *ret;
1063 PyListObject *self = (PyListObject *) _self;
1064 Py_BEGIN_CRITICAL_SECTION(self);
1065 if (list_inplace_repeat_lock_held(self, n) < 0) {
1066 ret = NULL;
1067 }
1068 else {
1069 ret = Py_NewRef(self);
1070 }
1071 Py_END_CRITICAL_SECTION();
1072 return ret;
1073 }
1074
1075 static int
list_ass_item_lock_held(PyListObject * a,Py_ssize_t i,PyObject * v)1076 list_ass_item_lock_held(PyListObject *a, Py_ssize_t i, PyObject *v)
1077 {
1078 if (!valid_index(i, Py_SIZE(a))) {
1079 PyErr_SetString(PyExc_IndexError,
1080 "list assignment index out of range");
1081 return -1;
1082 }
1083 PyObject *tmp = a->ob_item[i];
1084 if (v == NULL) {
1085 Py_ssize_t size = Py_SIZE(a);
1086 for (Py_ssize_t idx = i; idx < size - 1; idx++) {
1087 FT_ATOMIC_STORE_PTR_RELAXED(a->ob_item[idx], a->ob_item[idx + 1]);
1088 }
1089 Py_SET_SIZE(a, size - 1);
1090 }
1091 else {
1092 FT_ATOMIC_STORE_PTR_RELEASE(a->ob_item[i], Py_NewRef(v));
1093 }
1094 Py_DECREF(tmp);
1095 return 0;
1096 }
1097
1098 static int
list_ass_item(PyObject * aa,Py_ssize_t i,PyObject * v)1099 list_ass_item(PyObject *aa, Py_ssize_t i, PyObject *v)
1100 {
1101 int ret;
1102 PyListObject *a = (PyListObject *)aa;
1103 Py_BEGIN_CRITICAL_SECTION(a);
1104 ret = list_ass_item_lock_held(a, i, v);
1105 Py_END_CRITICAL_SECTION();
1106 return ret;
1107 }
1108
1109 /*[clinic input]
1110 @critical_section
1111 list.insert
1112
1113 index: Py_ssize_t
1114 object: object
1115 /
1116
1117 Insert object before index.
1118 [clinic start generated code]*/
1119
1120 static PyObject *
list_insert_impl(PyListObject * self,Py_ssize_t index,PyObject * object)1121 list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object)
1122 /*[clinic end generated code: output=7f35e32f60c8cb78 input=b1987ca998a4ae2d]*/
1123 {
1124 if (ins1(self, index, object) == 0) {
1125 Py_RETURN_NONE;
1126 }
1127 return NULL;
1128 }
1129
1130 /*[clinic input]
1131 @critical_section
1132 list.clear as py_list_clear
1133
1134 Remove all items from list.
1135 [clinic start generated code]*/
1136
1137 static PyObject *
py_list_clear_impl(PyListObject * self)1138 py_list_clear_impl(PyListObject *self)
1139 /*[clinic end generated code: output=83726743807e3518 input=e285b7f09051a9ba]*/
1140 {
1141 list_clear(self);
1142 Py_RETURN_NONE;
1143 }
1144
1145 /*[clinic input]
1146 @critical_section
1147 list.copy
1148
1149 Return a shallow copy of the list.
1150 [clinic start generated code]*/
1151
1152 static PyObject *
list_copy_impl(PyListObject * self)1153 list_copy_impl(PyListObject *self)
1154 /*[clinic end generated code: output=ec6b72d6209d418e input=81c54b0c7bb4f73d]*/
1155 {
1156 return list_slice_lock_held(self, 0, Py_SIZE(self));
1157 }
1158
1159 /*[clinic input]
1160 @critical_section
1161 list.append
1162
1163 object: object
1164 /
1165
1166 Append object to the end of the list.
1167 [clinic start generated code]*/
1168
1169 static PyObject *
list_append_impl(PyListObject * self,PyObject * object)1170 list_append_impl(PyListObject *self, PyObject *object)
1171 /*[clinic end generated code: output=78423561d92ed405 input=122b0853de54004f]*/
1172 {
1173 if (_PyList_AppendTakeRef(self, Py_NewRef(object)) < 0) {
1174 return NULL;
1175 }
1176 Py_RETURN_NONE;
1177 }
1178
1179 static int
list_extend_fast(PyListObject * self,PyObject * iterable)1180 list_extend_fast(PyListObject *self, PyObject *iterable)
1181 {
1182 Py_ssize_t n = PySequence_Fast_GET_SIZE(iterable);
1183 if (n == 0) {
1184 /* short circuit when iterable is empty */
1185 return 0;
1186 }
1187
1188 Py_ssize_t m = Py_SIZE(self);
1189 // It should not be possible to allocate a list large enough to cause
1190 // an overflow on any relevant platform.
1191 assert(m < PY_SSIZE_T_MAX - n);
1192 if (self->ob_item == NULL) {
1193 if (list_preallocate_exact(self, n) < 0) {
1194 return -1;
1195 }
1196 Py_SET_SIZE(self, n);
1197 }
1198 else if (list_resize(self, m + n) < 0) {
1199 return -1;
1200 }
1201
1202 // note that we may still have self == iterable here for the
1203 // situation a.extend(a), but the following code works
1204 // in that case too. Just make sure to resize self
1205 // before calling PySequence_Fast_ITEMS.
1206 //
1207 // populate the end of self with iterable's items.
1208 PyObject **src = PySequence_Fast_ITEMS(iterable);
1209 PyObject **dest = self->ob_item + m;
1210 for (Py_ssize_t i = 0; i < n; i++) {
1211 PyObject *o = src[i];
1212 FT_ATOMIC_STORE_PTR_RELEASE(dest[i], Py_NewRef(o));
1213 }
1214 return 0;
1215 }
1216
1217 static int
list_extend_iter_lock_held(PyListObject * self,PyObject * iterable)1218 list_extend_iter_lock_held(PyListObject *self, PyObject *iterable)
1219 {
1220 PyObject *it = PyObject_GetIter(iterable);
1221 if (it == NULL) {
1222 return -1;
1223 }
1224 PyObject *(*iternext)(PyObject *) = *Py_TYPE(it)->tp_iternext;
1225
1226 /* Guess a result list size. */
1227 Py_ssize_t n = PyObject_LengthHint(iterable, 8);
1228 if (n < 0) {
1229 Py_DECREF(it);
1230 return -1;
1231 }
1232
1233 Py_ssize_t m = Py_SIZE(self);
1234 if (m > PY_SSIZE_T_MAX - n) {
1235 /* m + n overflowed; on the chance that n lied, and there really
1236 * is enough room, ignore it. If n was telling the truth, we'll
1237 * eventually run out of memory during the loop.
1238 */
1239 }
1240 else if (self->ob_item == NULL) {
1241 if (n && list_preallocate_exact(self, n) < 0)
1242 goto error;
1243 }
1244 else {
1245 /* Make room. */
1246 if (list_resize(self, m + n) < 0) {
1247 goto error;
1248 }
1249
1250 /* Make the list sane again. */
1251 Py_SET_SIZE(self, m);
1252 }
1253
1254 /* Run iterator to exhaustion. */
1255 for (;;) {
1256 PyObject *item = iternext(it);
1257 if (item == NULL) {
1258 if (PyErr_Occurred()) {
1259 if (PyErr_ExceptionMatches(PyExc_StopIteration))
1260 PyErr_Clear();
1261 else
1262 goto error;
1263 }
1264 break;
1265 }
1266
1267 if (Py_SIZE(self) < self->allocated) {
1268 Py_ssize_t len = Py_SIZE(self);
1269 FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item[len], item); // steals item ref
1270 Py_SET_SIZE(self, len + 1);
1271 }
1272 else {
1273 if (_PyList_AppendTakeRef(self, item) < 0)
1274 goto error;
1275 }
1276 }
1277
1278 /* Cut back result list if initial guess was too large. */
1279 if (Py_SIZE(self) < self->allocated) {
1280 if (list_resize(self, Py_SIZE(self)) < 0)
1281 goto error;
1282 }
1283
1284 Py_DECREF(it);
1285 return 0;
1286
1287 error:
1288 Py_DECREF(it);
1289 return -1;
1290 }
1291
1292 static int
list_extend_lock_held(PyListObject * self,PyObject * iterable)1293 list_extend_lock_held(PyListObject *self, PyObject *iterable)
1294 {
1295 PyObject *seq = PySequence_Fast(iterable, "argument must be iterable");
1296 if (!seq) {
1297 return -1;
1298 }
1299
1300 int res = list_extend_fast(self, seq);
1301 Py_DECREF(seq);
1302 return res;
1303 }
1304
1305 static int
list_extend_set(PyListObject * self,PySetObject * other)1306 list_extend_set(PyListObject *self, PySetObject *other)
1307 {
1308 Py_ssize_t m = Py_SIZE(self);
1309 Py_ssize_t n = PySet_GET_SIZE(other);
1310 if (list_resize(self, m + n) < 0) {
1311 return -1;
1312 }
1313 /* populate the end of self with iterable's items */
1314 Py_ssize_t setpos = 0;
1315 Py_hash_t hash;
1316 PyObject *key;
1317 PyObject **dest = self->ob_item + m;
1318 while (_PySet_NextEntryRef((PyObject *)other, &setpos, &key, &hash)) {
1319 FT_ATOMIC_STORE_PTR_RELEASE(*dest, key);
1320 dest++;
1321 }
1322 Py_SET_SIZE(self, m + n);
1323 return 0;
1324 }
1325
1326 static int
list_extend_dict(PyListObject * self,PyDictObject * dict,int which_item)1327 list_extend_dict(PyListObject *self, PyDictObject *dict, int which_item)
1328 {
1329 // which_item: 0 for keys and 1 for values
1330 Py_ssize_t m = Py_SIZE(self);
1331 Py_ssize_t n = PyDict_GET_SIZE(dict);
1332 if (list_resize(self, m + n) < 0) {
1333 return -1;
1334 }
1335
1336 PyObject **dest = self->ob_item + m;
1337 Py_ssize_t pos = 0;
1338 PyObject *keyvalue[2];
1339 while (_PyDict_Next((PyObject *)dict, &pos, &keyvalue[0], &keyvalue[1], NULL)) {
1340 PyObject *obj = keyvalue[which_item];
1341 Py_INCREF(obj);
1342 FT_ATOMIC_STORE_PTR_RELEASE(*dest, obj);
1343 dest++;
1344 }
1345
1346 Py_SET_SIZE(self, m + n);
1347 return 0;
1348 }
1349
1350 static int
list_extend_dictitems(PyListObject * self,PyDictObject * dict)1351 list_extend_dictitems(PyListObject *self, PyDictObject *dict)
1352 {
1353 Py_ssize_t m = Py_SIZE(self);
1354 Py_ssize_t n = PyDict_GET_SIZE(dict);
1355 if (list_resize(self, m + n) < 0) {
1356 return -1;
1357 }
1358
1359 PyObject **dest = self->ob_item + m;
1360 Py_ssize_t pos = 0;
1361 Py_ssize_t i = 0;
1362 PyObject *key, *value;
1363 while (_PyDict_Next((PyObject *)dict, &pos, &key, &value, NULL)) {
1364 PyObject *item = PyTuple_Pack(2, key, value);
1365 if (item == NULL) {
1366 Py_SET_SIZE(self, m + i);
1367 return -1;
1368 }
1369 FT_ATOMIC_STORE_PTR_RELEASE(*dest, item);
1370 dest++;
1371 i++;
1372 }
1373
1374 Py_SET_SIZE(self, m + n);
1375 return 0;
1376 }
1377
1378 static int
_list_extend(PyListObject * self,PyObject * iterable)1379 _list_extend(PyListObject *self, PyObject *iterable)
1380 {
1381 // Special case:
1382 // lists and tuples which can use PySequence_Fast ops
1383 int res = -1;
1384 if ((PyObject *)self == iterable) {
1385 Py_BEGIN_CRITICAL_SECTION(self);
1386 res = list_inplace_repeat_lock_held(self, 2);
1387 Py_END_CRITICAL_SECTION();
1388 }
1389 else if (PyList_CheckExact(iterable)) {
1390 Py_BEGIN_CRITICAL_SECTION2(self, iterable);
1391 res = list_extend_lock_held(self, iterable);
1392 Py_END_CRITICAL_SECTION2();
1393 }
1394 else if (PyTuple_CheckExact(iterable)) {
1395 Py_BEGIN_CRITICAL_SECTION(self);
1396 res = list_extend_lock_held(self, iterable);
1397 Py_END_CRITICAL_SECTION();
1398 }
1399 else if (PyAnySet_CheckExact(iterable)) {
1400 Py_BEGIN_CRITICAL_SECTION2(self, iterable);
1401 res = list_extend_set(self, (PySetObject *)iterable);
1402 Py_END_CRITICAL_SECTION2();
1403 }
1404 else if (PyDict_CheckExact(iterable)) {
1405 Py_BEGIN_CRITICAL_SECTION2(self, iterable);
1406 res = list_extend_dict(self, (PyDictObject *)iterable, 0 /*keys*/);
1407 Py_END_CRITICAL_SECTION2();
1408 }
1409 else if (Py_IS_TYPE(iterable, &PyDictKeys_Type)) {
1410 PyDictObject *dict = ((_PyDictViewObject *)iterable)->dv_dict;
1411 Py_BEGIN_CRITICAL_SECTION2(self, dict);
1412 res = list_extend_dict(self, dict, 0 /*keys*/);
1413 Py_END_CRITICAL_SECTION2();
1414 }
1415 else if (Py_IS_TYPE(iterable, &PyDictValues_Type)) {
1416 PyDictObject *dict = ((_PyDictViewObject *)iterable)->dv_dict;
1417 Py_BEGIN_CRITICAL_SECTION2(self, dict);
1418 res = list_extend_dict(self, dict, 1 /*values*/);
1419 Py_END_CRITICAL_SECTION2();
1420 }
1421 else if (Py_IS_TYPE(iterable, &PyDictItems_Type)) {
1422 PyDictObject *dict = ((_PyDictViewObject *)iterable)->dv_dict;
1423 Py_BEGIN_CRITICAL_SECTION2(self, dict);
1424 res = list_extend_dictitems(self, dict);
1425 Py_END_CRITICAL_SECTION2();
1426 }
1427 else {
1428 Py_BEGIN_CRITICAL_SECTION(self);
1429 res = list_extend_iter_lock_held(self, iterable);
1430 Py_END_CRITICAL_SECTION();
1431 }
1432 return res;
1433 }
1434
1435 /*[clinic input]
1436 list.extend as list_extend
1437
1438 iterable: object
1439 /
1440
1441 Extend list by appending elements from the iterable.
1442 [clinic start generated code]*/
1443
1444 static PyObject *
list_extend(PyListObject * self,PyObject * iterable)1445 list_extend(PyListObject *self, PyObject *iterable)
1446 /*[clinic end generated code: output=630fb3bca0c8e789 input=979da7597a515791]*/
1447 {
1448 if (_list_extend(self, iterable) < 0) {
1449 return NULL;
1450 }
1451 Py_RETURN_NONE;
1452 }
1453
1454 PyObject *
_PyList_Extend(PyListObject * self,PyObject * iterable)1455 _PyList_Extend(PyListObject *self, PyObject *iterable)
1456 {
1457 return list_extend(self, iterable);
1458 }
1459
1460 int
PyList_Extend(PyObject * self,PyObject * iterable)1461 PyList_Extend(PyObject *self, PyObject *iterable)
1462 {
1463 if (!PyList_Check(self)) {
1464 PyErr_BadInternalCall();
1465 return -1;
1466 }
1467 return _list_extend((PyListObject*)self, iterable);
1468 }
1469
1470
1471 int
PyList_Clear(PyObject * self)1472 PyList_Clear(PyObject *self)
1473 {
1474 if (!PyList_Check(self)) {
1475 PyErr_BadInternalCall();
1476 return -1;
1477 }
1478 list_clear((PyListObject*)self);
1479 return 0;
1480 }
1481
1482
1483 static PyObject *
list_inplace_concat(PyObject * _self,PyObject * other)1484 list_inplace_concat(PyObject *_self, PyObject *other)
1485 {
1486 PyListObject *self = (PyListObject *)_self;
1487 if (_list_extend(self, other) < 0) {
1488 return NULL;
1489 }
1490 return Py_NewRef(self);
1491 }
1492
1493 /*[clinic input]
1494 @critical_section
1495 list.pop
1496
1497 index: Py_ssize_t = -1
1498 /
1499
1500 Remove and return item at index (default last).
1501
1502 Raises IndexError if list is empty or index is out of range.
1503 [clinic start generated code]*/
1504
1505 static PyObject *
list_pop_impl(PyListObject * self,Py_ssize_t index)1506 list_pop_impl(PyListObject *self, Py_ssize_t index)
1507 /*[clinic end generated code: output=6bd69dcb3f17eca8 input=c269141068ae4b8f]*/
1508 {
1509 PyObject *v;
1510 int status;
1511
1512 if (Py_SIZE(self) == 0) {
1513 /* Special-case most common failure cause */
1514 PyErr_SetString(PyExc_IndexError, "pop from empty list");
1515 return NULL;
1516 }
1517 if (index < 0)
1518 index += Py_SIZE(self);
1519 if (!valid_index(index, Py_SIZE(self))) {
1520 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1521 return NULL;
1522 }
1523
1524 PyObject **items = self->ob_item;
1525 v = items[index];
1526 const Py_ssize_t size_after_pop = Py_SIZE(self) - 1;
1527 if (size_after_pop == 0) {
1528 Py_INCREF(v);
1529 list_clear(self);
1530 status = 0;
1531 }
1532 else {
1533 if ((size_after_pop - index) > 0) {
1534 memmove(&items[index], &items[index+1], (size_after_pop - index) * sizeof(PyObject *));
1535 }
1536 status = list_resize(self, size_after_pop);
1537 }
1538 if (status >= 0) {
1539 return v; // and v now owns the reference the list had
1540 }
1541 else {
1542 // list resize failed, need to restore
1543 memmove(&items[index+1], &items[index], (size_after_pop - index)* sizeof(PyObject *));
1544 items[index] = v;
1545 return NULL;
1546 }
1547 }
1548
1549 /* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
1550 static void
reverse_slice(PyObject ** lo,PyObject ** hi)1551 reverse_slice(PyObject **lo, PyObject **hi)
1552 {
1553 assert(lo && hi);
1554
1555 --hi;
1556 while (lo < hi) {
1557 PyObject *t = *lo;
1558 *lo = *hi;
1559 *hi = t;
1560 ++lo;
1561 --hi;
1562 }
1563 }
1564
1565 /* Lots of code for an adaptive, stable, natural mergesort. There are many
1566 * pieces to this algorithm; read listsort.txt for overviews and details.
1567 */
1568
1569 /* A sortslice contains a pointer to an array of keys and a pointer to
1570 * an array of corresponding values. In other words, keys[i]
1571 * corresponds with values[i]. If values == NULL, then the keys are
1572 * also the values.
1573 *
1574 * Several convenience routines are provided here, so that keys and
1575 * values are always moved in sync.
1576 */
1577
1578 typedef struct {
1579 PyObject **keys;
1580 PyObject **values;
1581 } sortslice;
1582
1583 Py_LOCAL_INLINE(void)
sortslice_copy(sortslice * s1,Py_ssize_t i,sortslice * s2,Py_ssize_t j)1584 sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
1585 {
1586 s1->keys[i] = s2->keys[j];
1587 if (s1->values != NULL)
1588 s1->values[i] = s2->values[j];
1589 }
1590
1591 Py_LOCAL_INLINE(void)
sortslice_copy_incr(sortslice * dst,sortslice * src)1592 sortslice_copy_incr(sortslice *dst, sortslice *src)
1593 {
1594 *dst->keys++ = *src->keys++;
1595 if (dst->values != NULL)
1596 *dst->values++ = *src->values++;
1597 }
1598
1599 Py_LOCAL_INLINE(void)
sortslice_copy_decr(sortslice * dst,sortslice * src)1600 sortslice_copy_decr(sortslice *dst, sortslice *src)
1601 {
1602 *dst->keys-- = *src->keys--;
1603 if (dst->values != NULL)
1604 *dst->values-- = *src->values--;
1605 }
1606
1607
1608 Py_LOCAL_INLINE(void)
sortslice_memcpy(sortslice * s1,Py_ssize_t i,sortslice * s2,Py_ssize_t j,Py_ssize_t n)1609 sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
1610 Py_ssize_t n)
1611 {
1612 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1613 if (s1->values != NULL)
1614 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1615 }
1616
1617 Py_LOCAL_INLINE(void)
sortslice_memmove(sortslice * s1,Py_ssize_t i,sortslice * s2,Py_ssize_t j,Py_ssize_t n)1618 sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
1619 Py_ssize_t n)
1620 {
1621 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1622 if (s1->values != NULL)
1623 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1624 }
1625
1626 Py_LOCAL_INLINE(void)
sortslice_advance(sortslice * slice,Py_ssize_t n)1627 sortslice_advance(sortslice *slice, Py_ssize_t n)
1628 {
1629 slice->keys += n;
1630 if (slice->values != NULL)
1631 slice->values += n;
1632 }
1633
1634 /* Comparison function: ms->key_compare, which is set at run-time in
1635 * listsort_impl to optimize for various special cases.
1636 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1637 */
1638
1639 #define ISLT(X, Y) (*(ms->key_compare))(X, Y, ms)
1640
1641 /* Compare X to Y via "<". Goto "fail" if the comparison raises an
1642 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1643 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1644 */
1645 #define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
1646 if (k)
1647
1648 /* The maximum number of entries in a MergeState's pending-runs stack.
1649 * For a list with n elements, this needs at most floor(log2(n)) + 1 entries
1650 * even if we didn't force runs to a minimal length. So the number of bits
1651 * in a Py_ssize_t is plenty large enough for all cases.
1652 */
1653 #define MAX_MERGE_PENDING (SIZEOF_SIZE_T * 8)
1654
1655 /* When we get into galloping mode, we stay there until both runs win less
1656 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
1657 */
1658 #define MIN_GALLOP 7
1659
1660 /* Avoid malloc for small temp arrays. */
1661 #define MERGESTATE_TEMP_SIZE 256
1662
1663 /* The largest value of minrun. This must be a power of 2, and >= 1, so that
1664 * the compute_minrun() algorithm guarantees to return a result no larger than
1665 * this,
1666 */
1667 #define MAX_MINRUN 64
1668 #if ((MAX_MINRUN) < 1) || ((MAX_MINRUN) & ((MAX_MINRUN) - 1))
1669 #error "MAX_MINRUN must be a power of 2, and >= 1"
1670 #endif
1671
1672 /* One MergeState exists on the stack per invocation of mergesort. It's just
1673 * a convenient way to pass state around among the helper functions.
1674 */
1675 struct s_slice {
1676 sortslice base;
1677 Py_ssize_t len; /* length of run */
1678 int power; /* node "level" for powersort merge strategy */
1679 };
1680
1681 typedef struct s_MergeState MergeState;
1682 struct s_MergeState {
1683 /* This controls when we get *into* galloping mode. It's initialized
1684 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1685 * random data, and lower for highly structured data.
1686 */
1687 Py_ssize_t min_gallop;
1688
1689 Py_ssize_t listlen; /* len(input_list) - read only */
1690 PyObject **basekeys; /* base address of keys array - read only */
1691
1692 /* 'a' is temp storage to help with merges. It contains room for
1693 * alloced entries.
1694 */
1695 sortslice a; /* may point to temparray below */
1696 Py_ssize_t alloced;
1697
1698 /* A stack of n pending runs yet to be merged. Run #i starts at
1699 * address base[i] and extends for len[i] elements. It's always
1700 * true (so long as the indices are in bounds) that
1701 *
1702 * pending[i].base + pending[i].len == pending[i+1].base
1703 *
1704 * so we could cut the storage for this, but it's a minor amount,
1705 * and keeping all the info explicit simplifies the code.
1706 */
1707 int n;
1708 struct s_slice pending[MAX_MERGE_PENDING];
1709
1710 /* 'a' points to this when possible, rather than muck with malloc. */
1711 PyObject *temparray[MERGESTATE_TEMP_SIZE];
1712
1713 /* This is the function we will use to compare two keys,
1714 * even when none of our special cases apply and we have to use
1715 * safe_object_compare. */
1716 int (*key_compare)(PyObject *, PyObject *, MergeState *);
1717
1718 /* This function is used by unsafe_object_compare to optimize comparisons
1719 * when we know our list is type-homogeneous but we can't assume anything else.
1720 * In the pre-sort check it is set equal to Py_TYPE(key)->tp_richcompare */
1721 PyObject *(*key_richcompare)(PyObject *, PyObject *, int);
1722
1723 /* This function is used by unsafe_tuple_compare to compare the first elements
1724 * of tuples. It may be set to safe_object_compare, but the idea is that hopefully
1725 * we can assume more, and use one of the special-case compares. */
1726 int (*tuple_elem_compare)(PyObject *, PyObject *, MergeState *);
1727 };
1728
1729 /* binarysort is the best method for sorting small arrays: it does few
1730 compares, but can do data movement quadratic in the number of elements.
1731 ss->keys is viewed as an array of n kays, a[:n]. a[:ok] is already sorted.
1732 Pass ok = 0 (or 1) if you don't know.
1733 It's sorted in-place, by a stable binary insertion sort. If ss->values
1734 isn't NULL, it's permuted in lockstap with ss->keys.
1735 On entry, must have n >= 1, and 0 <= ok <= n <= MAX_MINRUN.
1736 Return -1 if comparison raises an exception, else 0.
1737 Even in case of error, the output slice will be some permutation of
1738 the input (nothing is lost or duplicated).
1739 */
1740 static int
binarysort(MergeState * ms,const sortslice * ss,Py_ssize_t n,Py_ssize_t ok)1741 binarysort(MergeState *ms, const sortslice *ss, Py_ssize_t n, Py_ssize_t ok)
1742 {
1743 Py_ssize_t k; /* for IFLT macro expansion */
1744 PyObject ** const a = ss->keys;
1745 PyObject ** const v = ss->values;
1746 const bool has_values = v != NULL;
1747 PyObject *pivot;
1748 Py_ssize_t M;
1749
1750 assert(0 <= ok && ok <= n && 1 <= n && n <= MAX_MINRUN);
1751 /* assert a[:ok] is sorted */
1752 if (! ok)
1753 ++ok;
1754 /* Regular insertion sort has average- and worst-case O(n**2) cost
1755 for both # of comparisons and number of bytes moved. But its branches
1756 are highly predictable, and it loves sorted input (n-1 compares and no
1757 data movement). This is significant in cases like sortperf.py's %sort,
1758 where an out-of-order element near the start of a run is moved into
1759 place slowly but then the remaining elements up to length minrun are
1760 generally at worst one slot away from their correct position (so only
1761 need 1 or 2 commpares to resolve). If comparisons are very fast (such
1762 as for a list of Python floats), the simple inner loop leaves it
1763 very competitive with binary insertion, despite that it does
1764 significantly more compares overall on random data.
1765
1766 Binary insertion sort has worst, average, and best case O(n log n)
1767 cost for # of comparisons, but worst and average case O(n**2) cost
1768 for data movement. The more expensive comparisons, the more important
1769 the comparison advantage. But its branches are less predictable the
1770 more "randomish" the data, and that's so significant its worst case
1771 in real life is random input rather than reverse-ordered (which does
1772 about twice the data movement than random input does).
1773
1774 Note that the number of bytes moved doesn't seem to matter. MAX_MINRUN
1775 of 64 is so small that the key and value pointers all fit in a corner
1776 of L1 cache, and moving things around in that is very fast. */
1777 #if 0 // ordinary insertion sort.
1778 PyObject * vpivot = NULL;
1779 for (; ok < n; ++ok) {
1780 pivot = a[ok];
1781 if (has_values)
1782 vpivot = v[ok];
1783 for (M = ok - 1; M >= 0; --M) {
1784 k = ISLT(pivot, a[M]);
1785 if (k < 0) {
1786 a[M + 1] = pivot;
1787 if (has_values)
1788 v[M + 1] = vpivot;
1789 goto fail;
1790 }
1791 else if (k) {
1792 a[M + 1] = a[M];
1793 if (has_values)
1794 v[M + 1] = v[M];
1795 }
1796 else
1797 break;
1798 }
1799 a[M + 1] = pivot;
1800 if (has_values)
1801 v[M + 1] = vpivot;
1802 }
1803 #else // binary insertion sort
1804 Py_ssize_t L, R;
1805 for (; ok < n; ++ok) {
1806 /* set L to where a[ok] belongs */
1807 L = 0;
1808 R = ok;
1809 pivot = a[ok];
1810 /* Slice invariants. vacuously true at the start:
1811 * all a[0:L] <= pivot
1812 * all a[L:R] unknown
1813 * all a[R:ok] > pivot
1814 */
1815 assert(L < R);
1816 do {
1817 /* don't do silly ;-) things to prevent overflow when finding
1818 the midpoint; L and R are very far from filling a Py_ssize_t */
1819 M = (L + R) >> 1;
1820 #if 1 // straightforward, but highly unpredictable branch on random data
1821 IFLT(pivot, a[M])
1822 R = M;
1823 else
1824 L = M + 1;
1825 #else
1826 /* Try to get compiler to generate conditional move instructions
1827 instead. Works fine, but leaving it disabled for now because
1828 it's not yielding consistently faster sorts. Needs more
1829 investigation. More computation in the inner loop adds its own
1830 costs, which can be significant when compares are fast. */
1831 k = ISLT(pivot, a[M]);
1832 if (k < 0)
1833 goto fail;
1834 Py_ssize_t Mp1 = M + 1;
1835 R = k ? M : R;
1836 L = k ? L : Mp1;
1837 #endif
1838 } while (L < R);
1839 assert(L == R);
1840 /* a[:L] holds all elements from a[:ok] <= pivot now, so pivot belongs
1841 at index L. Slide a[L:ok] to the right a slot to make room for it.
1842 Caution: using memmove is much slower under MSVC 5; we're not
1843 usually moving many slots. Years later: under Visual Studio 2022,
1844 memmove seems just slightly slower than doing it "by hand". */
1845 for (M = ok; M > L; --M)
1846 a[M] = a[M - 1];
1847 a[L] = pivot;
1848 if (has_values) {
1849 pivot = v[ok];
1850 for (M = ok; M > L; --M)
1851 v[M] = v[M - 1];
1852 v[L] = pivot;
1853 }
1854 }
1855 #endif // pick binary or regular insertion sort
1856 return 0;
1857
1858 fail:
1859 return -1;
1860 }
1861
1862 static void
sortslice_reverse(sortslice * s,Py_ssize_t n)1863 sortslice_reverse(sortslice *s, Py_ssize_t n)
1864 {
1865 reverse_slice(s->keys, &s->keys[n]);
1866 if (s->values != NULL)
1867 reverse_slice(s->values, &s->values[n]);
1868 }
1869
1870 /*
1871 Return the length of the run beginning at slo->keys, spanning no more than
1872 nremaining elements. The run beginning there may be ascending or descending,
1873 but the function permutes it in place, if needed, so that it's always ascending
1874 upon return.
1875
1876 Returns -1 in case of error.
1877 */
1878 static Py_ssize_t
count_run(MergeState * ms,sortslice * slo,Py_ssize_t nremaining)1879 count_run(MergeState *ms, sortslice *slo, Py_ssize_t nremaining)
1880 {
1881 Py_ssize_t k; /* used by IFLT macro expansion */
1882 Py_ssize_t n;
1883 PyObject ** const lo = slo->keys;
1884
1885 /* In general, as things go on we've established that the slice starts
1886 with a monotone run of n elements, starting at lo. */
1887
1888 /* We're n elements into the slice, and the most recent neq+1 elments are
1889 * all equal. This reverses them in-place, and resets neq for reuse.
1890 */
1891 #define REVERSE_LAST_NEQ \
1892 if (neq) { \
1893 sortslice slice = *slo; \
1894 ++neq; \
1895 sortslice_advance(&slice, n - neq); \
1896 sortslice_reverse(&slice, neq); \
1897 neq = 0; \
1898 }
1899
1900 /* Sticking to only __lt__ compares is confusing and error-prone. But in
1901 * this routine, almost all uses of IFLT can be captured by tiny macros
1902 * giving mnemonic names to the intent. Note that inline functions don't
1903 * work for this (IFLT expands to code including `goto fail`).
1904 */
1905 #define IF_NEXT_LARGER IFLT(lo[n-1], lo[n])
1906 #define IF_NEXT_SMALLER IFLT(lo[n], lo[n-1])
1907
1908 assert(nremaining);
1909 /* try ascending run first */
1910 for (n = 1; n < nremaining; ++n) {
1911 IF_NEXT_SMALLER
1912 break;
1913 }
1914 if (n == nremaining)
1915 return n;
1916 /* lo[n] is strictly less */
1917 /* If n is 1 now, then the first compare established it's a descending
1918 * run, so fall through to the descending case. But if n > 1, there are
1919 * n elements in an ascending run terminated by the strictly less lo[n].
1920 * If the first key < lo[n-1], *somewhere* along the way the sequence
1921 * increased, so we're done (there is no descending run).
1922 * Else first key >= lo[n-1], which implies that the entire ascending run
1923 * consists of equal elements. In that case, this is a descending run,
1924 * and we reverse the all-equal prefix in-place.
1925 */
1926 if (n > 1) {
1927 IFLT(lo[0], lo[n-1])
1928 return n;
1929 sortslice_reverse(slo, n);
1930 }
1931 ++n; /* in all cases it's been established that lo[n] has been resolved */
1932
1933 /* Finish descending run. All-squal subruns are reversed in-place on the
1934 * fly. Their original order will be restored at the end by the whole-slice
1935 * reversal.
1936 */
1937 Py_ssize_t neq = 0;
1938 for ( ; n < nremaining; ++n) {
1939 IF_NEXT_SMALLER {
1940 /* This ends the most recent run of equal elments, but still in
1941 * the "descending" direction.
1942 */
1943 REVERSE_LAST_NEQ
1944 }
1945 else {
1946 IF_NEXT_LARGER /* descending run is over */
1947 break;
1948 else /* not x < y and not y < x implies x == y */
1949 ++neq;
1950 }
1951 }
1952 REVERSE_LAST_NEQ
1953 sortslice_reverse(slo, n); /* transform to ascending run */
1954
1955 /* And after reversing, it's possible this can be extended by a
1956 * naturally increasing suffix; e.g., [3, 2, 3, 4, 1] makes an
1957 * ascending run from the first 4 elements.
1958 */
1959 for ( ; n < nremaining; ++n) {
1960 IF_NEXT_SMALLER
1961 break;
1962 }
1963
1964 return n;
1965 fail:
1966 return -1;
1967
1968 #undef REVERSE_LAST_NEQ
1969 #undef IF_NEXT_SMALLER
1970 #undef IF_NEXT_LARGER
1971 }
1972
1973 /*
1974 Locate the proper position of key in a sorted vector; if the vector contains
1975 an element equal to key, return the position immediately to the left of
1976 the leftmost equal element. [gallop_right() does the same except returns
1977 the position to the right of the rightmost equal element (if any).]
1978
1979 "a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
1980
1981 "hint" is an index at which to begin the search, 0 <= hint < n. The closer
1982 hint is to the final result, the faster this runs.
1983
1984 The return value is the int k in 0..n such that
1985
1986 a[k-1] < key <= a[k]
1987
1988 pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1989 key belongs at index k; or, IOW, the first k elements of a should precede
1990 key, and the last n-k should follow key.
1991
1992 Returns -1 on error. See listsort.txt for info on the method.
1993 */
1994 static Py_ssize_t
gallop_left(MergeState * ms,PyObject * key,PyObject ** a,Py_ssize_t n,Py_ssize_t hint)1995 gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
1996 {
1997 Py_ssize_t ofs;
1998 Py_ssize_t lastofs;
1999 Py_ssize_t k;
2000
2001 assert(key && a && n > 0 && hint >= 0 && hint < n);
2002
2003 a += hint;
2004 lastofs = 0;
2005 ofs = 1;
2006 IFLT(*a, key) {
2007 /* a[hint] < key -- gallop right, until
2008 * a[hint + lastofs] < key <= a[hint + ofs]
2009 */
2010 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
2011 while (ofs < maxofs) {
2012 IFLT(a[ofs], key) {
2013 lastofs = ofs;
2014 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
2015 ofs = (ofs << 1) + 1;
2016 }
2017 else /* key <= a[hint + ofs] */
2018 break;
2019 }
2020 if (ofs > maxofs)
2021 ofs = maxofs;
2022 /* Translate back to offsets relative to &a[0]. */
2023 lastofs += hint;
2024 ofs += hint;
2025 }
2026 else {
2027 /* key <= a[hint] -- gallop left, until
2028 * a[hint - ofs] < key <= a[hint - lastofs]
2029 */
2030 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
2031 while (ofs < maxofs) {
2032 IFLT(*(a-ofs), key)
2033 break;
2034 /* key <= a[hint - ofs] */
2035 lastofs = ofs;
2036 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
2037 ofs = (ofs << 1) + 1;
2038 }
2039 if (ofs > maxofs)
2040 ofs = maxofs;
2041 /* Translate back to positive offsets relative to &a[0]. */
2042 k = lastofs;
2043 lastofs = hint - ofs;
2044 ofs = hint - k;
2045 }
2046 a -= hint;
2047
2048 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
2049 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
2050 * right of lastofs but no farther right than ofs. Do a binary
2051 * search, with invariant a[lastofs-1] < key <= a[ofs].
2052 */
2053 ++lastofs;
2054 while (lastofs < ofs) {
2055 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
2056
2057 IFLT(a[m], key)
2058 lastofs = m+1; /* a[m] < key */
2059 else
2060 ofs = m; /* key <= a[m] */
2061 }
2062 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
2063 return ofs;
2064
2065 fail:
2066 return -1;
2067 }
2068
2069 /*
2070 Exactly like gallop_left(), except that if key already exists in a[0:n],
2071 finds the position immediately to the right of the rightmost equal value.
2072
2073 The return value is the int k in 0..n such that
2074
2075 a[k-1] <= key < a[k]
2076
2077 or -1 if error.
2078
2079 The code duplication is massive, but this is enough different given that
2080 we're sticking to "<" comparisons that it's much harder to follow if
2081 written as one routine with yet another "left or right?" flag.
2082 */
2083 static Py_ssize_t
gallop_right(MergeState * ms,PyObject * key,PyObject ** a,Py_ssize_t n,Py_ssize_t hint)2084 gallop_right(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
2085 {
2086 Py_ssize_t ofs;
2087 Py_ssize_t lastofs;
2088 Py_ssize_t k;
2089
2090 assert(key && a && n > 0 && hint >= 0 && hint < n);
2091
2092 a += hint;
2093 lastofs = 0;
2094 ofs = 1;
2095 IFLT(key, *a) {
2096 /* key < a[hint] -- gallop left, until
2097 * a[hint - ofs] <= key < a[hint - lastofs]
2098 */
2099 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
2100 while (ofs < maxofs) {
2101 IFLT(key, *(a-ofs)) {
2102 lastofs = ofs;
2103 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
2104 ofs = (ofs << 1) + 1;
2105 }
2106 else /* a[hint - ofs] <= key */
2107 break;
2108 }
2109 if (ofs > maxofs)
2110 ofs = maxofs;
2111 /* Translate back to positive offsets relative to &a[0]. */
2112 k = lastofs;
2113 lastofs = hint - ofs;
2114 ofs = hint - k;
2115 }
2116 else {
2117 /* a[hint] <= key -- gallop right, until
2118 * a[hint + lastofs] <= key < a[hint + ofs]
2119 */
2120 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
2121 while (ofs < maxofs) {
2122 IFLT(key, a[ofs])
2123 break;
2124 /* a[hint + ofs] <= key */
2125 lastofs = ofs;
2126 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
2127 ofs = (ofs << 1) + 1;
2128 }
2129 if (ofs > maxofs)
2130 ofs = maxofs;
2131 /* Translate back to offsets relative to &a[0]. */
2132 lastofs += hint;
2133 ofs += hint;
2134 }
2135 a -= hint;
2136
2137 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
2138 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
2139 * right of lastofs but no farther right than ofs. Do a binary
2140 * search, with invariant a[lastofs-1] <= key < a[ofs].
2141 */
2142 ++lastofs;
2143 while (lastofs < ofs) {
2144 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
2145
2146 IFLT(key, a[m])
2147 ofs = m; /* key < a[m] */
2148 else
2149 lastofs = m+1; /* a[m] <= key */
2150 }
2151 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
2152 return ofs;
2153
2154 fail:
2155 return -1;
2156 }
2157
2158 /* Conceptually a MergeState's constructor. */
2159 static void
merge_init(MergeState * ms,Py_ssize_t list_size,int has_keyfunc,sortslice * lo)2160 merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc,
2161 sortslice *lo)
2162 {
2163 assert(ms != NULL);
2164 if (has_keyfunc) {
2165 /* The temporary space for merging will need at most half the list
2166 * size rounded up. Use the minimum possible space so we can use the
2167 * rest of temparray for other things. In particular, if there is
2168 * enough extra space, listsort() will use it to store the keys.
2169 */
2170 ms->alloced = (list_size + 1) / 2;
2171
2172 /* ms->alloced describes how many keys will be stored at
2173 ms->temparray, but we also need to store the values. Hence,
2174 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
2175 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
2176 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
2177 ms->a.values = &ms->temparray[ms->alloced];
2178 }
2179 else {
2180 ms->alloced = MERGESTATE_TEMP_SIZE;
2181 ms->a.values = NULL;
2182 }
2183 ms->a.keys = ms->temparray;
2184 ms->n = 0;
2185 ms->min_gallop = MIN_GALLOP;
2186 ms->listlen = list_size;
2187 ms->basekeys = lo->keys;
2188 }
2189
2190 /* Free all the temp memory owned by the MergeState. This must be called
2191 * when you're done with a MergeState, and may be called before then if
2192 * you want to free the temp memory early.
2193 */
2194 static void
merge_freemem(MergeState * ms)2195 merge_freemem(MergeState *ms)
2196 {
2197 assert(ms != NULL);
2198 if (ms->a.keys != ms->temparray) {
2199 PyMem_Free(ms->a.keys);
2200 ms->a.keys = NULL;
2201 }
2202 }
2203
2204 /* Ensure enough temp memory for 'need' array slots is available.
2205 * Returns 0 on success and -1 if the memory can't be gotten.
2206 */
2207 static int
merge_getmem(MergeState * ms,Py_ssize_t need)2208 merge_getmem(MergeState *ms, Py_ssize_t need)
2209 {
2210 int multiplier;
2211
2212 assert(ms != NULL);
2213 if (need <= ms->alloced)
2214 return 0;
2215
2216 multiplier = ms->a.values != NULL ? 2 : 1;
2217
2218 /* Don't realloc! That can cost cycles to copy the old data, but
2219 * we don't care what's in the block.
2220 */
2221 merge_freemem(ms);
2222 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject *) / multiplier) {
2223 PyErr_NoMemory();
2224 return -1;
2225 }
2226 ms->a.keys = (PyObject **)PyMem_Malloc(multiplier * need
2227 * sizeof(PyObject *));
2228 if (ms->a.keys != NULL) {
2229 ms->alloced = need;
2230 if (ms->a.values != NULL)
2231 ms->a.values = &ms->a.keys[need];
2232 return 0;
2233 }
2234 PyErr_NoMemory();
2235 return -1;
2236 }
2237 #define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
2238 merge_getmem(MS, NEED))
2239
2240 /* Merge the na elements starting at ssa with the nb elements starting at
2241 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
2242 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
2243 * should have na <= nb. See listsort.txt for more info. Return 0 if
2244 * successful, -1 if error.
2245 */
2246 static Py_ssize_t
merge_lo(MergeState * ms,sortslice ssa,Py_ssize_t na,sortslice ssb,Py_ssize_t nb)2247 merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
2248 sortslice ssb, Py_ssize_t nb)
2249 {
2250 Py_ssize_t k;
2251 sortslice dest;
2252 int result = -1; /* guilty until proved innocent */
2253 Py_ssize_t min_gallop;
2254
2255 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
2256 assert(ssa.keys + na == ssb.keys);
2257 if (MERGE_GETMEM(ms, na) < 0)
2258 return -1;
2259 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
2260 dest = ssa;
2261 ssa = ms->a;
2262
2263 sortslice_copy_incr(&dest, &ssb);
2264 --nb;
2265 if (nb == 0)
2266 goto Succeed;
2267 if (na == 1)
2268 goto CopyB;
2269
2270 min_gallop = ms->min_gallop;
2271 for (;;) {
2272 Py_ssize_t acount = 0; /* # of times A won in a row */
2273 Py_ssize_t bcount = 0; /* # of times B won in a row */
2274
2275 /* Do the straightforward thing until (if ever) one run
2276 * appears to win consistently.
2277 */
2278 for (;;) {
2279 assert(na > 1 && nb > 0);
2280 k = ISLT(ssb.keys[0], ssa.keys[0]);
2281 if (k) {
2282 if (k < 0)
2283 goto Fail;
2284 sortslice_copy_incr(&dest, &ssb);
2285 ++bcount;
2286 acount = 0;
2287 --nb;
2288 if (nb == 0)
2289 goto Succeed;
2290 if (bcount >= min_gallop)
2291 break;
2292 }
2293 else {
2294 sortslice_copy_incr(&dest, &ssa);
2295 ++acount;
2296 bcount = 0;
2297 --na;
2298 if (na == 1)
2299 goto CopyB;
2300 if (acount >= min_gallop)
2301 break;
2302 }
2303 }
2304
2305 /* One run is winning so consistently that galloping may
2306 * be a huge win. So try that, and continue galloping until
2307 * (if ever) neither run appears to be winning consistently
2308 * anymore.
2309 */
2310 ++min_gallop;
2311 do {
2312 assert(na > 1 && nb > 0);
2313 min_gallop -= min_gallop > 1;
2314 ms->min_gallop = min_gallop;
2315 k = gallop_right(ms, ssb.keys[0], ssa.keys, na, 0);
2316 acount = k;
2317 if (k) {
2318 if (k < 0)
2319 goto Fail;
2320 sortslice_memcpy(&dest, 0, &ssa, 0, k);
2321 sortslice_advance(&dest, k);
2322 sortslice_advance(&ssa, k);
2323 na -= k;
2324 if (na == 1)
2325 goto CopyB;
2326 /* na==0 is impossible now if the comparison
2327 * function is consistent, but we can't assume
2328 * that it is.
2329 */
2330 if (na == 0)
2331 goto Succeed;
2332 }
2333 sortslice_copy_incr(&dest, &ssb);
2334 --nb;
2335 if (nb == 0)
2336 goto Succeed;
2337
2338 k = gallop_left(ms, ssa.keys[0], ssb.keys, nb, 0);
2339 bcount = k;
2340 if (k) {
2341 if (k < 0)
2342 goto Fail;
2343 sortslice_memmove(&dest, 0, &ssb, 0, k);
2344 sortslice_advance(&dest, k);
2345 sortslice_advance(&ssb, k);
2346 nb -= k;
2347 if (nb == 0)
2348 goto Succeed;
2349 }
2350 sortslice_copy_incr(&dest, &ssa);
2351 --na;
2352 if (na == 1)
2353 goto CopyB;
2354 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
2355 ++min_gallop; /* penalize it for leaving galloping mode */
2356 ms->min_gallop = min_gallop;
2357 }
2358 Succeed:
2359 result = 0;
2360 Fail:
2361 if (na)
2362 sortslice_memcpy(&dest, 0, &ssa, 0, na);
2363 return result;
2364 CopyB:
2365 assert(na == 1 && nb > 0);
2366 /* The last element of ssa belongs at the end of the merge. */
2367 sortslice_memmove(&dest, 0, &ssb, 0, nb);
2368 sortslice_copy(&dest, nb, &ssa, 0);
2369 return 0;
2370 }
2371
2372 /* Merge the na elements starting at pa with the nb elements starting at
2373 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
2374 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
2375 * should have na >= nb. See listsort.txt for more info. Return 0 if
2376 * successful, -1 if error.
2377 */
2378 static Py_ssize_t
merge_hi(MergeState * ms,sortslice ssa,Py_ssize_t na,sortslice ssb,Py_ssize_t nb)2379 merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
2380 sortslice ssb, Py_ssize_t nb)
2381 {
2382 Py_ssize_t k;
2383 sortslice dest, basea, baseb;
2384 int result = -1; /* guilty until proved innocent */
2385 Py_ssize_t min_gallop;
2386
2387 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
2388 assert(ssa.keys + na == ssb.keys);
2389 if (MERGE_GETMEM(ms, nb) < 0)
2390 return -1;
2391 dest = ssb;
2392 sortslice_advance(&dest, nb-1);
2393 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
2394 basea = ssa;
2395 baseb = ms->a;
2396 ssb.keys = ms->a.keys + nb - 1;
2397 if (ssb.values != NULL)
2398 ssb.values = ms->a.values + nb - 1;
2399 sortslice_advance(&ssa, na - 1);
2400
2401 sortslice_copy_decr(&dest, &ssa);
2402 --na;
2403 if (na == 0)
2404 goto Succeed;
2405 if (nb == 1)
2406 goto CopyA;
2407
2408 min_gallop = ms->min_gallop;
2409 for (;;) {
2410 Py_ssize_t acount = 0; /* # of times A won in a row */
2411 Py_ssize_t bcount = 0; /* # of times B won in a row */
2412
2413 /* Do the straightforward thing until (if ever) one run
2414 * appears to win consistently.
2415 */
2416 for (;;) {
2417 assert(na > 0 && nb > 1);
2418 k = ISLT(ssb.keys[0], ssa.keys[0]);
2419 if (k) {
2420 if (k < 0)
2421 goto Fail;
2422 sortslice_copy_decr(&dest, &ssa);
2423 ++acount;
2424 bcount = 0;
2425 --na;
2426 if (na == 0)
2427 goto Succeed;
2428 if (acount >= min_gallop)
2429 break;
2430 }
2431 else {
2432 sortslice_copy_decr(&dest, &ssb);
2433 ++bcount;
2434 acount = 0;
2435 --nb;
2436 if (nb == 1)
2437 goto CopyA;
2438 if (bcount >= min_gallop)
2439 break;
2440 }
2441 }
2442
2443 /* One run is winning so consistently that galloping may
2444 * be a huge win. So try that, and continue galloping until
2445 * (if ever) neither run appears to be winning consistently
2446 * anymore.
2447 */
2448 ++min_gallop;
2449 do {
2450 assert(na > 0 && nb > 1);
2451 min_gallop -= min_gallop > 1;
2452 ms->min_gallop = min_gallop;
2453 k = gallop_right(ms, ssb.keys[0], basea.keys, na, na-1);
2454 if (k < 0)
2455 goto Fail;
2456 k = na - k;
2457 acount = k;
2458 if (k) {
2459 sortslice_advance(&dest, -k);
2460 sortslice_advance(&ssa, -k);
2461 sortslice_memmove(&dest, 1, &ssa, 1, k);
2462 na -= k;
2463 if (na == 0)
2464 goto Succeed;
2465 }
2466 sortslice_copy_decr(&dest, &ssb);
2467 --nb;
2468 if (nb == 1)
2469 goto CopyA;
2470
2471 k = gallop_left(ms, ssa.keys[0], baseb.keys, nb, nb-1);
2472 if (k < 0)
2473 goto Fail;
2474 k = nb - k;
2475 bcount = k;
2476 if (k) {
2477 sortslice_advance(&dest, -k);
2478 sortslice_advance(&ssb, -k);
2479 sortslice_memcpy(&dest, 1, &ssb, 1, k);
2480 nb -= k;
2481 if (nb == 1)
2482 goto CopyA;
2483 /* nb==0 is impossible now if the comparison
2484 * function is consistent, but we can't assume
2485 * that it is.
2486 */
2487 if (nb == 0)
2488 goto Succeed;
2489 }
2490 sortslice_copy_decr(&dest, &ssa);
2491 --na;
2492 if (na == 0)
2493 goto Succeed;
2494 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
2495 ++min_gallop; /* penalize it for leaving galloping mode */
2496 ms->min_gallop = min_gallop;
2497 }
2498 Succeed:
2499 result = 0;
2500 Fail:
2501 if (nb)
2502 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
2503 return result;
2504 CopyA:
2505 assert(nb == 1 && na > 0);
2506 /* The first element of ssb belongs at the front of the merge. */
2507 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
2508 sortslice_advance(&dest, -na);
2509 sortslice_advance(&ssa, -na);
2510 sortslice_copy(&dest, 0, &ssb, 0);
2511 return 0;
2512 }
2513
2514 /* Merge the two runs at stack indices i and i+1.
2515 * Returns 0 on success, -1 on error.
2516 */
2517 static Py_ssize_t
merge_at(MergeState * ms,Py_ssize_t i)2518 merge_at(MergeState *ms, Py_ssize_t i)
2519 {
2520 sortslice ssa, ssb;
2521 Py_ssize_t na, nb;
2522 Py_ssize_t k;
2523
2524 assert(ms != NULL);
2525 assert(ms->n >= 2);
2526 assert(i >= 0);
2527 assert(i == ms->n - 2 || i == ms->n - 3);
2528
2529 ssa = ms->pending[i].base;
2530 na = ms->pending[i].len;
2531 ssb = ms->pending[i+1].base;
2532 nb = ms->pending[i+1].len;
2533 assert(na > 0 && nb > 0);
2534 assert(ssa.keys + na == ssb.keys);
2535
2536 /* Record the length of the combined runs; if i is the 3rd-last
2537 * run now, also slide over the last run (which isn't involved
2538 * in this merge). The current run i+1 goes away in any case.
2539 */
2540 ms->pending[i].len = na + nb;
2541 if (i == ms->n - 3)
2542 ms->pending[i+1] = ms->pending[i+2];
2543 --ms->n;
2544
2545 /* Where does b start in a? Elements in a before that can be
2546 * ignored (already in place).
2547 */
2548 k = gallop_right(ms, *ssb.keys, ssa.keys, na, 0);
2549 if (k < 0)
2550 return -1;
2551 sortslice_advance(&ssa, k);
2552 na -= k;
2553 if (na == 0)
2554 return 0;
2555
2556 /* Where does a end in b? Elements in b after that can be
2557 * ignored (already in place).
2558 */
2559 nb = gallop_left(ms, ssa.keys[na-1], ssb.keys, nb, nb-1);
2560 if (nb <= 0)
2561 return nb;
2562
2563 /* Merge what remains of the runs, using a temp array with
2564 * min(na, nb) elements.
2565 */
2566 if (na <= nb)
2567 return merge_lo(ms, ssa, na, ssb, nb);
2568 else
2569 return merge_hi(ms, ssa, na, ssb, nb);
2570 }
2571
2572 /* Two adjacent runs begin at index s1. The first run has length n1, and
2573 * the second run (starting at index s1+n1) has length n2. The list has total
2574 * length n.
2575 * Compute the "power" of the first run. See listsort.txt for details.
2576 */
2577 static int
powerloop(Py_ssize_t s1,Py_ssize_t n1,Py_ssize_t n2,Py_ssize_t n)2578 powerloop(Py_ssize_t s1, Py_ssize_t n1, Py_ssize_t n2, Py_ssize_t n)
2579 {
2580 int result = 0;
2581 assert(s1 >= 0);
2582 assert(n1 > 0 && n2 > 0);
2583 assert(s1 + n1 + n2 <= n);
2584 /* midpoints a and b:
2585 * a = s1 + n1/2
2586 * b = s1 + n1 + n2/2 = a + (n1 + n2)/2
2587 *
2588 * Those may not be integers, though, because of the "/2". So we work with
2589 * 2*a and 2*b instead, which are necessarily integers. It makes no
2590 * difference to the outcome, since the bits in the expansion of (2*i)/n
2591 * are merely shifted one position from those of i/n.
2592 */
2593 Py_ssize_t a = 2 * s1 + n1; /* 2*a */
2594 Py_ssize_t b = a + n1 + n2; /* 2*b */
2595 /* Emulate a/n and b/n one bit a time, until bits differ. */
2596 for (;;) {
2597 ++result;
2598 if (a >= n) { /* both quotient bits are 1 */
2599 assert(b >= a);
2600 a -= n;
2601 b -= n;
2602 }
2603 else if (b >= n) { /* a/n bit is 0, b/n bit is 1 */
2604 break;
2605 } /* else both quotient bits are 0 */
2606 assert(a < b && b < n);
2607 a <<= 1;
2608 b <<= 1;
2609 }
2610 return result;
2611 }
2612
2613 /* The next run has been identified, of length n2.
2614 * If there's already a run on the stack, apply the "powersort" merge strategy:
2615 * compute the topmost run's "power" (depth in a conceptual binary merge tree)
2616 * and merge adjacent runs on the stack with greater power. See listsort.txt
2617 * for more info.
2618 *
2619 * It's the caller's responsibility to push the new run on the stack when this
2620 * returns.
2621 *
2622 * Returns 0 on success, -1 on error.
2623 */
2624 static int
found_new_run(MergeState * ms,Py_ssize_t n2)2625 found_new_run(MergeState *ms, Py_ssize_t n2)
2626 {
2627 assert(ms);
2628 if (ms->n) {
2629 assert(ms->n > 0);
2630 struct s_slice *p = ms->pending;
2631 Py_ssize_t s1 = p[ms->n - 1].base.keys - ms->basekeys; /* start index */
2632 Py_ssize_t n1 = p[ms->n - 1].len;
2633 int power = powerloop(s1, n1, n2, ms->listlen);
2634 while (ms->n > 1 && p[ms->n - 2].power > power) {
2635 if (merge_at(ms, ms->n - 2) < 0)
2636 return -1;
2637 }
2638 assert(ms->n < 2 || p[ms->n - 2].power < power);
2639 p[ms->n - 1].power = power;
2640 }
2641 return 0;
2642 }
2643
2644 /* Regardless of invariants, merge all runs on the stack until only one
2645 * remains. This is used at the end of the mergesort.
2646 *
2647 * Returns 0 on success, -1 on error.
2648 */
2649 static int
merge_force_collapse(MergeState * ms)2650 merge_force_collapse(MergeState *ms)
2651 {
2652 struct s_slice *p = ms->pending;
2653
2654 assert(ms);
2655 while (ms->n > 1) {
2656 Py_ssize_t n = ms->n - 2;
2657 if (n > 0 && p[n-1].len < p[n+1].len)
2658 --n;
2659 if (merge_at(ms, n) < 0)
2660 return -1;
2661 }
2662 return 0;
2663 }
2664
2665 /* Compute a good value for the minimum run length; natural runs shorter
2666 * than this are boosted artificially via binary insertion.
2667 *
2668 * If n < MAX_MINRUN return n (it's too small to bother with fancy stuff).
2669 * Else if n is an exact power of 2, return MAX_MINRUN / 2.
2670 * Else return an int k, MAX_MINRUN / 2 <= k <= MAX_MINRUN, such that n/k is
2671 * close to, but strictly less than, an exact power of 2.
2672 *
2673 * See listsort.txt for more info.
2674 */
2675 static Py_ssize_t
merge_compute_minrun(Py_ssize_t n)2676 merge_compute_minrun(Py_ssize_t n)
2677 {
2678 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
2679
2680 assert(n >= 0);
2681 while (n >= MAX_MINRUN) {
2682 r |= n & 1;
2683 n >>= 1;
2684 }
2685 return n + r;
2686 }
2687
2688 /* Here we define custom comparison functions to optimize for the cases one commonly
2689 * encounters in practice: homogeneous lists, often of one of the basic types. */
2690
2691 /* This struct holds the comparison function and helper functions
2692 * selected in the pre-sort check. */
2693
2694 /* These are the special case compare functions.
2695 * ms->key_compare will always point to one of these: */
2696
2697 /* Heterogeneous compare: default, always safe to fall back on. */
2698 static int
safe_object_compare(PyObject * v,PyObject * w,MergeState * ms)2699 safe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2700 {
2701 /* No assumptions necessary! */
2702 return PyObject_RichCompareBool(v, w, Py_LT);
2703 }
2704
2705 /* Homogeneous compare: safe for any two comparable objects of the same type.
2706 * (ms->key_richcompare is set to ob_type->tp_richcompare in the
2707 * pre-sort check.)
2708 */
2709 static int
unsafe_object_compare(PyObject * v,PyObject * w,MergeState * ms)2710 unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2711 {
2712 PyObject *res_obj; int res;
2713
2714 /* No assumptions, because we check first: */
2715 if (Py_TYPE(v)->tp_richcompare != ms->key_richcompare)
2716 return PyObject_RichCompareBool(v, w, Py_LT);
2717
2718 assert(ms->key_richcompare != NULL);
2719 res_obj = (*(ms->key_richcompare))(v, w, Py_LT);
2720
2721 if (res_obj == Py_NotImplemented) {
2722 Py_DECREF(res_obj);
2723 return PyObject_RichCompareBool(v, w, Py_LT);
2724 }
2725 if (res_obj == NULL)
2726 return -1;
2727
2728 if (PyBool_Check(res_obj)) {
2729 res = (res_obj == Py_True);
2730 }
2731 else {
2732 res = PyObject_IsTrue(res_obj);
2733 }
2734 Py_DECREF(res_obj);
2735
2736 /* Note that we can't assert
2737 * res == PyObject_RichCompareBool(v, w, Py_LT);
2738 * because of evil compare functions like this:
2739 * lambda a, b: int(random.random() * 3) - 1)
2740 * (which is actually in test_sort.py) */
2741 return res;
2742 }
2743
2744 /* Latin string compare: safe for any two latin (one byte per char) strings. */
2745 static int
unsafe_latin_compare(PyObject * v,PyObject * w,MergeState * ms)2746 unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
2747 {
2748 Py_ssize_t len;
2749 int res;
2750
2751 /* Modified from Objects/unicodeobject.c:unicode_compare, assuming: */
2752 assert(Py_IS_TYPE(v, &PyUnicode_Type));
2753 assert(Py_IS_TYPE(w, &PyUnicode_Type));
2754 assert(PyUnicode_KIND(v) == PyUnicode_KIND(w));
2755 assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
2756
2757 len = Py_MIN(PyUnicode_GET_LENGTH(v), PyUnicode_GET_LENGTH(w));
2758 res = memcmp(PyUnicode_DATA(v), PyUnicode_DATA(w), len);
2759
2760 res = (res != 0 ?
2761 res < 0 :
2762 PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w));
2763
2764 assert(res == PyObject_RichCompareBool(v, w, Py_LT));;
2765 return res;
2766 }
2767
2768 /* Bounded int compare: compare any two longs that fit in a single machine word. */
2769 static int
unsafe_long_compare(PyObject * v,PyObject * w,MergeState * ms)2770 unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
2771 {
2772 PyLongObject *vl, *wl;
2773 intptr_t v0, w0;
2774 int res;
2775
2776 /* Modified from Objects/longobject.c:long_compare, assuming: */
2777 assert(Py_IS_TYPE(v, &PyLong_Type));
2778 assert(Py_IS_TYPE(w, &PyLong_Type));
2779 assert(_PyLong_IsCompact((PyLongObject *)v));
2780 assert(_PyLong_IsCompact((PyLongObject *)w));
2781
2782 vl = (PyLongObject*)v;
2783 wl = (PyLongObject*)w;
2784
2785 v0 = _PyLong_CompactValue(vl);
2786 w0 = _PyLong_CompactValue(wl);
2787
2788 res = v0 < w0;
2789 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2790 return res;
2791 }
2792
2793 /* Float compare: compare any two floats. */
2794 static int
unsafe_float_compare(PyObject * v,PyObject * w,MergeState * ms)2795 unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms)
2796 {
2797 int res;
2798
2799 /* Modified from Objects/floatobject.c:float_richcompare, assuming: */
2800 assert(Py_IS_TYPE(v, &PyFloat_Type));
2801 assert(Py_IS_TYPE(w, &PyFloat_Type));
2802
2803 res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w);
2804 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2805 return res;
2806 }
2807
2808 /* Tuple compare: compare *any* two tuples, using
2809 * ms->tuple_elem_compare to compare the first elements, which is set
2810 * using the same pre-sort check as we use for ms->key_compare,
2811 * but run on the list [x[0] for x in L]. This allows us to optimize compares
2812 * on two levels (as long as [x[0] for x in L] is type-homogeneous.) The idea is
2813 * that most tuple compares don't involve x[1:]. */
2814 static int
unsafe_tuple_compare(PyObject * v,PyObject * w,MergeState * ms)2815 unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
2816 {
2817 PyTupleObject *vt, *wt;
2818 Py_ssize_t i, vlen, wlen;
2819 int k;
2820
2821 /* Modified from Objects/tupleobject.c:tuplerichcompare, assuming: */
2822 assert(Py_IS_TYPE(v, &PyTuple_Type));
2823 assert(Py_IS_TYPE(w, &PyTuple_Type));
2824 assert(Py_SIZE(v) > 0);
2825 assert(Py_SIZE(w) > 0);
2826
2827 vt = (PyTupleObject *)v;
2828 wt = (PyTupleObject *)w;
2829
2830 vlen = Py_SIZE(vt);
2831 wlen = Py_SIZE(wt);
2832
2833 for (i = 0; i < vlen && i < wlen; i++) {
2834 k = PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_EQ);
2835 if (k < 0)
2836 return -1;
2837 if (!k)
2838 break;
2839 }
2840
2841 if (i >= vlen || i >= wlen)
2842 return vlen < wlen;
2843
2844 if (i == 0)
2845 return ms->tuple_elem_compare(vt->ob_item[i], wt->ob_item[i], ms);
2846 else
2847 return PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_LT);
2848 }
2849
2850 /* An adaptive, stable, natural mergesort. See listsort.txt.
2851 * Returns Py_None on success, NULL on error. Even in case of error, the
2852 * list will be some permutation of its input state (nothing is lost or
2853 * duplicated).
2854 */
2855 /*[clinic input]
2856 @critical_section
2857 list.sort
2858
2859 *
2860 key as keyfunc: object = None
2861 reverse: bool = False
2862
2863 Sort the list in ascending order and return None.
2864
2865 The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
2866 order of two equal elements is maintained).
2867
2868 If a key function is given, apply it once to each list item and sort them,
2869 ascending or descending, according to their function values.
2870
2871 The reverse flag can be set to sort in descending order.
2872 [clinic start generated code]*/
2873
2874 static PyObject *
list_sort_impl(PyListObject * self,PyObject * keyfunc,int reverse)2875 list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
2876 /*[clinic end generated code: output=57b9f9c5e23fbe42 input=667bf25d0e3a3676]*/
2877 {
2878 MergeState ms;
2879 Py_ssize_t nremaining;
2880 Py_ssize_t minrun;
2881 sortslice lo;
2882 Py_ssize_t saved_ob_size, saved_allocated;
2883 PyObject **saved_ob_item;
2884 PyObject **final_ob_item;
2885 PyObject *result = NULL; /* guilty until proved innocent */
2886 Py_ssize_t i;
2887 PyObject **keys;
2888
2889 assert(self != NULL);
2890 assert(PyList_Check(self));
2891 if (keyfunc == Py_None)
2892 keyfunc = NULL;
2893
2894 /* The list is temporarily made empty, so that mutations performed
2895 * by comparison functions can't affect the slice of memory we're
2896 * sorting (allowing mutations during sorting is a core-dump
2897 * factory, since ob_item may change).
2898 */
2899 saved_ob_size = Py_SIZE(self);
2900 saved_ob_item = self->ob_item;
2901 saved_allocated = self->allocated;
2902 Py_SET_SIZE(self, 0);
2903 FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item, NULL);
2904 self->allocated = -1; /* any operation will reset it to >= 0 */
2905
2906 if (keyfunc == NULL) {
2907 keys = NULL;
2908 lo.keys = saved_ob_item;
2909 lo.values = NULL;
2910 }
2911 else {
2912 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
2913 /* Leverage stack space we allocated but won't otherwise use */
2914 keys = &ms.temparray[saved_ob_size+1];
2915 else {
2916 keys = PyMem_Malloc(sizeof(PyObject *) * saved_ob_size);
2917 if (keys == NULL) {
2918 PyErr_NoMemory();
2919 goto keyfunc_fail;
2920 }
2921 }
2922
2923 for (i = 0; i < saved_ob_size ; i++) {
2924 keys[i] = PyObject_CallOneArg(keyfunc, saved_ob_item[i]);
2925 if (keys[i] == NULL) {
2926 for (i=i-1 ; i>=0 ; i--)
2927 Py_DECREF(keys[i]);
2928 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
2929 PyMem_Free(keys);
2930 goto keyfunc_fail;
2931 }
2932 }
2933
2934 lo.keys = keys;
2935 lo.values = saved_ob_item;
2936 }
2937
2938
2939 /* The pre-sort check: here's where we decide which compare function to use.
2940 * How much optimization is safe? We test for homogeneity with respect to
2941 * several properties that are expensive to check at compare-time, and
2942 * set ms appropriately. */
2943 if (saved_ob_size > 1) {
2944 /* Assume the first element is representative of the whole list. */
2945 int keys_are_in_tuples = (Py_IS_TYPE(lo.keys[0], &PyTuple_Type) &&
2946 Py_SIZE(lo.keys[0]) > 0);
2947
2948 PyTypeObject* key_type = (keys_are_in_tuples ?
2949 Py_TYPE(PyTuple_GET_ITEM(lo.keys[0], 0)) :
2950 Py_TYPE(lo.keys[0]));
2951
2952 int keys_are_all_same_type = 1;
2953 int strings_are_latin = 1;
2954 int ints_are_bounded = 1;
2955
2956 /* Prove that assumption by checking every key. */
2957 for (i=0; i < saved_ob_size; i++) {
2958
2959 if (keys_are_in_tuples &&
2960 !(Py_IS_TYPE(lo.keys[i], &PyTuple_Type) && Py_SIZE(lo.keys[i]) != 0)) {
2961 keys_are_in_tuples = 0;
2962 keys_are_all_same_type = 0;
2963 break;
2964 }
2965
2966 /* Note: for lists of tuples, key is the first element of the tuple
2967 * lo.keys[i], not lo.keys[i] itself! We verify type-homogeneity
2968 * for lists of tuples in the if-statement directly above. */
2969 PyObject *key = (keys_are_in_tuples ?
2970 PyTuple_GET_ITEM(lo.keys[i], 0) :
2971 lo.keys[i]);
2972
2973 if (!Py_IS_TYPE(key, key_type)) {
2974 keys_are_all_same_type = 0;
2975 /* If keys are in tuple we must loop over the whole list to make
2976 sure all items are tuples */
2977 if (!keys_are_in_tuples) {
2978 break;
2979 }
2980 }
2981
2982 if (keys_are_all_same_type) {
2983 if (key_type == &PyLong_Type &&
2984 ints_are_bounded &&
2985 !_PyLong_IsCompact((PyLongObject *)key)) {
2986
2987 ints_are_bounded = 0;
2988 }
2989 else if (key_type == &PyUnicode_Type &&
2990 strings_are_latin &&
2991 PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND) {
2992
2993 strings_are_latin = 0;
2994 }
2995 }
2996 }
2997
2998 /* Choose the best compare, given what we now know about the keys. */
2999 if (keys_are_all_same_type) {
3000
3001 if (key_type == &PyUnicode_Type && strings_are_latin) {
3002 ms.key_compare = unsafe_latin_compare;
3003 }
3004 else if (key_type == &PyLong_Type && ints_are_bounded) {
3005 ms.key_compare = unsafe_long_compare;
3006 }
3007 else if (key_type == &PyFloat_Type) {
3008 ms.key_compare = unsafe_float_compare;
3009 }
3010 else if ((ms.key_richcompare = key_type->tp_richcompare) != NULL) {
3011 ms.key_compare = unsafe_object_compare;
3012 }
3013 else {
3014 ms.key_compare = safe_object_compare;
3015 }
3016 }
3017 else {
3018 ms.key_compare = safe_object_compare;
3019 }
3020
3021 if (keys_are_in_tuples) {
3022 /* Make sure we're not dealing with tuples of tuples
3023 * (remember: here, key_type refers list [key[0] for key in keys]) */
3024 if (key_type == &PyTuple_Type) {
3025 ms.tuple_elem_compare = safe_object_compare;
3026 }
3027 else {
3028 ms.tuple_elem_compare = ms.key_compare;
3029 }
3030
3031 ms.key_compare = unsafe_tuple_compare;
3032 }
3033 }
3034 /* End of pre-sort check: ms is now set properly! */
3035
3036 merge_init(&ms, saved_ob_size, keys != NULL, &lo);
3037
3038 nremaining = saved_ob_size;
3039 if (nremaining < 2)
3040 goto succeed;
3041
3042 /* Reverse sort stability achieved by initially reversing the list,
3043 applying a stable forward sort, then reversing the final result. */
3044 if (reverse) {
3045 if (keys != NULL)
3046 reverse_slice(&keys[0], &keys[saved_ob_size]);
3047 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
3048 }
3049
3050 /* March over the array once, left to right, finding natural runs,
3051 * and extending short natural runs to minrun elements.
3052 */
3053 minrun = merge_compute_minrun(nremaining);
3054 do {
3055 Py_ssize_t n;
3056
3057 /* Identify next run. */
3058 n = count_run(&ms, &lo, nremaining);
3059 if (n < 0)
3060 goto fail;
3061 /* If short, extend to min(minrun, nremaining). */
3062 if (n < minrun) {
3063 const Py_ssize_t force = nremaining <= minrun ?
3064 nremaining : minrun;
3065 if (binarysort(&ms, &lo, force, n) < 0)
3066 goto fail;
3067 n = force;
3068 }
3069 /* Maybe merge pending runs. */
3070 assert(ms.n == 0 || ms.pending[ms.n -1].base.keys +
3071 ms.pending[ms.n-1].len == lo.keys);
3072 if (found_new_run(&ms, n) < 0)
3073 goto fail;
3074 /* Push new run on stack. */
3075 assert(ms.n < MAX_MERGE_PENDING);
3076 ms.pending[ms.n].base = lo;
3077 ms.pending[ms.n].len = n;
3078 ++ms.n;
3079 /* Advance to find next run. */
3080 sortslice_advance(&lo, n);
3081 nremaining -= n;
3082 } while (nremaining);
3083
3084 if (merge_force_collapse(&ms) < 0)
3085 goto fail;
3086 assert(ms.n == 1);
3087 assert(keys == NULL
3088 ? ms.pending[0].base.keys == saved_ob_item
3089 : ms.pending[0].base.keys == &keys[0]);
3090 assert(ms.pending[0].len == saved_ob_size);
3091 lo = ms.pending[0].base;
3092
3093 succeed:
3094 result = Py_None;
3095 fail:
3096 if (keys != NULL) {
3097 for (i = 0; i < saved_ob_size; i++)
3098 Py_DECREF(keys[i]);
3099 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
3100 PyMem_Free(keys);
3101 }
3102
3103 if (self->allocated != -1 && result != NULL) {
3104 /* The user mucked with the list during the sort,
3105 * and we don't already have another error to report.
3106 */
3107 PyErr_SetString(PyExc_ValueError, "list modified during sort");
3108 result = NULL;
3109 }
3110
3111 if (reverse && saved_ob_size > 1)
3112 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
3113
3114 merge_freemem(&ms);
3115
3116 keyfunc_fail:
3117 final_ob_item = self->ob_item;
3118 i = Py_SIZE(self);
3119 Py_SET_SIZE(self, saved_ob_size);
3120 FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item, saved_ob_item);
3121 FT_ATOMIC_STORE_SSIZE_RELAXED(self->allocated, saved_allocated);
3122 if (final_ob_item != NULL) {
3123 /* we cannot use list_clear() for this because it does not
3124 guarantee that the list is really empty when it returns */
3125 while (--i >= 0) {
3126 Py_XDECREF(final_ob_item[i]);
3127 }
3128 #ifdef Py_GIL_DISABLED
3129 ensure_shared_on_resize(self);
3130 bool use_qsbr = _PyObject_GC_IS_SHARED(self);
3131 #else
3132 bool use_qsbr = false;
3133 #endif
3134 free_list_items(final_ob_item, use_qsbr);
3135 }
3136 return Py_XNewRef(result);
3137 }
3138 #undef IFLT
3139 #undef ISLT
3140
3141 int
PyList_Sort(PyObject * v)3142 PyList_Sort(PyObject *v)
3143 {
3144 if (v == NULL || !PyList_Check(v)) {
3145 PyErr_BadInternalCall();
3146 return -1;
3147 }
3148 Py_BEGIN_CRITICAL_SECTION(v);
3149 v = list_sort_impl((PyListObject *)v, NULL, 0);
3150 Py_END_CRITICAL_SECTION();
3151 if (v == NULL)
3152 return -1;
3153 Py_DECREF(v);
3154 return 0;
3155 }
3156
3157 /*[clinic input]
3158 @critical_section
3159 list.reverse
3160
3161 Reverse *IN PLACE*.
3162 [clinic start generated code]*/
3163
3164 static PyObject *
list_reverse_impl(PyListObject * self)3165 list_reverse_impl(PyListObject *self)
3166 /*[clinic end generated code: output=482544fc451abea9 input=04ac8e0c6a66e4d9]*/
3167 {
3168 if (Py_SIZE(self) > 1)
3169 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
3170 Py_RETURN_NONE;
3171 }
3172
3173 int
PyList_Reverse(PyObject * v)3174 PyList_Reverse(PyObject *v)
3175 {
3176 PyListObject *self = (PyListObject *)v;
3177
3178 if (v == NULL || !PyList_Check(v)) {
3179 PyErr_BadInternalCall();
3180 return -1;
3181 }
3182 Py_BEGIN_CRITICAL_SECTION(self);
3183 if (Py_SIZE(self) > 1) {
3184 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
3185 }
3186 Py_END_CRITICAL_SECTION()
3187 return 0;
3188 }
3189
3190 PyObject *
PyList_AsTuple(PyObject * v)3191 PyList_AsTuple(PyObject *v)
3192 {
3193 if (v == NULL || !PyList_Check(v)) {
3194 PyErr_BadInternalCall();
3195 return NULL;
3196 }
3197 PyObject *ret;
3198 PyListObject *self = (PyListObject *)v;
3199 Py_BEGIN_CRITICAL_SECTION(self);
3200 ret = _PyTuple_FromArray(self->ob_item, Py_SIZE(v));
3201 Py_END_CRITICAL_SECTION();
3202 return ret;
3203 }
3204
3205 PyObject *
_PyList_FromArraySteal(PyObject * const * src,Py_ssize_t n)3206 _PyList_FromArraySteal(PyObject *const *src, Py_ssize_t n)
3207 {
3208 if (n == 0) {
3209 return PyList_New(0);
3210 }
3211
3212 PyListObject *list = (PyListObject *)PyList_New(n);
3213 if (list == NULL) {
3214 for (Py_ssize_t i = 0; i < n; i++) {
3215 Py_DECREF(src[i]);
3216 }
3217 return NULL;
3218 }
3219
3220 PyObject **dst = list->ob_item;
3221 memcpy(dst, src, n * sizeof(PyObject *));
3222
3223 return (PyObject *)list;
3224 }
3225
3226 /*[clinic input]
3227 list.index
3228
3229 value: object
3230 start: slice_index(accept={int}) = 0
3231 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
3232 /
3233
3234 Return first index of value.
3235
3236 Raises ValueError if the value is not present.
3237 [clinic start generated code]*/
3238
3239 static PyObject *
list_index_impl(PyListObject * self,PyObject * value,Py_ssize_t start,Py_ssize_t stop)3240 list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
3241 Py_ssize_t stop)
3242 /*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
3243 {
3244 if (start < 0) {
3245 start += Py_SIZE(self);
3246 if (start < 0)
3247 start = 0;
3248 }
3249 if (stop < 0) {
3250 stop += Py_SIZE(self);
3251 if (stop < 0)
3252 stop = 0;
3253 }
3254 for (Py_ssize_t i = start; i < stop; i++) {
3255 PyObject *obj = list_get_item_ref(self, i);
3256 if (obj == NULL) {
3257 // out-of-bounds
3258 break;
3259 }
3260 int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
3261 Py_DECREF(obj);
3262 if (cmp > 0)
3263 return PyLong_FromSsize_t(i);
3264 else if (cmp < 0)
3265 return NULL;
3266 }
3267 PyErr_Format(PyExc_ValueError, "%R is not in list", value);
3268 return NULL;
3269 }
3270
3271 /*[clinic input]
3272 list.count
3273
3274 value: object
3275 /
3276
3277 Return number of occurrences of value.
3278 [clinic start generated code]*/
3279
3280 static PyObject *
list_count(PyListObject * self,PyObject * value)3281 list_count(PyListObject *self, PyObject *value)
3282 /*[clinic end generated code: output=b1f5d284205ae714 input=3bdc3a5e6f749565]*/
3283 {
3284 Py_ssize_t count = 0;
3285 for (Py_ssize_t i = 0; ; i++) {
3286 PyObject *obj = list_get_item_ref(self, i);
3287 if (obj == NULL) {
3288 // out-of-bounds
3289 break;
3290 }
3291 if (obj == value) {
3292 count++;
3293 Py_DECREF(obj);
3294 continue;
3295 }
3296 int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
3297 Py_DECREF(obj);
3298 if (cmp > 0)
3299 count++;
3300 else if (cmp < 0)
3301 return NULL;
3302 }
3303 return PyLong_FromSsize_t(count);
3304 }
3305
3306 /*[clinic input]
3307 @critical_section
3308 list.remove
3309
3310 value: object
3311 /
3312
3313 Remove first occurrence of value.
3314
3315 Raises ValueError if the value is not present.
3316 [clinic start generated code]*/
3317
3318 static PyObject *
list_remove_impl(PyListObject * self,PyObject * value)3319 list_remove_impl(PyListObject *self, PyObject *value)
3320 /*[clinic end generated code: output=b9b76a6633b18778 input=26c813dbb95aa93b]*/
3321 {
3322 Py_ssize_t i;
3323
3324 for (i = 0; i < Py_SIZE(self); i++) {
3325 PyObject *obj = self->ob_item[i];
3326 Py_INCREF(obj);
3327 int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
3328 Py_DECREF(obj);
3329 if (cmp > 0) {
3330 if (list_ass_slice_lock_held(self, i, i+1, NULL) == 0)
3331 Py_RETURN_NONE;
3332 return NULL;
3333 }
3334 else if (cmp < 0)
3335 return NULL;
3336 }
3337 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
3338 return NULL;
3339 }
3340
3341 static int
list_traverse(PyObject * self,visitproc visit,void * arg)3342 list_traverse(PyObject *self, visitproc visit, void *arg)
3343 {
3344 PyListObject *o = (PyListObject *)self;
3345 Py_ssize_t i;
3346
3347 for (i = Py_SIZE(o); --i >= 0; )
3348 Py_VISIT(o->ob_item[i]);
3349 return 0;
3350 }
3351
3352 static PyObject *
list_richcompare_impl(PyObject * v,PyObject * w,int op)3353 list_richcompare_impl(PyObject *v, PyObject *w, int op)
3354 {
3355 PyListObject *vl, *wl;
3356 Py_ssize_t i;
3357
3358 if (!PyList_Check(v) || !PyList_Check(w))
3359 Py_RETURN_NOTIMPLEMENTED;
3360
3361 vl = (PyListObject *)v;
3362 wl = (PyListObject *)w;
3363
3364 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
3365 /* Shortcut: if the lengths differ, the lists differ */
3366 if (op == Py_EQ)
3367 Py_RETURN_FALSE;
3368 else
3369 Py_RETURN_TRUE;
3370 }
3371
3372 /* Search for the first index where items are different */
3373 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
3374 PyObject *vitem = vl->ob_item[i];
3375 PyObject *witem = wl->ob_item[i];
3376 if (vitem == witem) {
3377 continue;
3378 }
3379
3380 Py_INCREF(vitem);
3381 Py_INCREF(witem);
3382 int k = PyObject_RichCompareBool(vitem, witem, Py_EQ);
3383 Py_DECREF(vitem);
3384 Py_DECREF(witem);
3385 if (k < 0)
3386 return NULL;
3387 if (!k)
3388 break;
3389 }
3390
3391 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
3392 /* No more items to compare -- compare sizes */
3393 Py_RETURN_RICHCOMPARE(Py_SIZE(vl), Py_SIZE(wl), op);
3394 }
3395
3396 /* We have an item that differs -- shortcuts for EQ/NE */
3397 if (op == Py_EQ) {
3398 Py_RETURN_FALSE;
3399 }
3400 if (op == Py_NE) {
3401 Py_RETURN_TRUE;
3402 }
3403
3404 /* Compare the final item again using the proper operator */
3405 PyObject *vitem = vl->ob_item[i];
3406 PyObject *witem = wl->ob_item[i];
3407 Py_INCREF(vitem);
3408 Py_INCREF(witem);
3409 PyObject *result = PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
3410 Py_DECREF(vitem);
3411 Py_DECREF(witem);
3412 return result;
3413 }
3414
3415 static PyObject *
list_richcompare(PyObject * v,PyObject * w,int op)3416 list_richcompare(PyObject *v, PyObject *w, int op)
3417 {
3418 PyObject *ret;
3419 Py_BEGIN_CRITICAL_SECTION2(v, w);
3420 ret = list_richcompare_impl(v, w, op);
3421 Py_END_CRITICAL_SECTION2()
3422 return ret;
3423 }
3424
3425 /*[clinic input]
3426 list.__init__
3427
3428 iterable: object(c_default="NULL") = ()
3429 /
3430
3431 Built-in mutable sequence.
3432
3433 If no argument is given, the constructor creates a new empty list.
3434 The argument must be an iterable if specified.
3435 [clinic start generated code]*/
3436
3437 static int
list___init___impl(PyListObject * self,PyObject * iterable)3438 list___init___impl(PyListObject *self, PyObject *iterable)
3439 /*[clinic end generated code: output=0f3c21379d01de48 input=b3f3fe7206af8f6b]*/
3440 {
3441 /* Verify list invariants established by PyType_GenericAlloc() */
3442 assert(0 <= Py_SIZE(self));
3443 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
3444 assert(self->ob_item != NULL ||
3445 self->allocated == 0 || self->allocated == -1);
3446
3447 /* Empty previous contents */
3448 if (self->ob_item != NULL) {
3449 list_clear(self);
3450 }
3451 if (iterable != NULL) {
3452 if (_list_extend(self, iterable) < 0) {
3453 return -1;
3454 }
3455 }
3456 return 0;
3457 }
3458
3459 static PyObject *
list_vectorcall(PyObject * type,PyObject * const * args,size_t nargsf,PyObject * kwnames)3460 list_vectorcall(PyObject *type, PyObject * const*args,
3461 size_t nargsf, PyObject *kwnames)
3462 {
3463 if (!_PyArg_NoKwnames("list", kwnames)) {
3464 return NULL;
3465 }
3466 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
3467 if (!_PyArg_CheckPositional("list", nargs, 0, 1)) {
3468 return NULL;
3469 }
3470
3471 PyObject *list = PyType_GenericAlloc(_PyType_CAST(type), 0);
3472 if (list == NULL) {
3473 return NULL;
3474 }
3475 if (nargs) {
3476 if (list___init___impl((PyListObject *)list, args[0])) {
3477 Py_DECREF(list);
3478 return NULL;
3479 }
3480 }
3481 return list;
3482 }
3483
3484
3485 /*[clinic input]
3486 list.__sizeof__
3487
3488 Return the size of the list in memory, in bytes.
3489 [clinic start generated code]*/
3490
3491 static PyObject *
list___sizeof___impl(PyListObject * self)3492 list___sizeof___impl(PyListObject *self)
3493 /*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
3494 {
3495 size_t res = _PyObject_SIZE(Py_TYPE(self));
3496 Py_ssize_t allocated = FT_ATOMIC_LOAD_SSIZE_RELAXED(self->allocated);
3497 res += (size_t)allocated * sizeof(void*);
3498 return PyLong_FromSize_t(res);
3499 }
3500
3501 static PyObject *list_iter(PyObject *seq);
3502 static PyObject *list_subscript(PyObject*, PyObject*);
3503
3504 static PyMethodDef list_methods[] = {
3505 {"__getitem__", list_subscript, METH_O|METH_COEXIST,
3506 PyDoc_STR("__getitem__($self, index, /)\n--\n\nReturn self[index].")},
3507 LIST___REVERSED___METHODDEF
3508 LIST___SIZEOF___METHODDEF
3509 PY_LIST_CLEAR_METHODDEF
3510 LIST_COPY_METHODDEF
3511 LIST_APPEND_METHODDEF
3512 LIST_INSERT_METHODDEF
3513 LIST_EXTEND_METHODDEF
3514 LIST_POP_METHODDEF
3515 LIST_REMOVE_METHODDEF
3516 LIST_INDEX_METHODDEF
3517 LIST_COUNT_METHODDEF
3518 LIST_REVERSE_METHODDEF
3519 LIST_SORT_METHODDEF
3520 {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
3521 {NULL, NULL} /* sentinel */
3522 };
3523
3524 static PySequenceMethods list_as_sequence = {
3525 list_length, /* sq_length */
3526 list_concat, /* sq_concat */
3527 list_repeat, /* sq_repeat */
3528 list_item, /* sq_item */
3529 0, /* sq_slice */
3530 list_ass_item, /* sq_ass_item */
3531 0, /* sq_ass_slice */
3532 list_contains, /* sq_contains */
3533 list_inplace_concat, /* sq_inplace_concat */
3534 list_inplace_repeat, /* sq_inplace_repeat */
3535 };
3536
3537 static inline PyObject *
list_slice_step_lock_held(PyListObject * a,Py_ssize_t start,Py_ssize_t step,Py_ssize_t len)3538 list_slice_step_lock_held(PyListObject *a, Py_ssize_t start, Py_ssize_t step, Py_ssize_t len)
3539 {
3540 PyListObject *np = (PyListObject *)list_new_prealloc(len);
3541 if (np == NULL) {
3542 return NULL;
3543 }
3544 size_t cur;
3545 Py_ssize_t i;
3546 PyObject **src = a->ob_item;
3547 PyObject **dest = np->ob_item;
3548 for (cur = start, i = 0; i < len;
3549 cur += (size_t)step, i++) {
3550 PyObject *v = src[cur];
3551 dest[i] = Py_NewRef(v);
3552 }
3553 Py_SET_SIZE(np, len);
3554 return (PyObject *)np;
3555 }
3556
3557 static PyObject *
list_slice_wrap(PyListObject * aa,Py_ssize_t start,Py_ssize_t stop,Py_ssize_t step)3558 list_slice_wrap(PyListObject *aa, Py_ssize_t start, Py_ssize_t stop, Py_ssize_t step)
3559 {
3560 PyObject *res = NULL;
3561 Py_BEGIN_CRITICAL_SECTION(aa);
3562 Py_ssize_t len = PySlice_AdjustIndices(Py_SIZE(aa), &start, &stop, step);
3563 if (len <= 0) {
3564 res = PyList_New(0);
3565 }
3566 else if (step == 1) {
3567 res = list_slice_lock_held(aa, start, stop);
3568 }
3569 else {
3570 res = list_slice_step_lock_held(aa, start, step, len);
3571 }
3572 Py_END_CRITICAL_SECTION();
3573 return res;
3574 }
3575
3576 static PyObject *
list_subscript(PyObject * _self,PyObject * item)3577 list_subscript(PyObject* _self, PyObject* item)
3578 {
3579 PyListObject* self = (PyListObject*)_self;
3580 if (_PyIndex_Check(item)) {
3581 Py_ssize_t i;
3582 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
3583 if (i == -1 && PyErr_Occurred())
3584 return NULL;
3585 if (i < 0)
3586 i += PyList_GET_SIZE(self);
3587 return list_item((PyObject *)self, i);
3588 }
3589 else if (PySlice_Check(item)) {
3590 Py_ssize_t start, stop, step;
3591 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
3592 return NULL;
3593 }
3594 return list_slice_wrap(self, start, stop, step);
3595 }
3596 else {
3597 PyErr_Format(PyExc_TypeError,
3598 "list indices must be integers or slices, not %.200s",
3599 Py_TYPE(item)->tp_name);
3600 return NULL;
3601 }
3602 }
3603
3604 static Py_ssize_t
adjust_slice_indexes(PyListObject * lst,Py_ssize_t * start,Py_ssize_t * stop,Py_ssize_t step)3605 adjust_slice_indexes(PyListObject *lst,
3606 Py_ssize_t *start, Py_ssize_t *stop,
3607 Py_ssize_t step)
3608 {
3609 Py_ssize_t slicelength = PySlice_AdjustIndices(Py_SIZE(lst), start, stop,
3610 step);
3611
3612 /* Make sure s[5:2] = [..] inserts at the right place:
3613 before 5, not before 2. */
3614 if ((step < 0 && *start < *stop) ||
3615 (step > 0 && *start > *stop))
3616 *stop = *start;
3617
3618 return slicelength;
3619 }
3620
3621 static int
list_ass_subscript(PyObject * _self,PyObject * item,PyObject * value)3622 list_ass_subscript(PyObject* _self, PyObject* item, PyObject* value)
3623 {
3624 PyListObject *self = (PyListObject *)_self;
3625 if (_PyIndex_Check(item)) {
3626 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
3627 if (i == -1 && PyErr_Occurred())
3628 return -1;
3629 if (i < 0)
3630 i += PyList_GET_SIZE(self);
3631 return list_ass_item((PyObject *)self, i, value);
3632 }
3633 else if (PySlice_Check(item)) {
3634 Py_ssize_t start, stop, step;
3635
3636 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
3637 return -1;
3638 }
3639
3640 if (value == NULL) {
3641 /* delete slice */
3642 PyObject **garbage;
3643 size_t cur;
3644 Py_ssize_t i;
3645 int res;
3646
3647 Py_ssize_t slicelength = adjust_slice_indexes(self, &start, &stop,
3648 step);
3649
3650 if (step == 1)
3651 return list_ass_slice(self, start, stop, value);
3652
3653 if (slicelength <= 0)
3654 return 0;
3655
3656 if (step < 0) {
3657 stop = start + 1;
3658 start = stop + step*(slicelength - 1) - 1;
3659 step = -step;
3660 }
3661
3662 garbage = (PyObject**)
3663 PyMem_Malloc(slicelength*sizeof(PyObject*));
3664 if (!garbage) {
3665 PyErr_NoMemory();
3666 return -1;
3667 }
3668
3669 /* drawing pictures might help understand these for
3670 loops. Basically, we memmove the parts of the
3671 list that are *not* part of the slice: step-1
3672 items for each item that is part of the slice,
3673 and then tail end of the list that was not
3674 covered by the slice */
3675 for (cur = start, i = 0;
3676 cur < (size_t)stop;
3677 cur += step, i++) {
3678 Py_ssize_t lim = step - 1;
3679
3680 garbage[i] = PyList_GET_ITEM(self, cur);
3681
3682 if (cur + step >= (size_t)Py_SIZE(self)) {
3683 lim = Py_SIZE(self) - cur - 1;
3684 }
3685
3686 memmove(self->ob_item + cur - i,
3687 self->ob_item + cur + 1,
3688 lim * sizeof(PyObject *));
3689 }
3690 cur = start + (size_t)slicelength * step;
3691 if (cur < (size_t)Py_SIZE(self)) {
3692 memmove(self->ob_item + cur - slicelength,
3693 self->ob_item + cur,
3694 (Py_SIZE(self) - cur) *
3695 sizeof(PyObject *));
3696 }
3697
3698 Py_SET_SIZE(self, Py_SIZE(self) - slicelength);
3699 res = list_resize(self, Py_SIZE(self));
3700
3701 for (i = 0; i < slicelength; i++) {
3702 Py_DECREF(garbage[i]);
3703 }
3704 PyMem_Free(garbage);
3705
3706 return res;
3707 }
3708 else {
3709 /* assign slice */
3710 PyObject *ins, *seq;
3711 PyObject **garbage, **seqitems, **selfitems;
3712 Py_ssize_t i;
3713 size_t cur;
3714
3715 /* protect against a[::-1] = a */
3716 if (self == (PyListObject*)value) {
3717 Py_BEGIN_CRITICAL_SECTION(value);
3718 seq = list_slice_lock_held((PyListObject*)value, 0,
3719 Py_SIZE(value));
3720 Py_END_CRITICAL_SECTION();
3721 }
3722 else {
3723 seq = PySequence_Fast(value,
3724 "must assign iterable "
3725 "to extended slice");
3726 }
3727 if (!seq)
3728 return -1;
3729
3730 Py_ssize_t slicelength = adjust_slice_indexes(self, &start, &stop,
3731 step);
3732
3733 if (step == 1) {
3734 int res = list_ass_slice(self, start, stop, seq);
3735 Py_DECREF(seq);
3736 return res;
3737 }
3738
3739 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
3740 PyErr_Format(PyExc_ValueError,
3741 "attempt to assign sequence of "
3742 "size %zd to extended slice of "
3743 "size %zd",
3744 PySequence_Fast_GET_SIZE(seq),
3745 slicelength);
3746 Py_DECREF(seq);
3747 return -1;
3748 }
3749
3750 if (!slicelength) {
3751 Py_DECREF(seq);
3752 return 0;
3753 }
3754
3755 garbage = (PyObject**)
3756 PyMem_Malloc(slicelength*sizeof(PyObject*));
3757 if (!garbage) {
3758 Py_DECREF(seq);
3759 PyErr_NoMemory();
3760 return -1;
3761 }
3762
3763 selfitems = self->ob_item;
3764 seqitems = PySequence_Fast_ITEMS(seq);
3765 for (cur = start, i = 0; i < slicelength;
3766 cur += (size_t)step, i++) {
3767 garbage[i] = selfitems[cur];
3768 ins = Py_NewRef(seqitems[i]);
3769 selfitems[cur] = ins;
3770 }
3771
3772 for (i = 0; i < slicelength; i++) {
3773 Py_DECREF(garbage[i]);
3774 }
3775
3776 PyMem_Free(garbage);
3777 Py_DECREF(seq);
3778
3779 return 0;
3780 }
3781 }
3782 else {
3783 PyErr_Format(PyExc_TypeError,
3784 "list indices must be integers or slices, not %.200s",
3785 Py_TYPE(item)->tp_name);
3786 return -1;
3787 }
3788 }
3789
3790 static PyMappingMethods list_as_mapping = {
3791 list_length,
3792 list_subscript,
3793 list_ass_subscript
3794 };
3795
3796 PyTypeObject PyList_Type = {
3797 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3798 "list",
3799 sizeof(PyListObject),
3800 0,
3801 list_dealloc, /* tp_dealloc */
3802 0, /* tp_vectorcall_offset */
3803 0, /* tp_getattr */
3804 0, /* tp_setattr */
3805 0, /* tp_as_async */
3806 list_repr, /* tp_repr */
3807 0, /* tp_as_number */
3808 &list_as_sequence, /* tp_as_sequence */
3809 &list_as_mapping, /* tp_as_mapping */
3810 PyObject_HashNotImplemented, /* tp_hash */
3811 0, /* tp_call */
3812 0, /* tp_str */
3813 PyObject_GenericGetAttr, /* tp_getattro */
3814 0, /* tp_setattro */
3815 0, /* tp_as_buffer */
3816 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3817 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS |
3818 _Py_TPFLAGS_MATCH_SELF | Py_TPFLAGS_SEQUENCE, /* tp_flags */
3819 list___init____doc__, /* tp_doc */
3820 list_traverse, /* tp_traverse */
3821 list_clear_slot, /* tp_clear */
3822 list_richcompare, /* tp_richcompare */
3823 0, /* tp_weaklistoffset */
3824 list_iter, /* tp_iter */
3825 0, /* tp_iternext */
3826 list_methods, /* tp_methods */
3827 0, /* tp_members */
3828 0, /* tp_getset */
3829 0, /* tp_base */
3830 0, /* tp_dict */
3831 0, /* tp_descr_get */
3832 0, /* tp_descr_set */
3833 0, /* tp_dictoffset */
3834 (initproc)list___init__, /* tp_init */
3835 PyType_GenericAlloc, /* tp_alloc */
3836 PyType_GenericNew, /* tp_new */
3837 PyObject_GC_Del, /* tp_free */
3838 .tp_vectorcall = list_vectorcall,
3839 };
3840
3841 /*********************** List Iterator **************************/
3842
3843 static void listiter_dealloc(PyObject *);
3844 static int listiter_traverse(PyObject *, visitproc, void *);
3845 static PyObject *listiter_next(PyObject *);
3846 static PyObject *listiter_len(PyObject *, PyObject *);
3847 static PyObject *listiter_reduce_general(void *_it, int forward);
3848 static PyObject *listiter_reduce(PyObject *, PyObject *);
3849 static PyObject *listiter_setstate(PyObject *, PyObject *state);
3850
3851 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
3852 PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3853 PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
3854
3855 static PyMethodDef listiter_methods[] = {
3856 {"__length_hint__", listiter_len, METH_NOARGS, length_hint_doc},
3857 {"__reduce__", listiter_reduce, METH_NOARGS, reduce_doc},
3858 {"__setstate__", listiter_setstate, METH_O, setstate_doc},
3859 {NULL, NULL} /* sentinel */
3860 };
3861
3862 PyTypeObject PyListIter_Type = {
3863 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3864 "list_iterator", /* tp_name */
3865 sizeof(_PyListIterObject), /* tp_basicsize */
3866 0, /* tp_itemsize */
3867 /* methods */
3868 listiter_dealloc, /* tp_dealloc */
3869 0, /* tp_vectorcall_offset */
3870 0, /* tp_getattr */
3871 0, /* tp_setattr */
3872 0, /* tp_as_async */
3873 0, /* tp_repr */
3874 0, /* tp_as_number */
3875 0, /* tp_as_sequence */
3876 0, /* tp_as_mapping */
3877 0, /* tp_hash */
3878 0, /* tp_call */
3879 0, /* tp_str */
3880 PyObject_GenericGetAttr, /* tp_getattro */
3881 0, /* tp_setattro */
3882 0, /* tp_as_buffer */
3883 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3884 0, /* tp_doc */
3885 listiter_traverse, /* tp_traverse */
3886 0, /* tp_clear */
3887 0, /* tp_richcompare */
3888 0, /* tp_weaklistoffset */
3889 PyObject_SelfIter, /* tp_iter */
3890 listiter_next, /* tp_iternext */
3891 listiter_methods, /* tp_methods */
3892 0, /* tp_members */
3893 };
3894
3895
3896 static PyObject *
list_iter(PyObject * seq)3897 list_iter(PyObject *seq)
3898 {
3899 _PyListIterObject *it;
3900
3901 if (!PyList_Check(seq)) {
3902 PyErr_BadInternalCall();
3903 return NULL;
3904 }
3905 it = PyObject_GC_New(_PyListIterObject, &PyListIter_Type);
3906 if (it == NULL)
3907 return NULL;
3908 it->it_index = 0;
3909 it->it_seq = (PyListObject *)Py_NewRef(seq);
3910 _PyObject_GC_TRACK(it);
3911 return (PyObject *)it;
3912 }
3913
3914 static void
listiter_dealloc(PyObject * self)3915 listiter_dealloc(PyObject *self)
3916 {
3917 _PyListIterObject *it = (_PyListIterObject *)self;
3918 _PyObject_GC_UNTRACK(it);
3919 Py_XDECREF(it->it_seq);
3920 PyObject_GC_Del(it);
3921 }
3922
3923 static int
listiter_traverse(PyObject * it,visitproc visit,void * arg)3924 listiter_traverse(PyObject *it, visitproc visit, void *arg)
3925 {
3926 Py_VISIT(((_PyListIterObject *)it)->it_seq);
3927 return 0;
3928 }
3929
3930 static PyObject *
listiter_next(PyObject * self)3931 listiter_next(PyObject *self)
3932 {
3933 _PyListIterObject *it = (_PyListIterObject *)self;
3934 Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index);
3935 if (index < 0) {
3936 return NULL;
3937 }
3938
3939 PyObject *item = list_get_item_ref(it->it_seq, index);
3940 if (item == NULL) {
3941 // out-of-bounds
3942 FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, -1);
3943 #ifndef Py_GIL_DISABLED
3944 PyListObject *seq = it->it_seq;
3945 it->it_seq = NULL;
3946 Py_DECREF(seq);
3947 #endif
3948 return NULL;
3949 }
3950 FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, index + 1);
3951 return item;
3952 }
3953
3954 static PyObject *
listiter_len(PyObject * self,PyObject * Py_UNUSED (ignored))3955 listiter_len(PyObject *self, PyObject *Py_UNUSED(ignored))
3956 {
3957 assert(self != NULL);
3958 _PyListIterObject *it = (_PyListIterObject *)self;
3959 Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index);
3960 if (index >= 0) {
3961 Py_ssize_t len = PyList_GET_SIZE(it->it_seq) - index;
3962 if (len >= 0)
3963 return PyLong_FromSsize_t(len);
3964 }
3965 return PyLong_FromLong(0);
3966 }
3967
3968 static PyObject *
listiter_reduce(PyObject * it,PyObject * Py_UNUSED (ignored))3969 listiter_reduce(PyObject *it, PyObject *Py_UNUSED(ignored))
3970 {
3971 return listiter_reduce_general(it, 1);
3972 }
3973
3974 static PyObject *
listiter_setstate(PyObject * self,PyObject * state)3975 listiter_setstate(PyObject *self, PyObject *state)
3976 {
3977 _PyListIterObject *it = (_PyListIterObject *)self;
3978 Py_ssize_t index = PyLong_AsSsize_t(state);
3979 if (index == -1 && PyErr_Occurred())
3980 return NULL;
3981 if (it->it_seq != NULL) {
3982 if (index < -1)
3983 index = -1;
3984 else if (index > PyList_GET_SIZE(it->it_seq))
3985 index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
3986 it->it_index = index;
3987 }
3988 Py_RETURN_NONE;
3989 }
3990
3991 /*********************** List Reverse Iterator **************************/
3992
3993 typedef struct {
3994 PyObject_HEAD
3995 Py_ssize_t it_index;
3996 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
3997 } listreviterobject;
3998
3999 static void listreviter_dealloc(PyObject *);
4000 static int listreviter_traverse(PyObject *, visitproc, void *);
4001 static PyObject *listreviter_next(PyObject *);
4002 static PyObject *listreviter_len(PyObject *, PyObject *);
4003 static PyObject *listreviter_reduce(PyObject *, PyObject *);
4004 static PyObject *listreviter_setstate(PyObject *, PyObject *);
4005
4006 static PyMethodDef listreviter_methods[] = {
4007 {"__length_hint__", listreviter_len, METH_NOARGS, length_hint_doc},
4008 {"__reduce__", listreviter_reduce, METH_NOARGS, reduce_doc},
4009 {"__setstate__", listreviter_setstate, METH_O, setstate_doc},
4010 {NULL, NULL} /* sentinel */
4011 };
4012
4013 PyTypeObject PyListRevIter_Type = {
4014 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4015 "list_reverseiterator", /* tp_name */
4016 sizeof(listreviterobject), /* tp_basicsize */
4017 0, /* tp_itemsize */
4018 /* methods */
4019 listreviter_dealloc, /* tp_dealloc */
4020 0, /* tp_vectorcall_offset */
4021 0, /* tp_getattr */
4022 0, /* tp_setattr */
4023 0, /* tp_as_async */
4024 0, /* tp_repr */
4025 0, /* tp_as_number */
4026 0, /* tp_as_sequence */
4027 0, /* tp_as_mapping */
4028 0, /* tp_hash */
4029 0, /* tp_call */
4030 0, /* tp_str */
4031 PyObject_GenericGetAttr, /* tp_getattro */
4032 0, /* tp_setattro */
4033 0, /* tp_as_buffer */
4034 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4035 0, /* tp_doc */
4036 listreviter_traverse, /* tp_traverse */
4037 0, /* tp_clear */
4038 0, /* tp_richcompare */
4039 0, /* tp_weaklistoffset */
4040 PyObject_SelfIter, /* tp_iter */
4041 listreviter_next, /* tp_iternext */
4042 listreviter_methods, /* tp_methods */
4043 0,
4044 };
4045
4046 /*[clinic input]
4047 list.__reversed__
4048
4049 Return a reverse iterator over the list.
4050 [clinic start generated code]*/
4051
4052 static PyObject *
list___reversed___impl(PyListObject * self)4053 list___reversed___impl(PyListObject *self)
4054 /*[clinic end generated code: output=b166f073208c888c input=eadb6e17f8a6a280]*/
4055 {
4056 listreviterobject *it;
4057
4058 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
4059 if (it == NULL)
4060 return NULL;
4061 assert(PyList_Check(self));
4062 it->it_index = PyList_GET_SIZE(self) - 1;
4063 it->it_seq = (PyListObject*)Py_NewRef(self);
4064 PyObject_GC_Track(it);
4065 return (PyObject *)it;
4066 }
4067
4068 static void
listreviter_dealloc(PyObject * self)4069 listreviter_dealloc(PyObject *self)
4070 {
4071 listreviterobject *it = (listreviterobject *)self;
4072 PyObject_GC_UnTrack(it);
4073 Py_XDECREF(it->it_seq);
4074 PyObject_GC_Del(it);
4075 }
4076
4077 static int
listreviter_traverse(PyObject * it,visitproc visit,void * arg)4078 listreviter_traverse(PyObject *it, visitproc visit, void *arg)
4079 {
4080 Py_VISIT(((listreviterobject *)it)->it_seq);
4081 return 0;
4082 }
4083
4084 static PyObject *
listreviter_next(PyObject * self)4085 listreviter_next(PyObject *self)
4086 {
4087 listreviterobject *it = (listreviterobject *)self;
4088 assert(it != NULL);
4089 Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index);
4090 if (index < 0) {
4091 return NULL;
4092 }
4093
4094 PyListObject *seq = it->it_seq;
4095 assert(PyList_Check(seq));
4096 PyObject *item = list_get_item_ref(seq, index);
4097 if (item != NULL) {
4098 FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, index - 1);
4099 return item;
4100 }
4101 FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, -1);
4102 #ifndef Py_GIL_DISABLED
4103 it->it_seq = NULL;
4104 Py_DECREF(seq);
4105 #endif
4106 return NULL;
4107 }
4108
4109 static PyObject *
listreviter_len(PyObject * self,PyObject * Py_UNUSED (ignored))4110 listreviter_len(PyObject *self, PyObject *Py_UNUSED(ignored))
4111 {
4112 listreviterobject *it = (listreviterobject *)self;
4113 Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index);
4114 Py_ssize_t len = index + 1;
4115 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
4116 len = 0;
4117 return PyLong_FromSsize_t(len);
4118 }
4119
4120 static PyObject *
listreviter_reduce(PyObject * it,PyObject * Py_UNUSED (ignored))4121 listreviter_reduce(PyObject *it, PyObject *Py_UNUSED(ignored))
4122 {
4123 return listiter_reduce_general(it, 0);
4124 }
4125
4126 static PyObject *
listreviter_setstate(PyObject * self,PyObject * state)4127 listreviter_setstate(PyObject *self, PyObject *state)
4128 {
4129 listreviterobject *it = (listreviterobject *)self;
4130 Py_ssize_t index = PyLong_AsSsize_t(state);
4131 if (index == -1 && PyErr_Occurred())
4132 return NULL;
4133 if (it->it_seq != NULL) {
4134 if (index < -1)
4135 index = -1;
4136 else if (index > PyList_GET_SIZE(it->it_seq) - 1)
4137 index = PyList_GET_SIZE(it->it_seq) - 1;
4138 it->it_index = index;
4139 }
4140 Py_RETURN_NONE;
4141 }
4142
4143 /* common pickling support */
4144
4145 static PyObject *
listiter_reduce_general(void * _it,int forward)4146 listiter_reduce_general(void *_it, int forward)
4147 {
4148 PyObject *list;
4149 PyObject *iter;
4150
4151 /* _PyEval_GetBuiltin can invoke arbitrary code,
4152 * call must be before access of iterator pointers.
4153 * see issue #101765 */
4154
4155 /* the objects are not the same, index is of different types! */
4156 if (forward) {
4157 iter = _PyEval_GetBuiltin(&_Py_ID(iter));
4158 _PyListIterObject *it = (_PyListIterObject *)_it;
4159 if (it->it_index >= 0) {
4160 return Py_BuildValue("N(O)n", iter, it->it_seq, it->it_index);
4161 }
4162 } else {
4163 iter = _PyEval_GetBuiltin(&_Py_ID(reversed));
4164 listreviterobject *it = (listreviterobject *)_it;
4165 if (it->it_index >= 0) {
4166 return Py_BuildValue("N(O)n", iter, it->it_seq, it->it_index);
4167 }
4168 }
4169 /* empty iterator, create an empty list */
4170 list = PyList_New(0);
4171 if (list == NULL)
4172 return NULL;
4173 return Py_BuildValue("N(N)", iter, list);
4174 }
4175