1 /* List object implementation */
2 
3 #include "Python.h"
4 #include "pycore_object.h"
5 #include "pycore_pystate.h"
6 #include "pycore_tupleobject.h"
7 #include "pycore_accu.h"
8 
9 #ifdef STDC_HEADERS
10 #include <stddef.h>
11 #else
12 #include <sys/types.h>          /* For size_t */
13 #endif
14 
15 /*[clinic input]
16 class list "PyListObject *" "&PyList_Type"
17 [clinic start generated code]*/
18 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=f9b222678f9f71e0]*/
19 
20 #include "clinic/listobject.c.h"
21 
22 /* Ensure ob_item has room for at least newsize elements, and set
23  * ob_size to newsize.  If newsize > ob_size on entry, the content
24  * of the new slots at exit is undefined heap trash; it's the caller's
25  * responsibility to overwrite them with sane values.
26  * The number of allocated elements may grow, shrink, or stay the same.
27  * Failure is impossible if newsize <= self.allocated on entry, although
28  * that partly relies on an assumption that the system realloc() never
29  * fails when passed a number of bytes <= the number of bytes last
30  * allocated (the C standard doesn't guarantee this, but it's hard to
31  * imagine a realloc implementation where it wouldn't be true).
32  * Note that self->ob_item may change, and even if newsize is less
33  * than ob_size on entry.
34  */
35 static int
list_resize(PyListObject * self,Py_ssize_t newsize)36 list_resize(PyListObject *self, Py_ssize_t newsize)
37 {
38     PyObject **items;
39     size_t new_allocated, num_allocated_bytes;
40     Py_ssize_t allocated = self->allocated;
41 
42     /* Bypass realloc() when a previous overallocation is large enough
43        to accommodate the newsize.  If the newsize falls lower than half
44        the allocated size, then proceed with the realloc() to shrink the list.
45     */
46     if (allocated >= newsize && newsize >= (allocated >> 1)) {
47         assert(self->ob_item != NULL || newsize == 0);
48         Py_SIZE(self) = newsize;
49         return 0;
50     }
51 
52     /* This over-allocates proportional to the list size, making room
53      * for additional growth.  The over-allocation is mild, but is
54      * enough to give linear-time amortized behavior over a long
55      * sequence of appends() in the presence of a poorly-performing
56      * system realloc().
57      * The growth pattern is:  0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
58      * Note: new_allocated won't overflow because the largest possible value
59      *       is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
60      */
61     new_allocated = (size_t)newsize + (newsize >> 3) + (newsize < 9 ? 3 : 6);
62     if (new_allocated > (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
63         PyErr_NoMemory();
64         return -1;
65     }
66 
67     if (newsize == 0)
68         new_allocated = 0;
69     num_allocated_bytes = new_allocated * sizeof(PyObject *);
70     items = (PyObject **)PyMem_Realloc(self->ob_item, num_allocated_bytes);
71     if (items == NULL) {
72         PyErr_NoMemory();
73         return -1;
74     }
75     self->ob_item = items;
76     Py_SIZE(self) = newsize;
77     self->allocated = new_allocated;
78     return 0;
79 }
80 
81 static int
list_preallocate_exact(PyListObject * self,Py_ssize_t size)82 list_preallocate_exact(PyListObject *self, Py_ssize_t size)
83 {
84     assert(self->ob_item == NULL);
85     assert(size > 0);
86 
87     PyObject **items = PyMem_New(PyObject*, size);
88     if (items == NULL) {
89         PyErr_NoMemory();
90         return -1;
91     }
92     self->ob_item = items;
93     self->allocated = size;
94     return 0;
95 }
96 
97 /* Debug statistic to compare allocations with reuse through the free list */
98 #undef SHOW_ALLOC_COUNT
99 #ifdef SHOW_ALLOC_COUNT
100 static size_t count_alloc = 0;
101 static size_t count_reuse = 0;
102 
103 static void
show_alloc(void)104 show_alloc(void)
105 {
106     PyInterpreterState *interp = _PyInterpreterState_Get();
107     if (!interp->config.show_alloc_count) {
108         return;
109     }
110 
111     fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
112         count_alloc);
113     fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T
114         "d\n", count_reuse);
115     fprintf(stderr, "%.2f%% reuse rate\n\n",
116         (100.0*count_reuse/(count_alloc+count_reuse)));
117 }
118 #endif
119 
120 /* Empty list reuse scheme to save calls to malloc and free */
121 #ifndef PyList_MAXFREELIST
122 #define PyList_MAXFREELIST 80
123 #endif
124 static PyListObject *free_list[PyList_MAXFREELIST];
125 static int numfree = 0;
126 
127 int
PyList_ClearFreeList(void)128 PyList_ClearFreeList(void)
129 {
130     PyListObject *op;
131     int ret = numfree;
132     while (numfree) {
133         op = free_list[--numfree];
134         assert(PyList_CheckExact(op));
135         PyObject_GC_Del(op);
136     }
137     return ret;
138 }
139 
140 void
PyList_Fini(void)141 PyList_Fini(void)
142 {
143     PyList_ClearFreeList();
144 }
145 
146 /* Print summary info about the state of the optimized allocator */
147 void
_PyList_DebugMallocStats(FILE * out)148 _PyList_DebugMallocStats(FILE *out)
149 {
150     _PyDebugAllocatorStats(out,
151                            "free PyListObject",
152                            numfree, sizeof(PyListObject));
153 }
154 
155 PyObject *
PyList_New(Py_ssize_t size)156 PyList_New(Py_ssize_t size)
157 {
158     PyListObject *op;
159 #ifdef SHOW_ALLOC_COUNT
160     static int initialized = 0;
161     if (!initialized) {
162         Py_AtExit(show_alloc);
163         initialized = 1;
164     }
165 #endif
166 
167     if (size < 0) {
168         PyErr_BadInternalCall();
169         return NULL;
170     }
171     if (numfree) {
172         numfree--;
173         op = free_list[numfree];
174         _Py_NewReference((PyObject *)op);
175 #ifdef SHOW_ALLOC_COUNT
176         count_reuse++;
177 #endif
178     } else {
179         op = PyObject_GC_New(PyListObject, &PyList_Type);
180         if (op == NULL)
181             return NULL;
182 #ifdef SHOW_ALLOC_COUNT
183         count_alloc++;
184 #endif
185     }
186     if (size <= 0)
187         op->ob_item = NULL;
188     else {
189         op->ob_item = (PyObject **) PyMem_Calloc(size, sizeof(PyObject *));
190         if (op->ob_item == NULL) {
191             Py_DECREF(op);
192             return PyErr_NoMemory();
193         }
194     }
195     Py_SIZE(op) = size;
196     op->allocated = size;
197     _PyObject_GC_TRACK(op);
198     return (PyObject *) op;
199 }
200 
201 static PyObject *
list_new_prealloc(Py_ssize_t size)202 list_new_prealloc(Py_ssize_t size)
203 {
204     PyListObject *op = (PyListObject *) PyList_New(0);
205     if (size == 0 || op == NULL) {
206         return (PyObject *) op;
207     }
208     assert(op->ob_item == NULL);
209     op->ob_item = PyMem_New(PyObject *, size);
210     if (op->ob_item == NULL) {
211         Py_DECREF(op);
212         return PyErr_NoMemory();
213     }
214     op->allocated = size;
215     return (PyObject *) op;
216 }
217 
218 Py_ssize_t
PyList_Size(PyObject * op)219 PyList_Size(PyObject *op)
220 {
221     if (!PyList_Check(op)) {
222         PyErr_BadInternalCall();
223         return -1;
224     }
225     else
226         return Py_SIZE(op);
227 }
228 
229 static inline int
valid_index(Py_ssize_t i,Py_ssize_t limit)230 valid_index(Py_ssize_t i, Py_ssize_t limit)
231 {
232     /* The cast to size_t lets us use just a single comparison
233        to check whether i is in the range: 0 <= i < limit.
234 
235        See:  Section 14.2 "Bounds Checking" in the Agner Fog
236        optimization manual found at:
237        https://www.agner.org/optimize/optimizing_cpp.pdf
238     */
239     return (size_t) i < (size_t) limit;
240 }
241 
242 static PyObject *indexerr = NULL;
243 
244 PyObject *
PyList_GetItem(PyObject * op,Py_ssize_t i)245 PyList_GetItem(PyObject *op, Py_ssize_t i)
246 {
247     if (!PyList_Check(op)) {
248         PyErr_BadInternalCall();
249         return NULL;
250     }
251     if (!valid_index(i, Py_SIZE(op))) {
252         if (indexerr == NULL) {
253             indexerr = PyUnicode_FromString(
254                 "list index out of range");
255             if (indexerr == NULL)
256                 return NULL;
257         }
258         PyErr_SetObject(PyExc_IndexError, indexerr);
259         return NULL;
260     }
261     return ((PyListObject *)op) -> ob_item[i];
262 }
263 
264 int
PyList_SetItem(PyObject * op,Py_ssize_t i,PyObject * newitem)265 PyList_SetItem(PyObject *op, Py_ssize_t i,
266                PyObject *newitem)
267 {
268     PyObject **p;
269     if (!PyList_Check(op)) {
270         Py_XDECREF(newitem);
271         PyErr_BadInternalCall();
272         return -1;
273     }
274     if (!valid_index(i, Py_SIZE(op))) {
275         Py_XDECREF(newitem);
276         PyErr_SetString(PyExc_IndexError,
277                         "list assignment index out of range");
278         return -1;
279     }
280     p = ((PyListObject *)op) -> ob_item + i;
281     Py_XSETREF(*p, newitem);
282     return 0;
283 }
284 
285 static int
ins1(PyListObject * self,Py_ssize_t where,PyObject * v)286 ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
287 {
288     Py_ssize_t i, n = Py_SIZE(self);
289     PyObject **items;
290     if (v == NULL) {
291         PyErr_BadInternalCall();
292         return -1;
293     }
294     if (n == PY_SSIZE_T_MAX) {
295         PyErr_SetString(PyExc_OverflowError,
296             "cannot add more objects to list");
297         return -1;
298     }
299 
300     if (list_resize(self, n+1) < 0)
301         return -1;
302 
303     if (where < 0) {
304         where += n;
305         if (where < 0)
306             where = 0;
307     }
308     if (where > n)
309         where = n;
310     items = self->ob_item;
311     for (i = n; --i >= where; )
312         items[i+1] = items[i];
313     Py_INCREF(v);
314     items[where] = v;
315     return 0;
316 }
317 
318 int
PyList_Insert(PyObject * op,Py_ssize_t where,PyObject * newitem)319 PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
320 {
321     if (!PyList_Check(op)) {
322         PyErr_BadInternalCall();
323         return -1;
324     }
325     return ins1((PyListObject *)op, where, newitem);
326 }
327 
328 static int
app1(PyListObject * self,PyObject * v)329 app1(PyListObject *self, PyObject *v)
330 {
331     Py_ssize_t n = PyList_GET_SIZE(self);
332 
333     assert (v != NULL);
334     if (n == PY_SSIZE_T_MAX) {
335         PyErr_SetString(PyExc_OverflowError,
336             "cannot add more objects to list");
337         return -1;
338     }
339 
340     if (list_resize(self, n+1) < 0)
341         return -1;
342 
343     Py_INCREF(v);
344     PyList_SET_ITEM(self, n, v);
345     return 0;
346 }
347 
348 int
PyList_Append(PyObject * op,PyObject * newitem)349 PyList_Append(PyObject *op, PyObject *newitem)
350 {
351     if (PyList_Check(op) && (newitem != NULL))
352         return app1((PyListObject *)op, newitem);
353     PyErr_BadInternalCall();
354     return -1;
355 }
356 
357 /* Methods */
358 
359 static void
list_dealloc(PyListObject * op)360 list_dealloc(PyListObject *op)
361 {
362     Py_ssize_t i;
363     PyObject_GC_UnTrack(op);
364     Py_TRASHCAN_BEGIN(op, list_dealloc)
365     if (op->ob_item != NULL) {
366         /* Do it backwards, for Christian Tismer.
367            There's a simple test case where somehow this reduces
368            thrashing when a *very* large list is created and
369            immediately deleted. */
370         i = Py_SIZE(op);
371         while (--i >= 0) {
372             Py_XDECREF(op->ob_item[i]);
373         }
374         PyMem_FREE(op->ob_item);
375     }
376     if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
377         free_list[numfree++] = op;
378     else
379         Py_TYPE(op)->tp_free((PyObject *)op);
380     Py_TRASHCAN_END
381 }
382 
383 static PyObject *
list_repr(PyListObject * v)384 list_repr(PyListObject *v)
385 {
386     Py_ssize_t i;
387     PyObject *s;
388     _PyUnicodeWriter writer;
389 
390     if (Py_SIZE(v) == 0) {
391         return PyUnicode_FromString("[]");
392     }
393 
394     i = Py_ReprEnter((PyObject*)v);
395     if (i != 0) {
396         return i > 0 ? PyUnicode_FromString("[...]") : NULL;
397     }
398 
399     _PyUnicodeWriter_Init(&writer);
400     writer.overallocate = 1;
401     /* "[" + "1" + ", 2" * (len - 1) + "]" */
402     writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
403 
404     if (_PyUnicodeWriter_WriteChar(&writer, '[') < 0)
405         goto error;
406 
407     /* Do repr() on each element.  Note that this may mutate the list,
408        so must refetch the list size on each iteration. */
409     for (i = 0; i < Py_SIZE(v); ++i) {
410         if (i > 0) {
411             if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
412                 goto error;
413         }
414 
415         s = PyObject_Repr(v->ob_item[i]);
416         if (s == NULL)
417             goto error;
418 
419         if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
420             Py_DECREF(s);
421             goto error;
422         }
423         Py_DECREF(s);
424     }
425 
426     writer.overallocate = 0;
427     if (_PyUnicodeWriter_WriteChar(&writer, ']') < 0)
428         goto error;
429 
430     Py_ReprLeave((PyObject *)v);
431     return _PyUnicodeWriter_Finish(&writer);
432 
433 error:
434     _PyUnicodeWriter_Dealloc(&writer);
435     Py_ReprLeave((PyObject *)v);
436     return NULL;
437 }
438 
439 static Py_ssize_t
list_length(PyListObject * a)440 list_length(PyListObject *a)
441 {
442     return Py_SIZE(a);
443 }
444 
445 static int
list_contains(PyListObject * a,PyObject * el)446 list_contains(PyListObject *a, PyObject *el)
447 {
448     PyObject *item;
449     Py_ssize_t i;
450     int cmp;
451 
452     for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i) {
453         item = PyList_GET_ITEM(a, i);
454         Py_INCREF(item);
455         cmp = PyObject_RichCompareBool(el, item, Py_EQ);
456         Py_DECREF(item);
457     }
458     return cmp;
459 }
460 
461 static PyObject *
list_item(PyListObject * a,Py_ssize_t i)462 list_item(PyListObject *a, Py_ssize_t i)
463 {
464     if (!valid_index(i, Py_SIZE(a))) {
465         if (indexerr == NULL) {
466             indexerr = PyUnicode_FromString(
467                 "list index out of range");
468             if (indexerr == NULL)
469                 return NULL;
470         }
471         PyErr_SetObject(PyExc_IndexError, indexerr);
472         return NULL;
473     }
474     Py_INCREF(a->ob_item[i]);
475     return a->ob_item[i];
476 }
477 
478 static PyObject *
list_slice(PyListObject * a,Py_ssize_t ilow,Py_ssize_t ihigh)479 list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
480 {
481     PyListObject *np;
482     PyObject **src, **dest;
483     Py_ssize_t i, len;
484     len = ihigh - ilow;
485     np = (PyListObject *) list_new_prealloc(len);
486     if (np == NULL)
487         return NULL;
488 
489     src = a->ob_item + ilow;
490     dest = np->ob_item;
491     for (i = 0; i < len; i++) {
492         PyObject *v = src[i];
493         Py_INCREF(v);
494         dest[i] = v;
495     }
496     Py_SIZE(np) = len;
497     return (PyObject *)np;
498 }
499 
500 PyObject *
PyList_GetSlice(PyObject * a,Py_ssize_t ilow,Py_ssize_t ihigh)501 PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
502 {
503     if (!PyList_Check(a)) {
504         PyErr_BadInternalCall();
505         return NULL;
506     }
507     if (ilow < 0) {
508         ilow = 0;
509     }
510     else if (ilow > Py_SIZE(a)) {
511         ilow = Py_SIZE(a);
512     }
513     if (ihigh < ilow) {
514         ihigh = ilow;
515     }
516     else if (ihigh > Py_SIZE(a)) {
517         ihigh = Py_SIZE(a);
518     }
519     return list_slice((PyListObject *)a, ilow, ihigh);
520 }
521 
522 static PyObject *
list_concat(PyListObject * a,PyObject * bb)523 list_concat(PyListObject *a, PyObject *bb)
524 {
525     Py_ssize_t size;
526     Py_ssize_t i;
527     PyObject **src, **dest;
528     PyListObject *np;
529     if (!PyList_Check(bb)) {
530         PyErr_Format(PyExc_TypeError,
531                   "can only concatenate list (not \"%.200s\") to list",
532                   bb->ob_type->tp_name);
533         return NULL;
534     }
535 #define b ((PyListObject *)bb)
536     if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
537         return PyErr_NoMemory();
538     size = Py_SIZE(a) + Py_SIZE(b);
539     np = (PyListObject *) list_new_prealloc(size);
540     if (np == NULL) {
541         return NULL;
542     }
543     src = a->ob_item;
544     dest = np->ob_item;
545     for (i = 0; i < Py_SIZE(a); i++) {
546         PyObject *v = src[i];
547         Py_INCREF(v);
548         dest[i] = v;
549     }
550     src = b->ob_item;
551     dest = np->ob_item + Py_SIZE(a);
552     for (i = 0; i < Py_SIZE(b); i++) {
553         PyObject *v = src[i];
554         Py_INCREF(v);
555         dest[i] = v;
556     }
557     Py_SIZE(np) = size;
558     return (PyObject *)np;
559 #undef b
560 }
561 
562 static PyObject *
list_repeat(PyListObject * a,Py_ssize_t n)563 list_repeat(PyListObject *a, Py_ssize_t n)
564 {
565     Py_ssize_t i, j;
566     Py_ssize_t size;
567     PyListObject *np;
568     PyObject **p, **items;
569     PyObject *elem;
570     if (n < 0)
571         n = 0;
572     if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
573         return PyErr_NoMemory();
574     size = Py_SIZE(a) * n;
575     if (size == 0)
576         return PyList_New(0);
577     np = (PyListObject *) list_new_prealloc(size);
578     if (np == NULL)
579         return NULL;
580 
581     if (Py_SIZE(a) == 1) {
582         items = np->ob_item;
583         elem = a->ob_item[0];
584         for (i = 0; i < n; i++) {
585             items[i] = elem;
586             Py_INCREF(elem);
587         }
588     }
589     else {
590         p = np->ob_item;
591         items = a->ob_item;
592         for (i = 0; i < n; i++) {
593             for (j = 0; j < Py_SIZE(a); j++) {
594                 *p = items[j];
595                 Py_INCREF(*p);
596                 p++;
597             }
598         }
599     }
600     Py_SIZE(np) = size;
601     return (PyObject *) np;
602 }
603 
604 static int
_list_clear(PyListObject * a)605 _list_clear(PyListObject *a)
606 {
607     Py_ssize_t i;
608     PyObject **item = a->ob_item;
609     if (item != NULL) {
610         /* Because XDECREF can recursively invoke operations on
611            this list, we make it empty first. */
612         i = Py_SIZE(a);
613         Py_SIZE(a) = 0;
614         a->ob_item = NULL;
615         a->allocated = 0;
616         while (--i >= 0) {
617             Py_XDECREF(item[i]);
618         }
619         PyMem_FREE(item);
620     }
621     /* Never fails; the return value can be ignored.
622        Note that there is no guarantee that the list is actually empty
623        at this point, because XDECREF may have populated it again! */
624     return 0;
625 }
626 
627 /* a[ilow:ihigh] = v if v != NULL.
628  * del a[ilow:ihigh] if v == NULL.
629  *
630  * Special speed gimmick:  when v is NULL and ihigh - ilow <= 8, it's
631  * guaranteed the call cannot fail.
632  */
633 static int
list_ass_slice(PyListObject * a,Py_ssize_t ilow,Py_ssize_t ihigh,PyObject * v)634 list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
635 {
636     /* Because [X]DECREF can recursively invoke list operations on
637        this list, we must postpone all [X]DECREF activity until
638        after the list is back in its canonical shape.  Therefore
639        we must allocate an additional array, 'recycle', into which
640        we temporarily copy the items that are deleted from the
641        list. :-( */
642     PyObject *recycle_on_stack[8];
643     PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
644     PyObject **item;
645     PyObject **vitem = NULL;
646     PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
647     Py_ssize_t n; /* # of elements in replacement list */
648     Py_ssize_t norig; /* # of elements in list getting replaced */
649     Py_ssize_t d; /* Change in size */
650     Py_ssize_t k;
651     size_t s;
652     int result = -1;            /* guilty until proved innocent */
653 #define b ((PyListObject *)v)
654     if (v == NULL)
655         n = 0;
656     else {
657         if (a == b) {
658             /* Special case "a[i:j] = a" -- copy b first */
659             v = list_slice(b, 0, Py_SIZE(b));
660             if (v == NULL)
661                 return result;
662             result = list_ass_slice(a, ilow, ihigh, v);
663             Py_DECREF(v);
664             return result;
665         }
666         v_as_SF = PySequence_Fast(v, "can only assign an iterable");
667         if(v_as_SF == NULL)
668             goto Error;
669         n = PySequence_Fast_GET_SIZE(v_as_SF);
670         vitem = PySequence_Fast_ITEMS(v_as_SF);
671     }
672     if (ilow < 0)
673         ilow = 0;
674     else if (ilow > Py_SIZE(a))
675         ilow = Py_SIZE(a);
676 
677     if (ihigh < ilow)
678         ihigh = ilow;
679     else if (ihigh > Py_SIZE(a))
680         ihigh = Py_SIZE(a);
681 
682     norig = ihigh - ilow;
683     assert(norig >= 0);
684     d = n - norig;
685     if (Py_SIZE(a) + d == 0) {
686         Py_XDECREF(v_as_SF);
687         return _list_clear(a);
688     }
689     item = a->ob_item;
690     /* recycle the items that we are about to remove */
691     s = norig * sizeof(PyObject *);
692     /* If norig == 0, item might be NULL, in which case we may not memcpy from it. */
693     if (s) {
694         if (s > sizeof(recycle_on_stack)) {
695             recycle = (PyObject **)PyMem_MALLOC(s);
696             if (recycle == NULL) {
697                 PyErr_NoMemory();
698                 goto Error;
699             }
700         }
701         memcpy(recycle, &item[ilow], s);
702     }
703 
704     if (d < 0) { /* Delete -d items */
705         Py_ssize_t tail;
706         tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
707         memmove(&item[ihigh+d], &item[ihigh], tail);
708         if (list_resize(a, Py_SIZE(a) + d) < 0) {
709             memmove(&item[ihigh], &item[ihigh+d], tail);
710             memcpy(&item[ilow], recycle, s);
711             goto Error;
712         }
713         item = a->ob_item;
714     }
715     else if (d > 0) { /* Insert d items */
716         k = Py_SIZE(a);
717         if (list_resize(a, k+d) < 0)
718             goto Error;
719         item = a->ob_item;
720         memmove(&item[ihigh+d], &item[ihigh],
721             (k - ihigh)*sizeof(PyObject *));
722     }
723     for (k = 0; k < n; k++, ilow++) {
724         PyObject *w = vitem[k];
725         Py_XINCREF(w);
726         item[ilow] = w;
727     }
728     for (k = norig - 1; k >= 0; --k)
729         Py_XDECREF(recycle[k]);
730     result = 0;
731  Error:
732     if (recycle != recycle_on_stack)
733         PyMem_FREE(recycle);
734     Py_XDECREF(v_as_SF);
735     return result;
736 #undef b
737 }
738 
739 int
PyList_SetSlice(PyObject * a,Py_ssize_t ilow,Py_ssize_t ihigh,PyObject * v)740 PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
741 {
742     if (!PyList_Check(a)) {
743         PyErr_BadInternalCall();
744         return -1;
745     }
746     return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
747 }
748 
749 static PyObject *
list_inplace_repeat(PyListObject * self,Py_ssize_t n)750 list_inplace_repeat(PyListObject *self, Py_ssize_t n)
751 {
752     PyObject **items;
753     Py_ssize_t size, i, j, p;
754 
755 
756     size = PyList_GET_SIZE(self);
757     if (size == 0 || n == 1) {
758         Py_INCREF(self);
759         return (PyObject *)self;
760     }
761 
762     if (n < 1) {
763         (void)_list_clear(self);
764         Py_INCREF(self);
765         return (PyObject *)self;
766     }
767 
768     if (size > PY_SSIZE_T_MAX / n) {
769         return PyErr_NoMemory();
770     }
771 
772     if (list_resize(self, size*n) < 0)
773         return NULL;
774 
775     p = size;
776     items = self->ob_item;
777     for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
778         for (j = 0; j < size; j++) {
779             PyObject *o = items[j];
780             Py_INCREF(o);
781             items[p++] = o;
782         }
783     }
784     Py_INCREF(self);
785     return (PyObject *)self;
786 }
787 
788 static int
list_ass_item(PyListObject * a,Py_ssize_t i,PyObject * v)789 list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
790 {
791     if (!valid_index(i, Py_SIZE(a))) {
792         PyErr_SetString(PyExc_IndexError,
793                         "list assignment index out of range");
794         return -1;
795     }
796     if (v == NULL)
797         return list_ass_slice(a, i, i+1, v);
798     Py_INCREF(v);
799     Py_SETREF(a->ob_item[i], v);
800     return 0;
801 }
802 
803 /*[clinic input]
804 list.insert
805 
806     index: Py_ssize_t
807     object: object
808     /
809 
810 Insert object before index.
811 [clinic start generated code]*/
812 
813 static PyObject *
list_insert_impl(PyListObject * self,Py_ssize_t index,PyObject * object)814 list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object)
815 /*[clinic end generated code: output=7f35e32f60c8cb78 input=858514cf894c7eab]*/
816 {
817     if (ins1(self, index, object) == 0)
818         Py_RETURN_NONE;
819     return NULL;
820 }
821 
822 /*[clinic input]
823 list.clear
824 
825 Remove all items from list.
826 [clinic start generated code]*/
827 
828 static PyObject *
list_clear_impl(PyListObject * self)829 list_clear_impl(PyListObject *self)
830 /*[clinic end generated code: output=67a1896c01f74362 input=ca3c1646856742f6]*/
831 {
832     _list_clear(self);
833     Py_RETURN_NONE;
834 }
835 
836 /*[clinic input]
837 list.copy
838 
839 Return a shallow copy of the list.
840 [clinic start generated code]*/
841 
842 static PyObject *
list_copy_impl(PyListObject * self)843 list_copy_impl(PyListObject *self)
844 /*[clinic end generated code: output=ec6b72d6209d418e input=6453ab159e84771f]*/
845 {
846     return list_slice(self, 0, Py_SIZE(self));
847 }
848 
849 /*[clinic input]
850 list.append
851 
852      object: object
853      /
854 
855 Append object to the end of the list.
856 [clinic start generated code]*/
857 
858 static PyObject *
list_append(PyListObject * self,PyObject * object)859 list_append(PyListObject *self, PyObject *object)
860 /*[clinic end generated code: output=7c096003a29c0eae input=43a3fe48a7066e91]*/
861 {
862     if (app1(self, object) == 0)
863         Py_RETURN_NONE;
864     return NULL;
865 }
866 
867 /*[clinic input]
868 list.extend
869 
870      iterable: object
871      /
872 
873 Extend list by appending elements from the iterable.
874 [clinic start generated code]*/
875 
876 static PyObject *
list_extend(PyListObject * self,PyObject * iterable)877 list_extend(PyListObject *self, PyObject *iterable)
878 /*[clinic end generated code: output=630fb3bca0c8e789 input=9ec5ba3a81be3a4d]*/
879 {
880     PyObject *it;      /* iter(v) */
881     Py_ssize_t m;                  /* size of self */
882     Py_ssize_t n;                  /* guess for size of iterable */
883     Py_ssize_t mn;                 /* m + n */
884     Py_ssize_t i;
885     PyObject *(*iternext)(PyObject *);
886 
887     /* Special cases:
888        1) lists and tuples which can use PySequence_Fast ops
889        2) extending self to self requires making a copy first
890     */
891     if (PyList_CheckExact(iterable) || PyTuple_CheckExact(iterable) ||
892                 (PyObject *)self == iterable) {
893         PyObject **src, **dest;
894         iterable = PySequence_Fast(iterable, "argument must be iterable");
895         if (!iterable)
896             return NULL;
897         n = PySequence_Fast_GET_SIZE(iterable);
898         if (n == 0) {
899             /* short circuit when iterable is empty */
900             Py_DECREF(iterable);
901             Py_RETURN_NONE;
902         }
903         m = Py_SIZE(self);
904         /* It should not be possible to allocate a list large enough to cause
905         an overflow on any relevant platform */
906         assert(m < PY_SSIZE_T_MAX - n);
907         if (list_resize(self, m + n) < 0) {
908             Py_DECREF(iterable);
909             return NULL;
910         }
911         /* note that we may still have self == iterable here for the
912          * situation a.extend(a), but the following code works
913          * in that case too.  Just make sure to resize self
914          * before calling PySequence_Fast_ITEMS.
915          */
916         /* populate the end of self with iterable's items */
917         src = PySequence_Fast_ITEMS(iterable);
918         dest = self->ob_item + m;
919         for (i = 0; i < n; i++) {
920             PyObject *o = src[i];
921             Py_INCREF(o);
922             dest[i] = o;
923         }
924         Py_DECREF(iterable);
925         Py_RETURN_NONE;
926     }
927 
928     it = PyObject_GetIter(iterable);
929     if (it == NULL)
930         return NULL;
931     iternext = *it->ob_type->tp_iternext;
932 
933     /* Guess a result list size. */
934     n = PyObject_LengthHint(iterable, 8);
935     if (n < 0) {
936         Py_DECREF(it);
937         return NULL;
938     }
939     m = Py_SIZE(self);
940     if (m > PY_SSIZE_T_MAX - n) {
941         /* m + n overflowed; on the chance that n lied, and there really
942          * is enough room, ignore it.  If n was telling the truth, we'll
943          * eventually run out of memory during the loop.
944          */
945     }
946     else {
947         mn = m + n;
948         /* Make room. */
949         if (list_resize(self, mn) < 0)
950             goto error;
951         /* Make the list sane again. */
952         Py_SIZE(self) = m;
953     }
954 
955     /* Run iterator to exhaustion. */
956     for (;;) {
957         PyObject *item = iternext(it);
958         if (item == NULL) {
959             if (PyErr_Occurred()) {
960                 if (PyErr_ExceptionMatches(PyExc_StopIteration))
961                     PyErr_Clear();
962                 else
963                     goto error;
964             }
965             break;
966         }
967         if (Py_SIZE(self) < self->allocated) {
968             /* steals ref */
969             PyList_SET_ITEM(self, Py_SIZE(self), item);
970             ++Py_SIZE(self);
971         }
972         else {
973             int status = app1(self, item);
974             Py_DECREF(item);  /* append creates a new ref */
975             if (status < 0)
976                 goto error;
977         }
978     }
979 
980     /* Cut back result list if initial guess was too large. */
981     if (Py_SIZE(self) < self->allocated) {
982         if (list_resize(self, Py_SIZE(self)) < 0)
983             goto error;
984     }
985 
986     Py_DECREF(it);
987     Py_RETURN_NONE;
988 
989   error:
990     Py_DECREF(it);
991     return NULL;
992 }
993 
994 PyObject *
_PyList_Extend(PyListObject * self,PyObject * iterable)995 _PyList_Extend(PyListObject *self, PyObject *iterable)
996 {
997     return list_extend(self, iterable);
998 }
999 
1000 static PyObject *
list_inplace_concat(PyListObject * self,PyObject * other)1001 list_inplace_concat(PyListObject *self, PyObject *other)
1002 {
1003     PyObject *result;
1004 
1005     result = list_extend(self, other);
1006     if (result == NULL)
1007         return result;
1008     Py_DECREF(result);
1009     Py_INCREF(self);
1010     return (PyObject *)self;
1011 }
1012 
1013 /*[clinic input]
1014 list.pop
1015 
1016     index: Py_ssize_t = -1
1017     /
1018 
1019 Remove and return item at index (default last).
1020 
1021 Raises IndexError if list is empty or index is out of range.
1022 [clinic start generated code]*/
1023 
1024 static PyObject *
list_pop_impl(PyListObject * self,Py_ssize_t index)1025 list_pop_impl(PyListObject *self, Py_ssize_t index)
1026 /*[clinic end generated code: output=6bd69dcb3f17eca8 input=b83675976f329e6f]*/
1027 {
1028     PyObject *v;
1029     int status;
1030 
1031     if (Py_SIZE(self) == 0) {
1032         /* Special-case most common failure cause */
1033         PyErr_SetString(PyExc_IndexError, "pop from empty list");
1034         return NULL;
1035     }
1036     if (index < 0)
1037         index += Py_SIZE(self);
1038     if (!valid_index(index, Py_SIZE(self))) {
1039         PyErr_SetString(PyExc_IndexError, "pop index out of range");
1040         return NULL;
1041     }
1042     v = self->ob_item[index];
1043     if (index == Py_SIZE(self) - 1) {
1044         status = list_resize(self, Py_SIZE(self) - 1);
1045         if (status >= 0)
1046             return v; /* and v now owns the reference the list had */
1047         else
1048             return NULL;
1049     }
1050     Py_INCREF(v);
1051     status = list_ass_slice(self, index, index+1, (PyObject *)NULL);
1052     if (status < 0) {
1053         Py_DECREF(v);
1054         return NULL;
1055     }
1056     return v;
1057 }
1058 
1059 /* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
1060 static void
reverse_slice(PyObject ** lo,PyObject ** hi)1061 reverse_slice(PyObject **lo, PyObject **hi)
1062 {
1063     assert(lo && hi);
1064 
1065     --hi;
1066     while (lo < hi) {
1067         PyObject *t = *lo;
1068         *lo = *hi;
1069         *hi = t;
1070         ++lo;
1071         --hi;
1072     }
1073 }
1074 
1075 /* Lots of code for an adaptive, stable, natural mergesort.  There are many
1076  * pieces to this algorithm; read listsort.txt for overviews and details.
1077  */
1078 
1079 /* A sortslice contains a pointer to an array of keys and a pointer to
1080  * an array of corresponding values.  In other words, keys[i]
1081  * corresponds with values[i].  If values == NULL, then the keys are
1082  * also the values.
1083  *
1084  * Several convenience routines are provided here, so that keys and
1085  * values are always moved in sync.
1086  */
1087 
1088 typedef struct {
1089     PyObject **keys;
1090     PyObject **values;
1091 } sortslice;
1092 
1093 Py_LOCAL_INLINE(void)
sortslice_copy(sortslice * s1,Py_ssize_t i,sortslice * s2,Py_ssize_t j)1094 sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
1095 {
1096     s1->keys[i] = s2->keys[j];
1097     if (s1->values != NULL)
1098         s1->values[i] = s2->values[j];
1099 }
1100 
1101 Py_LOCAL_INLINE(void)
sortslice_copy_incr(sortslice * dst,sortslice * src)1102 sortslice_copy_incr(sortslice *dst, sortslice *src)
1103 {
1104     *dst->keys++ = *src->keys++;
1105     if (dst->values != NULL)
1106         *dst->values++ = *src->values++;
1107 }
1108 
1109 Py_LOCAL_INLINE(void)
sortslice_copy_decr(sortslice * dst,sortslice * src)1110 sortslice_copy_decr(sortslice *dst, sortslice *src)
1111 {
1112     *dst->keys-- = *src->keys--;
1113     if (dst->values != NULL)
1114         *dst->values-- = *src->values--;
1115 }
1116 
1117 
1118 Py_LOCAL_INLINE(void)
sortslice_memcpy(sortslice * s1,Py_ssize_t i,sortslice * s2,Py_ssize_t j,Py_ssize_t n)1119 sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
1120                  Py_ssize_t n)
1121 {
1122     memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1123     if (s1->values != NULL)
1124         memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1125 }
1126 
1127 Py_LOCAL_INLINE(void)
sortslice_memmove(sortslice * s1,Py_ssize_t i,sortslice * s2,Py_ssize_t j,Py_ssize_t n)1128 sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
1129                   Py_ssize_t n)
1130 {
1131     memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1132     if (s1->values != NULL)
1133         memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1134 }
1135 
1136 Py_LOCAL_INLINE(void)
sortslice_advance(sortslice * slice,Py_ssize_t n)1137 sortslice_advance(sortslice *slice, Py_ssize_t n)
1138 {
1139     slice->keys += n;
1140     if (slice->values != NULL)
1141         slice->values += n;
1142 }
1143 
1144 /* Comparison function: ms->key_compare, which is set at run-time in
1145  * listsort_impl to optimize for various special cases.
1146  * Returns -1 on error, 1 if x < y, 0 if x >= y.
1147  */
1148 
1149 #define ISLT(X, Y) (*(ms->key_compare))(X, Y, ms)
1150 
1151 /* Compare X to Y via "<".  Goto "fail" if the comparison raises an
1152    error.  Else "k" is set to true iff X<Y, and an "if (k)" block is
1153    started.  It makes more sense in context <wink>.  X and Y are PyObject*s.
1154 */
1155 #define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail;  \
1156            if (k)
1157 
1158 /* The maximum number of entries in a MergeState's pending-runs stack.
1159  * This is enough to sort arrays of size up to about
1160  *     32 * phi ** MAX_MERGE_PENDING
1161  * where phi ~= 1.618.  85 is ridiculouslylarge enough, good for an array
1162  * with 2**64 elements.
1163  */
1164 #define MAX_MERGE_PENDING 85
1165 
1166 /* When we get into galloping mode, we stay there until both runs win less
1167  * often than MIN_GALLOP consecutive times.  See listsort.txt for more info.
1168  */
1169 #define MIN_GALLOP 7
1170 
1171 /* Avoid malloc for small temp arrays. */
1172 #define MERGESTATE_TEMP_SIZE 256
1173 
1174 /* One MergeState exists on the stack per invocation of mergesort.  It's just
1175  * a convenient way to pass state around among the helper functions.
1176  */
1177 struct s_slice {
1178     sortslice base;
1179     Py_ssize_t len;
1180 };
1181 
1182 typedef struct s_MergeState MergeState;
1183 struct s_MergeState {
1184     /* This controls when we get *into* galloping mode.  It's initialized
1185      * to MIN_GALLOP.  merge_lo and merge_hi tend to nudge it higher for
1186      * random data, and lower for highly structured data.
1187      */
1188     Py_ssize_t min_gallop;
1189 
1190     /* 'a' is temp storage to help with merges.  It contains room for
1191      * alloced entries.
1192      */
1193     sortslice a;        /* may point to temparray below */
1194     Py_ssize_t alloced;
1195 
1196     /* A stack of n pending runs yet to be merged.  Run #i starts at
1197      * address base[i] and extends for len[i] elements.  It's always
1198      * true (so long as the indices are in bounds) that
1199      *
1200      *     pending[i].base + pending[i].len == pending[i+1].base
1201      *
1202      * so we could cut the storage for this, but it's a minor amount,
1203      * and keeping all the info explicit simplifies the code.
1204      */
1205     int n;
1206     struct s_slice pending[MAX_MERGE_PENDING];
1207 
1208     /* 'a' points to this when possible, rather than muck with malloc. */
1209     PyObject *temparray[MERGESTATE_TEMP_SIZE];
1210 
1211     /* This is the function we will use to compare two keys,
1212      * even when none of our special cases apply and we have to use
1213      * safe_object_compare. */
1214     int (*key_compare)(PyObject *, PyObject *, MergeState *);
1215 
1216     /* This function is used by unsafe_object_compare to optimize comparisons
1217      * when we know our list is type-homogeneous but we can't assume anything else.
1218      * In the pre-sort check it is set equal to key->ob_type->tp_richcompare */
1219     PyObject *(*key_richcompare)(PyObject *, PyObject *, int);
1220 
1221     /* This function is used by unsafe_tuple_compare to compare the first elements
1222      * of tuples. It may be set to safe_object_compare, but the idea is that hopefully
1223      * we can assume more, and use one of the special-case compares. */
1224     int (*tuple_elem_compare)(PyObject *, PyObject *, MergeState *);
1225 };
1226 
1227 /* binarysort is the best method for sorting small arrays: it does
1228    few compares, but can do data movement quadratic in the number of
1229    elements.
1230    [lo, hi) is a contiguous slice of a list, and is sorted via
1231    binary insertion.  This sort is stable.
1232    On entry, must have lo <= start <= hi, and that [lo, start) is already
1233    sorted (pass start == lo if you don't know!).
1234    If islt() complains return -1, else 0.
1235    Even in case of error, the output slice will be some permutation of
1236    the input (nothing is lost or duplicated).
1237 */
1238 static int
binarysort(MergeState * ms,sortslice lo,PyObject ** hi,PyObject ** start)1239 binarysort(MergeState *ms, sortslice lo, PyObject **hi, PyObject **start)
1240 {
1241     Py_ssize_t k;
1242     PyObject **l, **p, **r;
1243     PyObject *pivot;
1244 
1245     assert(lo.keys <= start && start <= hi);
1246     /* assert [lo, start) is sorted */
1247     if (lo.keys == start)
1248         ++start;
1249     for (; start < hi; ++start) {
1250         /* set l to where *start belongs */
1251         l = lo.keys;
1252         r = start;
1253         pivot = *r;
1254         /* Invariants:
1255          * pivot >= all in [lo, l).
1256          * pivot  < all in [r, start).
1257          * The second is vacuously true at the start.
1258          */
1259         assert(l < r);
1260         do {
1261             p = l + ((r - l) >> 1);
1262             IFLT(pivot, *p)
1263                 r = p;
1264             else
1265                 l = p+1;
1266         } while (l < r);
1267         assert(l == r);
1268         /* The invariants still hold, so pivot >= all in [lo, l) and
1269            pivot < all in [l, start), so pivot belongs at l.  Note
1270            that if there are elements equal to pivot, l points to the
1271            first slot after them -- that's why this sort is stable.
1272            Slide over to make room.
1273            Caution: using memmove is much slower under MSVC 5;
1274            we're not usually moving many slots. */
1275         for (p = start; p > l; --p)
1276             *p = *(p-1);
1277         *l = pivot;
1278         if (lo.values != NULL) {
1279             Py_ssize_t offset = lo.values - lo.keys;
1280             p = start + offset;
1281             pivot = *p;
1282             l += offset;
1283             for (p = start + offset; p > l; --p)
1284                 *p = *(p-1);
1285             *l = pivot;
1286         }
1287     }
1288     return 0;
1289 
1290  fail:
1291     return -1;
1292 }
1293 
1294 /*
1295 Return the length of the run beginning at lo, in the slice [lo, hi).  lo < hi
1296 is required on entry.  "A run" is the longest ascending sequence, with
1297 
1298     lo[0] <= lo[1] <= lo[2] <= ...
1299 
1300 or the longest descending sequence, with
1301 
1302     lo[0] > lo[1] > lo[2] > ...
1303 
1304 Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1305 For its intended use in a stable mergesort, the strictness of the defn of
1306 "descending" is needed so that the caller can safely reverse a descending
1307 sequence without violating stability (strict > ensures there are no equal
1308 elements to get out of order).
1309 
1310 Returns -1 in case of error.
1311 */
1312 static Py_ssize_t
count_run(MergeState * ms,PyObject ** lo,PyObject ** hi,int * descending)1313 count_run(MergeState *ms, PyObject **lo, PyObject **hi, int *descending)
1314 {
1315     Py_ssize_t k;
1316     Py_ssize_t n;
1317 
1318     assert(lo < hi);
1319     *descending = 0;
1320     ++lo;
1321     if (lo == hi)
1322         return 1;
1323 
1324     n = 2;
1325     IFLT(*lo, *(lo-1)) {
1326         *descending = 1;
1327         for (lo = lo+1; lo < hi; ++lo, ++n) {
1328             IFLT(*lo, *(lo-1))
1329                 ;
1330             else
1331                 break;
1332         }
1333     }
1334     else {
1335         for (lo = lo+1; lo < hi; ++lo, ++n) {
1336             IFLT(*lo, *(lo-1))
1337                 break;
1338         }
1339     }
1340 
1341     return n;
1342 fail:
1343     return -1;
1344 }
1345 
1346 /*
1347 Locate the proper position of key in a sorted vector; if the vector contains
1348 an element equal to key, return the position immediately to the left of
1349 the leftmost equal element.  [gallop_right() does the same except returns
1350 the position to the right of the rightmost equal element (if any).]
1351 
1352 "a" is a sorted vector with n elements, starting at a[0].  n must be > 0.
1353 
1354 "hint" is an index at which to begin the search, 0 <= hint < n.  The closer
1355 hint is to the final result, the faster this runs.
1356 
1357 The return value is the int k in 0..n such that
1358 
1359     a[k-1] < key <= a[k]
1360 
1361 pretending that *(a-1) is minus infinity and a[n] is plus infinity.  IOW,
1362 key belongs at index k; or, IOW, the first k elements of a should precede
1363 key, and the last n-k should follow key.
1364 
1365 Returns -1 on error.  See listsort.txt for info on the method.
1366 */
1367 static Py_ssize_t
gallop_left(MergeState * ms,PyObject * key,PyObject ** a,Py_ssize_t n,Py_ssize_t hint)1368 gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
1369 {
1370     Py_ssize_t ofs;
1371     Py_ssize_t lastofs;
1372     Py_ssize_t k;
1373 
1374     assert(key && a && n > 0 && hint >= 0 && hint < n);
1375 
1376     a += hint;
1377     lastofs = 0;
1378     ofs = 1;
1379     IFLT(*a, key) {
1380         /* a[hint] < key -- gallop right, until
1381          * a[hint + lastofs] < key <= a[hint + ofs]
1382          */
1383         const Py_ssize_t maxofs = n - hint;             /* &a[n-1] is highest */
1384         while (ofs < maxofs) {
1385             IFLT(a[ofs], key) {
1386                 lastofs = ofs;
1387                 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
1388                 ofs = (ofs << 1) + 1;
1389             }
1390             else                /* key <= a[hint + ofs] */
1391                 break;
1392         }
1393         if (ofs > maxofs)
1394             ofs = maxofs;
1395         /* Translate back to offsets relative to &a[0]. */
1396         lastofs += hint;
1397         ofs += hint;
1398     }
1399     else {
1400         /* key <= a[hint] -- gallop left, until
1401          * a[hint - ofs] < key <= a[hint - lastofs]
1402          */
1403         const Py_ssize_t maxofs = hint + 1;             /* &a[0] is lowest */
1404         while (ofs < maxofs) {
1405             IFLT(*(a-ofs), key)
1406                 break;
1407             /* key <= a[hint - ofs] */
1408             lastofs = ofs;
1409             assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
1410             ofs = (ofs << 1) + 1;
1411         }
1412         if (ofs > maxofs)
1413             ofs = maxofs;
1414         /* Translate back to positive offsets relative to &a[0]. */
1415         k = lastofs;
1416         lastofs = hint - ofs;
1417         ofs = hint - k;
1418     }
1419     a -= hint;
1420 
1421     assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1422     /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1423      * right of lastofs but no farther right than ofs.  Do a binary
1424      * search, with invariant a[lastofs-1] < key <= a[ofs].
1425      */
1426     ++lastofs;
1427     while (lastofs < ofs) {
1428         Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
1429 
1430         IFLT(a[m], key)
1431             lastofs = m+1;              /* a[m] < key */
1432         else
1433             ofs = m;                    /* key <= a[m] */
1434     }
1435     assert(lastofs == ofs);             /* so a[ofs-1] < key <= a[ofs] */
1436     return ofs;
1437 
1438 fail:
1439     return -1;
1440 }
1441 
1442 /*
1443 Exactly like gallop_left(), except that if key already exists in a[0:n],
1444 finds the position immediately to the right of the rightmost equal value.
1445 
1446 The return value is the int k in 0..n such that
1447 
1448     a[k-1] <= key < a[k]
1449 
1450 or -1 if error.
1451 
1452 The code duplication is massive, but this is enough different given that
1453 we're sticking to "<" comparisons that it's much harder to follow if
1454 written as one routine with yet another "left or right?" flag.
1455 */
1456 static Py_ssize_t
gallop_right(MergeState * ms,PyObject * key,PyObject ** a,Py_ssize_t n,Py_ssize_t hint)1457 gallop_right(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
1458 {
1459     Py_ssize_t ofs;
1460     Py_ssize_t lastofs;
1461     Py_ssize_t k;
1462 
1463     assert(key && a && n > 0 && hint >= 0 && hint < n);
1464 
1465     a += hint;
1466     lastofs = 0;
1467     ofs = 1;
1468     IFLT(key, *a) {
1469         /* key < a[hint] -- gallop left, until
1470          * a[hint - ofs] <= key < a[hint - lastofs]
1471          */
1472         const Py_ssize_t maxofs = hint + 1;             /* &a[0] is lowest */
1473         while (ofs < maxofs) {
1474             IFLT(key, *(a-ofs)) {
1475                 lastofs = ofs;
1476                 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
1477                 ofs = (ofs << 1) + 1;
1478             }
1479             else                /* a[hint - ofs] <= key */
1480                 break;
1481         }
1482         if (ofs > maxofs)
1483             ofs = maxofs;
1484         /* Translate back to positive offsets relative to &a[0]. */
1485         k = lastofs;
1486         lastofs = hint - ofs;
1487         ofs = hint - k;
1488     }
1489     else {
1490         /* a[hint] <= key -- gallop right, until
1491          * a[hint + lastofs] <= key < a[hint + ofs]
1492         */
1493         const Py_ssize_t maxofs = n - hint;             /* &a[n-1] is highest */
1494         while (ofs < maxofs) {
1495             IFLT(key, a[ofs])
1496                 break;
1497             /* a[hint + ofs] <= key */
1498             lastofs = ofs;
1499             assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
1500             ofs = (ofs << 1) + 1;
1501         }
1502         if (ofs > maxofs)
1503             ofs = maxofs;
1504         /* Translate back to offsets relative to &a[0]. */
1505         lastofs += hint;
1506         ofs += hint;
1507     }
1508     a -= hint;
1509 
1510     assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1511     /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1512      * right of lastofs but no farther right than ofs.  Do a binary
1513      * search, with invariant a[lastofs-1] <= key < a[ofs].
1514      */
1515     ++lastofs;
1516     while (lastofs < ofs) {
1517         Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
1518 
1519         IFLT(key, a[m])
1520             ofs = m;                    /* key < a[m] */
1521         else
1522             lastofs = m+1;              /* a[m] <= key */
1523     }
1524     assert(lastofs == ofs);             /* so a[ofs-1] <= key < a[ofs] */
1525     return ofs;
1526 
1527 fail:
1528     return -1;
1529 }
1530 
1531 /* Conceptually a MergeState's constructor. */
1532 static void
merge_init(MergeState * ms,Py_ssize_t list_size,int has_keyfunc)1533 merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
1534 {
1535     assert(ms != NULL);
1536     if (has_keyfunc) {
1537         /* The temporary space for merging will need at most half the list
1538          * size rounded up.  Use the minimum possible space so we can use the
1539          * rest of temparray for other things.  In particular, if there is
1540          * enough extra space, listsort() will use it to store the keys.
1541          */
1542         ms->alloced = (list_size + 1) / 2;
1543 
1544         /* ms->alloced describes how many keys will be stored at
1545            ms->temparray, but we also need to store the values.  Hence,
1546            ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1547         if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1548             ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1549         ms->a.values = &ms->temparray[ms->alloced];
1550     }
1551     else {
1552         ms->alloced = MERGESTATE_TEMP_SIZE;
1553         ms->a.values = NULL;
1554     }
1555     ms->a.keys = ms->temparray;
1556     ms->n = 0;
1557     ms->min_gallop = MIN_GALLOP;
1558 }
1559 
1560 /* Free all the temp memory owned by the MergeState.  This must be called
1561  * when you're done with a MergeState, and may be called before then if
1562  * you want to free the temp memory early.
1563  */
1564 static void
merge_freemem(MergeState * ms)1565 merge_freemem(MergeState *ms)
1566 {
1567     assert(ms != NULL);
1568     if (ms->a.keys != ms->temparray)
1569         PyMem_Free(ms->a.keys);
1570 }
1571 
1572 /* Ensure enough temp memory for 'need' array slots is available.
1573  * Returns 0 on success and -1 if the memory can't be gotten.
1574  */
1575 static int
merge_getmem(MergeState * ms,Py_ssize_t need)1576 merge_getmem(MergeState *ms, Py_ssize_t need)
1577 {
1578     int multiplier;
1579 
1580     assert(ms != NULL);
1581     if (need <= ms->alloced)
1582         return 0;
1583 
1584     multiplier = ms->a.values != NULL ? 2 : 1;
1585 
1586     /* Don't realloc!  That can cost cycles to copy the old data, but
1587      * we don't care what's in the block.
1588      */
1589     merge_freemem(ms);
1590     if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject *) / multiplier) {
1591         PyErr_NoMemory();
1592         return -1;
1593     }
1594     ms->a.keys = (PyObject **)PyMem_Malloc(multiplier * need
1595                                           * sizeof(PyObject *));
1596     if (ms->a.keys != NULL) {
1597         ms->alloced = need;
1598         if (ms->a.values != NULL)
1599             ms->a.values = &ms->a.keys[need];
1600         return 0;
1601     }
1602     PyErr_NoMemory();
1603     return -1;
1604 }
1605 #define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 :   \
1606                                 merge_getmem(MS, NEED))
1607 
1608 /* Merge the na elements starting at ssa with the nb elements starting at
1609  * ssb.keys = ssa.keys + na in a stable way, in-place.  na and nb must be > 0.
1610  * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1611  * should have na <= nb.  See listsort.txt for more info.  Return 0 if
1612  * successful, -1 if error.
1613  */
1614 static Py_ssize_t
merge_lo(MergeState * ms,sortslice ssa,Py_ssize_t na,sortslice ssb,Py_ssize_t nb)1615 merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1616          sortslice ssb, Py_ssize_t nb)
1617 {
1618     Py_ssize_t k;
1619     sortslice dest;
1620     int result = -1;            /* guilty until proved innocent */
1621     Py_ssize_t min_gallop;
1622 
1623     assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1624     assert(ssa.keys + na == ssb.keys);
1625     if (MERGE_GETMEM(ms, na) < 0)
1626         return -1;
1627     sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1628     dest = ssa;
1629     ssa = ms->a;
1630 
1631     sortslice_copy_incr(&dest, &ssb);
1632     --nb;
1633     if (nb == 0)
1634         goto Succeed;
1635     if (na == 1)
1636         goto CopyB;
1637 
1638     min_gallop = ms->min_gallop;
1639     for (;;) {
1640         Py_ssize_t acount = 0;          /* # of times A won in a row */
1641         Py_ssize_t bcount = 0;          /* # of times B won in a row */
1642 
1643         /* Do the straightforward thing until (if ever) one run
1644          * appears to win consistently.
1645          */
1646         for (;;) {
1647             assert(na > 1 && nb > 0);
1648             k = ISLT(ssb.keys[0], ssa.keys[0]);
1649             if (k) {
1650                 if (k < 0)
1651                     goto Fail;
1652                 sortslice_copy_incr(&dest, &ssb);
1653                 ++bcount;
1654                 acount = 0;
1655                 --nb;
1656                 if (nb == 0)
1657                     goto Succeed;
1658                 if (bcount >= min_gallop)
1659                     break;
1660             }
1661             else {
1662                 sortslice_copy_incr(&dest, &ssa);
1663                 ++acount;
1664                 bcount = 0;
1665                 --na;
1666                 if (na == 1)
1667                     goto CopyB;
1668                 if (acount >= min_gallop)
1669                     break;
1670             }
1671         }
1672 
1673         /* One run is winning so consistently that galloping may
1674          * be a huge win.  So try that, and continue galloping until
1675          * (if ever) neither run appears to be winning consistently
1676          * anymore.
1677          */
1678         ++min_gallop;
1679         do {
1680             assert(na > 1 && nb > 0);
1681             min_gallop -= min_gallop > 1;
1682             ms->min_gallop = min_gallop;
1683             k = gallop_right(ms, ssb.keys[0], ssa.keys, na, 0);
1684             acount = k;
1685             if (k) {
1686                 if (k < 0)
1687                     goto Fail;
1688                 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1689                 sortslice_advance(&dest, k);
1690                 sortslice_advance(&ssa, k);
1691                 na -= k;
1692                 if (na == 1)
1693                     goto CopyB;
1694                 /* na==0 is impossible now if the comparison
1695                  * function is consistent, but we can't assume
1696                  * that it is.
1697                  */
1698                 if (na == 0)
1699                     goto Succeed;
1700             }
1701             sortslice_copy_incr(&dest, &ssb);
1702             --nb;
1703             if (nb == 0)
1704                 goto Succeed;
1705 
1706             k = gallop_left(ms, ssa.keys[0], ssb.keys, nb, 0);
1707             bcount = k;
1708             if (k) {
1709                 if (k < 0)
1710                     goto Fail;
1711                 sortslice_memmove(&dest, 0, &ssb, 0, k);
1712                 sortslice_advance(&dest, k);
1713                 sortslice_advance(&ssb, k);
1714                 nb -= k;
1715                 if (nb == 0)
1716                     goto Succeed;
1717             }
1718             sortslice_copy_incr(&dest, &ssa);
1719             --na;
1720             if (na == 1)
1721                 goto CopyB;
1722         } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1723         ++min_gallop;           /* penalize it for leaving galloping mode */
1724         ms->min_gallop = min_gallop;
1725     }
1726 Succeed:
1727     result = 0;
1728 Fail:
1729     if (na)
1730         sortslice_memcpy(&dest, 0, &ssa, 0, na);
1731     return result;
1732 CopyB:
1733     assert(na == 1 && nb > 0);
1734     /* The last element of ssa belongs at the end of the merge. */
1735     sortslice_memmove(&dest, 0, &ssb, 0, nb);
1736     sortslice_copy(&dest, nb, &ssa, 0);
1737     return 0;
1738 }
1739 
1740 /* Merge the na elements starting at pa with the nb elements starting at
1741  * ssb.keys = ssa.keys + na in a stable way, in-place.  na and nb must be > 0.
1742  * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1743  * should have na >= nb.  See listsort.txt for more info.  Return 0 if
1744  * successful, -1 if error.
1745  */
1746 static Py_ssize_t
merge_hi(MergeState * ms,sortslice ssa,Py_ssize_t na,sortslice ssb,Py_ssize_t nb)1747 merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1748          sortslice ssb, Py_ssize_t nb)
1749 {
1750     Py_ssize_t k;
1751     sortslice dest, basea, baseb;
1752     int result = -1;            /* guilty until proved innocent */
1753     Py_ssize_t min_gallop;
1754 
1755     assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1756     assert(ssa.keys + na == ssb.keys);
1757     if (MERGE_GETMEM(ms, nb) < 0)
1758         return -1;
1759     dest = ssb;
1760     sortslice_advance(&dest, nb-1);
1761     sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1762     basea = ssa;
1763     baseb = ms->a;
1764     ssb.keys = ms->a.keys + nb - 1;
1765     if (ssb.values != NULL)
1766         ssb.values = ms->a.values + nb - 1;
1767     sortslice_advance(&ssa, na - 1);
1768 
1769     sortslice_copy_decr(&dest, &ssa);
1770     --na;
1771     if (na == 0)
1772         goto Succeed;
1773     if (nb == 1)
1774         goto CopyA;
1775 
1776     min_gallop = ms->min_gallop;
1777     for (;;) {
1778         Py_ssize_t acount = 0;          /* # of times A won in a row */
1779         Py_ssize_t bcount = 0;          /* # of times B won in a row */
1780 
1781         /* Do the straightforward thing until (if ever) one run
1782          * appears to win consistently.
1783          */
1784         for (;;) {
1785             assert(na > 0 && nb > 1);
1786             k = ISLT(ssb.keys[0], ssa.keys[0]);
1787             if (k) {
1788                 if (k < 0)
1789                     goto Fail;
1790                 sortslice_copy_decr(&dest, &ssa);
1791                 ++acount;
1792                 bcount = 0;
1793                 --na;
1794                 if (na == 0)
1795                     goto Succeed;
1796                 if (acount >= min_gallop)
1797                     break;
1798             }
1799             else {
1800                 sortslice_copy_decr(&dest, &ssb);
1801                 ++bcount;
1802                 acount = 0;
1803                 --nb;
1804                 if (nb == 1)
1805                     goto CopyA;
1806                 if (bcount >= min_gallop)
1807                     break;
1808             }
1809         }
1810 
1811         /* One run is winning so consistently that galloping may
1812          * be a huge win.  So try that, and continue galloping until
1813          * (if ever) neither run appears to be winning consistently
1814          * anymore.
1815          */
1816         ++min_gallop;
1817         do {
1818             assert(na > 0 && nb > 1);
1819             min_gallop -= min_gallop > 1;
1820             ms->min_gallop = min_gallop;
1821             k = gallop_right(ms, ssb.keys[0], basea.keys, na, na-1);
1822             if (k < 0)
1823                 goto Fail;
1824             k = na - k;
1825             acount = k;
1826             if (k) {
1827                 sortslice_advance(&dest, -k);
1828                 sortslice_advance(&ssa, -k);
1829                 sortslice_memmove(&dest, 1, &ssa, 1, k);
1830                 na -= k;
1831                 if (na == 0)
1832                     goto Succeed;
1833             }
1834             sortslice_copy_decr(&dest, &ssb);
1835             --nb;
1836             if (nb == 1)
1837                 goto CopyA;
1838 
1839             k = gallop_left(ms, ssa.keys[0], baseb.keys, nb, nb-1);
1840             if (k < 0)
1841                 goto Fail;
1842             k = nb - k;
1843             bcount = k;
1844             if (k) {
1845                 sortslice_advance(&dest, -k);
1846                 sortslice_advance(&ssb, -k);
1847                 sortslice_memcpy(&dest, 1, &ssb, 1, k);
1848                 nb -= k;
1849                 if (nb == 1)
1850                     goto CopyA;
1851                 /* nb==0 is impossible now if the comparison
1852                  * function is consistent, but we can't assume
1853                  * that it is.
1854                  */
1855                 if (nb == 0)
1856                     goto Succeed;
1857             }
1858             sortslice_copy_decr(&dest, &ssa);
1859             --na;
1860             if (na == 0)
1861                 goto Succeed;
1862         } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1863         ++min_gallop;           /* penalize it for leaving galloping mode */
1864         ms->min_gallop = min_gallop;
1865     }
1866 Succeed:
1867     result = 0;
1868 Fail:
1869     if (nb)
1870         sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
1871     return result;
1872 CopyA:
1873     assert(nb == 1 && na > 0);
1874     /* The first element of ssb belongs at the front of the merge. */
1875     sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1876     sortslice_advance(&dest, -na);
1877     sortslice_advance(&ssa, -na);
1878     sortslice_copy(&dest, 0, &ssb, 0);
1879     return 0;
1880 }
1881 
1882 /* Merge the two runs at stack indices i and i+1.
1883  * Returns 0 on success, -1 on error.
1884  */
1885 static Py_ssize_t
merge_at(MergeState * ms,Py_ssize_t i)1886 merge_at(MergeState *ms, Py_ssize_t i)
1887 {
1888     sortslice ssa, ssb;
1889     Py_ssize_t na, nb;
1890     Py_ssize_t k;
1891 
1892     assert(ms != NULL);
1893     assert(ms->n >= 2);
1894     assert(i >= 0);
1895     assert(i == ms->n - 2 || i == ms->n - 3);
1896 
1897     ssa = ms->pending[i].base;
1898     na = ms->pending[i].len;
1899     ssb = ms->pending[i+1].base;
1900     nb = ms->pending[i+1].len;
1901     assert(na > 0 && nb > 0);
1902     assert(ssa.keys + na == ssb.keys);
1903 
1904     /* Record the length of the combined runs; if i is the 3rd-last
1905      * run now, also slide over the last run (which isn't involved
1906      * in this merge).  The current run i+1 goes away in any case.
1907      */
1908     ms->pending[i].len = na + nb;
1909     if (i == ms->n - 3)
1910         ms->pending[i+1] = ms->pending[i+2];
1911     --ms->n;
1912 
1913     /* Where does b start in a?  Elements in a before that can be
1914      * ignored (already in place).
1915      */
1916     k = gallop_right(ms, *ssb.keys, ssa.keys, na, 0);
1917     if (k < 0)
1918         return -1;
1919     sortslice_advance(&ssa, k);
1920     na -= k;
1921     if (na == 0)
1922         return 0;
1923 
1924     /* Where does a end in b?  Elements in b after that can be
1925      * ignored (already in place).
1926      */
1927     nb = gallop_left(ms, ssa.keys[na-1], ssb.keys, nb, nb-1);
1928     if (nb <= 0)
1929         return nb;
1930 
1931     /* Merge what remains of the runs, using a temp array with
1932      * min(na, nb) elements.
1933      */
1934     if (na <= nb)
1935         return merge_lo(ms, ssa, na, ssb, nb);
1936     else
1937         return merge_hi(ms, ssa, na, ssb, nb);
1938 }
1939 
1940 /* Examine the stack of runs waiting to be merged, merging adjacent runs
1941  * until the stack invariants are re-established:
1942  *
1943  * 1. len[-3] > len[-2] + len[-1]
1944  * 2. len[-2] > len[-1]
1945  *
1946  * See listsort.txt for more info.
1947  *
1948  * Returns 0 on success, -1 on error.
1949  */
1950 static int
merge_collapse(MergeState * ms)1951 merge_collapse(MergeState *ms)
1952 {
1953     struct s_slice *p = ms->pending;
1954 
1955     assert(ms);
1956     while (ms->n > 1) {
1957         Py_ssize_t n = ms->n - 2;
1958         if ((n > 0 && p[n-1].len <= p[n].len + p[n+1].len) ||
1959             (n > 1 && p[n-2].len <= p[n-1].len + p[n].len)) {
1960             if (p[n-1].len < p[n+1].len)
1961                 --n;
1962             if (merge_at(ms, n) < 0)
1963                 return -1;
1964         }
1965         else if (p[n].len <= p[n+1].len) {
1966             if (merge_at(ms, n) < 0)
1967                 return -1;
1968         }
1969         else
1970             break;
1971     }
1972     return 0;
1973 }
1974 
1975 /* Regardless of invariants, merge all runs on the stack until only one
1976  * remains.  This is used at the end of the mergesort.
1977  *
1978  * Returns 0 on success, -1 on error.
1979  */
1980 static int
merge_force_collapse(MergeState * ms)1981 merge_force_collapse(MergeState *ms)
1982 {
1983     struct s_slice *p = ms->pending;
1984 
1985     assert(ms);
1986     while (ms->n > 1) {
1987         Py_ssize_t n = ms->n - 2;
1988         if (n > 0 && p[n-1].len < p[n+1].len)
1989             --n;
1990         if (merge_at(ms, n) < 0)
1991             return -1;
1992     }
1993     return 0;
1994 }
1995 
1996 /* Compute a good value for the minimum run length; natural runs shorter
1997  * than this are boosted artificially via binary insertion.
1998  *
1999  * If n < 64, return n (it's too small to bother with fancy stuff).
2000  * Else if n is an exact power of 2, return 32.
2001  * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
2002  * strictly less than, an exact power of 2.
2003  *
2004  * See listsort.txt for more info.
2005  */
2006 static Py_ssize_t
merge_compute_minrun(Py_ssize_t n)2007 merge_compute_minrun(Py_ssize_t n)
2008 {
2009     Py_ssize_t r = 0;           /* becomes 1 if any 1 bits are shifted off */
2010 
2011     assert(n >= 0);
2012     while (n >= 64) {
2013         r |= n & 1;
2014         n >>= 1;
2015     }
2016     return n + r;
2017 }
2018 
2019 static void
reverse_sortslice(sortslice * s,Py_ssize_t n)2020 reverse_sortslice(sortslice *s, Py_ssize_t n)
2021 {
2022     reverse_slice(s->keys, &s->keys[n]);
2023     if (s->values != NULL)
2024         reverse_slice(s->values, &s->values[n]);
2025 }
2026 
2027 /* Here we define custom comparison functions to optimize for the cases one commonly
2028  * encounters in practice: homogeneous lists, often of one of the basic types. */
2029 
2030 /* This struct holds the comparison function and helper functions
2031  * selected in the pre-sort check. */
2032 
2033 /* These are the special case compare functions.
2034  * ms->key_compare will always point to one of these: */
2035 
2036 /* Heterogeneous compare: default, always safe to fall back on. */
2037 static int
safe_object_compare(PyObject * v,PyObject * w,MergeState * ms)2038 safe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2039 {
2040     /* No assumptions necessary! */
2041     return PyObject_RichCompareBool(v, w, Py_LT);
2042 }
2043 
2044 /* Homogeneous compare: safe for any two compareable objects of the same type.
2045  * (ms->key_richcompare is set to ob_type->tp_richcompare in the
2046  *  pre-sort check.)
2047  */
2048 static int
unsafe_object_compare(PyObject * v,PyObject * w,MergeState * ms)2049 unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2050 {
2051     PyObject *res_obj; int res;
2052 
2053     /* No assumptions, because we check first: */
2054     if (v->ob_type->tp_richcompare != ms->key_richcompare)
2055         return PyObject_RichCompareBool(v, w, Py_LT);
2056 
2057     assert(ms->key_richcompare != NULL);
2058     res_obj = (*(ms->key_richcompare))(v, w, Py_LT);
2059 
2060     if (res_obj == Py_NotImplemented) {
2061         Py_DECREF(res_obj);
2062         return PyObject_RichCompareBool(v, w, Py_LT);
2063     }
2064     if (res_obj == NULL)
2065         return -1;
2066 
2067     if (PyBool_Check(res_obj)) {
2068         res = (res_obj == Py_True);
2069     }
2070     else {
2071         res = PyObject_IsTrue(res_obj);
2072     }
2073     Py_DECREF(res_obj);
2074 
2075     /* Note that we can't assert
2076      *     res == PyObject_RichCompareBool(v, w, Py_LT);
2077      * because of evil compare functions like this:
2078      *     lambda a, b:  int(random.random() * 3) - 1)
2079      * (which is actually in test_sort.py) */
2080     return res;
2081 }
2082 
2083 /* Latin string compare: safe for any two latin (one byte per char) strings. */
2084 static int
unsafe_latin_compare(PyObject * v,PyObject * w,MergeState * ms)2085 unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
2086 {
2087     Py_ssize_t len;
2088     int res;
2089 
2090     /* Modified from Objects/unicodeobject.c:unicode_compare, assuming: */
2091     assert(v->ob_type == w->ob_type);
2092     assert(v->ob_type == &PyUnicode_Type);
2093     assert(PyUnicode_KIND(v) == PyUnicode_KIND(w));
2094     assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
2095 
2096     len = Py_MIN(PyUnicode_GET_LENGTH(v), PyUnicode_GET_LENGTH(w));
2097     res = memcmp(PyUnicode_DATA(v), PyUnicode_DATA(w), len);
2098 
2099     res = (res != 0 ?
2100            res < 0 :
2101            PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w));
2102 
2103     assert(res == PyObject_RichCompareBool(v, w, Py_LT));;
2104     return res;
2105 }
2106 
2107 /* Bounded int compare: compare any two longs that fit in a single machine word. */
2108 static int
unsafe_long_compare(PyObject * v,PyObject * w,MergeState * ms)2109 unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
2110 {
2111     PyLongObject *vl, *wl; sdigit v0, w0; int res;
2112 
2113     /* Modified from Objects/longobject.c:long_compare, assuming: */
2114     assert(v->ob_type == w->ob_type);
2115     assert(v->ob_type == &PyLong_Type);
2116     assert(Py_ABS(Py_SIZE(v)) <= 1);
2117     assert(Py_ABS(Py_SIZE(w)) <= 1);
2118 
2119     vl = (PyLongObject*)v;
2120     wl = (PyLongObject*)w;
2121 
2122     v0 = Py_SIZE(vl) == 0 ? 0 : (sdigit)vl->ob_digit[0];
2123     w0 = Py_SIZE(wl) == 0 ? 0 : (sdigit)wl->ob_digit[0];
2124 
2125     if (Py_SIZE(vl) < 0)
2126         v0 = -v0;
2127     if (Py_SIZE(wl) < 0)
2128         w0 = -w0;
2129 
2130     res = v0 < w0;
2131     assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2132     return res;
2133 }
2134 
2135 /* Float compare: compare any two floats. */
2136 static int
unsafe_float_compare(PyObject * v,PyObject * w,MergeState * ms)2137 unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms)
2138 {
2139     int res;
2140 
2141     /* Modified from Objects/floatobject.c:float_richcompare, assuming: */
2142     assert(v->ob_type == w->ob_type);
2143     assert(v->ob_type == &PyFloat_Type);
2144 
2145     res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w);
2146     assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2147     return res;
2148 }
2149 
2150 /* Tuple compare: compare *any* two tuples, using
2151  * ms->tuple_elem_compare to compare the first elements, which is set
2152  * using the same pre-sort check as we use for ms->key_compare,
2153  * but run on the list [x[0] for x in L]. This allows us to optimize compares
2154  * on two levels (as long as [x[0] for x in L] is type-homogeneous.) The idea is
2155  * that most tuple compares don't involve x[1:]. */
2156 static int
unsafe_tuple_compare(PyObject * v,PyObject * w,MergeState * ms)2157 unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
2158 {
2159     PyTupleObject *vt, *wt;
2160     Py_ssize_t i, vlen, wlen;
2161     int k;
2162 
2163     /* Modified from Objects/tupleobject.c:tuplerichcompare, assuming: */
2164     assert(v->ob_type == w->ob_type);
2165     assert(v->ob_type == &PyTuple_Type);
2166     assert(Py_SIZE(v) > 0);
2167     assert(Py_SIZE(w) > 0);
2168 
2169     vt = (PyTupleObject *)v;
2170     wt = (PyTupleObject *)w;
2171 
2172     vlen = Py_SIZE(vt);
2173     wlen = Py_SIZE(wt);
2174 
2175     for (i = 0; i < vlen && i < wlen; i++) {
2176         k = PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_EQ);
2177         if (k < 0)
2178             return -1;
2179         if (!k)
2180             break;
2181     }
2182 
2183     if (i >= vlen || i >= wlen)
2184         return vlen < wlen;
2185 
2186     if (i == 0)
2187         return ms->tuple_elem_compare(vt->ob_item[i], wt->ob_item[i], ms);
2188     else
2189         return PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_LT);
2190 }
2191 
2192 /* An adaptive, stable, natural mergesort.  See listsort.txt.
2193  * Returns Py_None on success, NULL on error.  Even in case of error, the
2194  * list will be some permutation of its input state (nothing is lost or
2195  * duplicated).
2196  */
2197 /*[clinic input]
2198 list.sort
2199 
2200     *
2201     key as keyfunc: object = None
2202     reverse: bool(accept={int}) = False
2203 
2204 Sort the list in ascending order and return None.
2205 
2206 The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
2207 order of two equal elements is maintained).
2208 
2209 If a key function is given, apply it once to each list item and sort them,
2210 ascending or descending, according to their function values.
2211 
2212 The reverse flag can be set to sort in descending order.
2213 [clinic start generated code]*/
2214 
2215 static PyObject *
list_sort_impl(PyListObject * self,PyObject * keyfunc,int reverse)2216 list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
2217 /*[clinic end generated code: output=57b9f9c5e23fbe42 input=cb56cd179a713060]*/
2218 {
2219     MergeState ms;
2220     Py_ssize_t nremaining;
2221     Py_ssize_t minrun;
2222     sortslice lo;
2223     Py_ssize_t saved_ob_size, saved_allocated;
2224     PyObject **saved_ob_item;
2225     PyObject **final_ob_item;
2226     PyObject *result = NULL;            /* guilty until proved innocent */
2227     Py_ssize_t i;
2228     PyObject **keys;
2229 
2230     assert(self != NULL);
2231     assert(PyList_Check(self));
2232     if (keyfunc == Py_None)
2233         keyfunc = NULL;
2234 
2235     /* The list is temporarily made empty, so that mutations performed
2236      * by comparison functions can't affect the slice of memory we're
2237      * sorting (allowing mutations during sorting is a core-dump
2238      * factory, since ob_item may change).
2239      */
2240     saved_ob_size = Py_SIZE(self);
2241     saved_ob_item = self->ob_item;
2242     saved_allocated = self->allocated;
2243     Py_SIZE(self) = 0;
2244     self->ob_item = NULL;
2245     self->allocated = -1; /* any operation will reset it to >= 0 */
2246 
2247     if (keyfunc == NULL) {
2248         keys = NULL;
2249         lo.keys = saved_ob_item;
2250         lo.values = NULL;
2251     }
2252     else {
2253         if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
2254             /* Leverage stack space we allocated but won't otherwise use */
2255             keys = &ms.temparray[saved_ob_size+1];
2256         else {
2257             keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
2258             if (keys == NULL) {
2259                 PyErr_NoMemory();
2260                 goto keyfunc_fail;
2261             }
2262         }
2263 
2264         for (i = 0; i < saved_ob_size ; i++) {
2265             keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
2266                                                    NULL);
2267             if (keys[i] == NULL) {
2268                 for (i=i-1 ; i>=0 ; i--)
2269                     Py_DECREF(keys[i]);
2270                 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
2271                     PyMem_FREE(keys);
2272                 goto keyfunc_fail;
2273             }
2274         }
2275 
2276         lo.keys = keys;
2277         lo.values = saved_ob_item;
2278     }
2279 
2280 
2281     /* The pre-sort check: here's where we decide which compare function to use.
2282      * How much optimization is safe? We test for homogeneity with respect to
2283      * several properties that are expensive to check at compare-time, and
2284      * set ms appropriately. */
2285     if (saved_ob_size > 1) {
2286         /* Assume the first element is representative of the whole list. */
2287         int keys_are_in_tuples = (lo.keys[0]->ob_type == &PyTuple_Type &&
2288                                   Py_SIZE(lo.keys[0]) > 0);
2289 
2290         PyTypeObject* key_type = (keys_are_in_tuples ?
2291                                   PyTuple_GET_ITEM(lo.keys[0], 0)->ob_type :
2292                                   lo.keys[0]->ob_type);
2293 
2294         int keys_are_all_same_type = 1;
2295         int strings_are_latin = 1;
2296         int ints_are_bounded = 1;
2297 
2298         /* Prove that assumption by checking every key. */
2299         for (i=0; i < saved_ob_size; i++) {
2300 
2301             if (keys_are_in_tuples &&
2302                 !(lo.keys[i]->ob_type == &PyTuple_Type && Py_SIZE(lo.keys[i]) != 0)) {
2303                 keys_are_in_tuples = 0;
2304                 keys_are_all_same_type = 0;
2305                 break;
2306             }
2307 
2308             /* Note: for lists of tuples, key is the first element of the tuple
2309              * lo.keys[i], not lo.keys[i] itself! We verify type-homogeneity
2310              * for lists of tuples in the if-statement directly above. */
2311             PyObject *key = (keys_are_in_tuples ?
2312                              PyTuple_GET_ITEM(lo.keys[i], 0) :
2313                              lo.keys[i]);
2314 
2315             if (key->ob_type != key_type) {
2316                 keys_are_all_same_type = 0;
2317                 /* If keys are in tuple we must loop over the whole list to make
2318                    sure all items are tuples */
2319                 if (!keys_are_in_tuples) {
2320                     break;
2321                 }
2322             }
2323 
2324             if (keys_are_all_same_type) {
2325                 if (key_type == &PyLong_Type &&
2326                     ints_are_bounded &&
2327                     Py_ABS(Py_SIZE(key)) > 1) {
2328 
2329                     ints_are_bounded = 0;
2330                 }
2331                 else if (key_type == &PyUnicode_Type &&
2332                          strings_are_latin &&
2333                          PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND) {
2334 
2335                         strings_are_latin = 0;
2336                     }
2337                 }
2338             }
2339 
2340         /* Choose the best compare, given what we now know about the keys. */
2341         if (keys_are_all_same_type) {
2342 
2343             if (key_type == &PyUnicode_Type && strings_are_latin) {
2344                 ms.key_compare = unsafe_latin_compare;
2345             }
2346             else if (key_type == &PyLong_Type && ints_are_bounded) {
2347                 ms.key_compare = unsafe_long_compare;
2348             }
2349             else if (key_type == &PyFloat_Type) {
2350                 ms.key_compare = unsafe_float_compare;
2351             }
2352             else if ((ms.key_richcompare = key_type->tp_richcompare) != NULL) {
2353                 ms.key_compare = unsafe_object_compare;
2354             }
2355             else {
2356                 ms.key_compare = safe_object_compare;
2357             }
2358         }
2359         else {
2360             ms.key_compare = safe_object_compare;
2361         }
2362 
2363         if (keys_are_in_tuples) {
2364             /* Make sure we're not dealing with tuples of tuples
2365              * (remember: here, key_type refers list [key[0] for key in keys]) */
2366             if (key_type == &PyTuple_Type) {
2367                 ms.tuple_elem_compare = safe_object_compare;
2368             }
2369             else {
2370                 ms.tuple_elem_compare = ms.key_compare;
2371             }
2372 
2373             ms.key_compare = unsafe_tuple_compare;
2374         }
2375     }
2376     /* End of pre-sort check: ms is now set properly! */
2377 
2378     merge_init(&ms, saved_ob_size, keys != NULL);
2379 
2380     nremaining = saved_ob_size;
2381     if (nremaining < 2)
2382         goto succeed;
2383 
2384     /* Reverse sort stability achieved by initially reversing the list,
2385     applying a stable forward sort, then reversing the final result. */
2386     if (reverse) {
2387         if (keys != NULL)
2388             reverse_slice(&keys[0], &keys[saved_ob_size]);
2389         reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
2390     }
2391 
2392     /* March over the array once, left to right, finding natural runs,
2393      * and extending short natural runs to minrun elements.
2394      */
2395     minrun = merge_compute_minrun(nremaining);
2396     do {
2397         int descending;
2398         Py_ssize_t n;
2399 
2400         /* Identify next run. */
2401         n = count_run(&ms, lo.keys, lo.keys + nremaining, &descending);
2402         if (n < 0)
2403             goto fail;
2404         if (descending)
2405             reverse_sortslice(&lo, n);
2406         /* If short, extend to min(minrun, nremaining). */
2407         if (n < minrun) {
2408             const Py_ssize_t force = nremaining <= minrun ?
2409                               nremaining : minrun;
2410             if (binarysort(&ms, lo, lo.keys + force, lo.keys + n) < 0)
2411                 goto fail;
2412             n = force;
2413         }
2414         /* Push run onto pending-runs stack, and maybe merge. */
2415         assert(ms.n < MAX_MERGE_PENDING);
2416         ms.pending[ms.n].base = lo;
2417         ms.pending[ms.n].len = n;
2418         ++ms.n;
2419         if (merge_collapse(&ms) < 0)
2420             goto fail;
2421         /* Advance to find next run. */
2422         sortslice_advance(&lo, n);
2423         nremaining -= n;
2424     } while (nremaining);
2425 
2426     if (merge_force_collapse(&ms) < 0)
2427         goto fail;
2428     assert(ms.n == 1);
2429     assert(keys == NULL
2430            ? ms.pending[0].base.keys == saved_ob_item
2431            : ms.pending[0].base.keys == &keys[0]);
2432     assert(ms.pending[0].len == saved_ob_size);
2433     lo = ms.pending[0].base;
2434 
2435 succeed:
2436     result = Py_None;
2437 fail:
2438     if (keys != NULL) {
2439         for (i = 0; i < saved_ob_size; i++)
2440             Py_DECREF(keys[i]);
2441         if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
2442             PyMem_FREE(keys);
2443     }
2444 
2445     if (self->allocated != -1 && result != NULL) {
2446         /* The user mucked with the list during the sort,
2447          * and we don't already have another error to report.
2448          */
2449         PyErr_SetString(PyExc_ValueError, "list modified during sort");
2450         result = NULL;
2451     }
2452 
2453     if (reverse && saved_ob_size > 1)
2454         reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
2455 
2456     merge_freemem(&ms);
2457 
2458 keyfunc_fail:
2459     final_ob_item = self->ob_item;
2460     i = Py_SIZE(self);
2461     Py_SIZE(self) = saved_ob_size;
2462     self->ob_item = saved_ob_item;
2463     self->allocated = saved_allocated;
2464     if (final_ob_item != NULL) {
2465         /* we cannot use _list_clear() for this because it does not
2466            guarantee that the list is really empty when it returns */
2467         while (--i >= 0) {
2468             Py_XDECREF(final_ob_item[i]);
2469         }
2470         PyMem_FREE(final_ob_item);
2471     }
2472     Py_XINCREF(result);
2473     return result;
2474 }
2475 #undef IFLT
2476 #undef ISLT
2477 
2478 int
PyList_Sort(PyObject * v)2479 PyList_Sort(PyObject *v)
2480 {
2481     if (v == NULL || !PyList_Check(v)) {
2482         PyErr_BadInternalCall();
2483         return -1;
2484     }
2485     v = list_sort_impl((PyListObject *)v, NULL, 0);
2486     if (v == NULL)
2487         return -1;
2488     Py_DECREF(v);
2489     return 0;
2490 }
2491 
2492 /*[clinic input]
2493 list.reverse
2494 
2495 Reverse *IN PLACE*.
2496 [clinic start generated code]*/
2497 
2498 static PyObject *
list_reverse_impl(PyListObject * self)2499 list_reverse_impl(PyListObject *self)
2500 /*[clinic end generated code: output=482544fc451abea9 input=eefd4c3ae1bc9887]*/
2501 {
2502     if (Py_SIZE(self) > 1)
2503         reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2504     Py_RETURN_NONE;
2505 }
2506 
2507 int
PyList_Reverse(PyObject * v)2508 PyList_Reverse(PyObject *v)
2509 {
2510     PyListObject *self = (PyListObject *)v;
2511 
2512     if (v == NULL || !PyList_Check(v)) {
2513         PyErr_BadInternalCall();
2514         return -1;
2515     }
2516     if (Py_SIZE(self) > 1)
2517         reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2518     return 0;
2519 }
2520 
2521 PyObject *
PyList_AsTuple(PyObject * v)2522 PyList_AsTuple(PyObject *v)
2523 {
2524     if (v == NULL || !PyList_Check(v)) {
2525         PyErr_BadInternalCall();
2526         return NULL;
2527     }
2528     return _PyTuple_FromArray(((PyListObject *)v)->ob_item, Py_SIZE(v));
2529 }
2530 
2531 /*[clinic input]
2532 list.index
2533 
2534     value: object
2535     start: slice_index(accept={int}) = 0
2536     stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
2537     /
2538 
2539 Return first index of value.
2540 
2541 Raises ValueError if the value is not present.
2542 [clinic start generated code]*/
2543 
2544 static PyObject *
list_index_impl(PyListObject * self,PyObject * value,Py_ssize_t start,Py_ssize_t stop)2545 list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
2546                 Py_ssize_t stop)
2547 /*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
2548 {
2549     Py_ssize_t i;
2550 
2551     if (start < 0) {
2552         start += Py_SIZE(self);
2553         if (start < 0)
2554             start = 0;
2555     }
2556     if (stop < 0) {
2557         stop += Py_SIZE(self);
2558         if (stop < 0)
2559             stop = 0;
2560     }
2561     for (i = start; i < stop && i < Py_SIZE(self); i++) {
2562         PyObject *obj = self->ob_item[i];
2563         Py_INCREF(obj);
2564         int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
2565         Py_DECREF(obj);
2566         if (cmp > 0)
2567             return PyLong_FromSsize_t(i);
2568         else if (cmp < 0)
2569             return NULL;
2570     }
2571     PyErr_Format(PyExc_ValueError, "%R is not in list", value);
2572     return NULL;
2573 }
2574 
2575 /*[clinic input]
2576 list.count
2577 
2578      value: object
2579      /
2580 
2581 Return number of occurrences of value.
2582 [clinic start generated code]*/
2583 
2584 static PyObject *
list_count(PyListObject * self,PyObject * value)2585 list_count(PyListObject *self, PyObject *value)
2586 /*[clinic end generated code: output=b1f5d284205ae714 input=3bdc3a5e6f749565]*/
2587 {
2588     Py_ssize_t count = 0;
2589     Py_ssize_t i;
2590 
2591     for (i = 0; i < Py_SIZE(self); i++) {
2592         PyObject *obj = self->ob_item[i];
2593         if (obj == value) {
2594            count++;
2595            continue;
2596         }
2597         Py_INCREF(obj);
2598         int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
2599         Py_DECREF(obj);
2600         if (cmp > 0)
2601             count++;
2602         else if (cmp < 0)
2603             return NULL;
2604     }
2605     return PyLong_FromSsize_t(count);
2606 }
2607 
2608 /*[clinic input]
2609 list.remove
2610 
2611      value: object
2612      /
2613 
2614 Remove first occurrence of value.
2615 
2616 Raises ValueError if the value is not present.
2617 [clinic start generated code]*/
2618 
2619 static PyObject *
list_remove(PyListObject * self,PyObject * value)2620 list_remove(PyListObject *self, PyObject *value)
2621 /*[clinic end generated code: output=f087e1951a5e30d1 input=2dc2ba5bb2fb1f82]*/
2622 {
2623     Py_ssize_t i;
2624 
2625     for (i = 0; i < Py_SIZE(self); i++) {
2626         PyObject *obj = self->ob_item[i];
2627         Py_INCREF(obj);
2628         int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
2629         Py_DECREF(obj);
2630         if (cmp > 0) {
2631             if (list_ass_slice(self, i, i+1,
2632                                (PyObject *)NULL) == 0)
2633                 Py_RETURN_NONE;
2634             return NULL;
2635         }
2636         else if (cmp < 0)
2637             return NULL;
2638     }
2639     PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2640     return NULL;
2641 }
2642 
2643 static int
list_traverse(PyListObject * o,visitproc visit,void * arg)2644 list_traverse(PyListObject *o, visitproc visit, void *arg)
2645 {
2646     Py_ssize_t i;
2647 
2648     for (i = Py_SIZE(o); --i >= 0; )
2649         Py_VISIT(o->ob_item[i]);
2650     return 0;
2651 }
2652 
2653 static PyObject *
list_richcompare(PyObject * v,PyObject * w,int op)2654 list_richcompare(PyObject *v, PyObject *w, int op)
2655 {
2656     PyListObject *vl, *wl;
2657     Py_ssize_t i;
2658 
2659     if (!PyList_Check(v) || !PyList_Check(w))
2660         Py_RETURN_NOTIMPLEMENTED;
2661 
2662     vl = (PyListObject *)v;
2663     wl = (PyListObject *)w;
2664 
2665     if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2666         /* Shortcut: if the lengths differ, the lists differ */
2667         if (op == Py_EQ)
2668             Py_RETURN_FALSE;
2669         else
2670             Py_RETURN_TRUE;
2671     }
2672 
2673     /* Search for the first index where items are different */
2674     for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2675         PyObject *vitem = vl->ob_item[i];
2676         PyObject *witem = wl->ob_item[i];
2677         if (vitem == witem) {
2678             continue;
2679         }
2680 
2681         Py_INCREF(vitem);
2682         Py_INCREF(witem);
2683         int k = PyObject_RichCompareBool(vl->ob_item[i],
2684                                          wl->ob_item[i], Py_EQ);
2685         Py_DECREF(vitem);
2686         Py_DECREF(witem);
2687         if (k < 0)
2688             return NULL;
2689         if (!k)
2690             break;
2691     }
2692 
2693     if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2694         /* No more items to compare -- compare sizes */
2695         Py_RETURN_RICHCOMPARE(Py_SIZE(vl), Py_SIZE(wl), op);
2696     }
2697 
2698     /* We have an item that differs -- shortcuts for EQ/NE */
2699     if (op == Py_EQ) {
2700         Py_RETURN_FALSE;
2701     }
2702     if (op == Py_NE) {
2703         Py_RETURN_TRUE;
2704     }
2705 
2706     /* Compare the final item again using the proper operator */
2707     return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
2708 }
2709 
2710 /*[clinic input]
2711 list.__init__
2712 
2713     iterable: object(c_default="NULL") = ()
2714     /
2715 
2716 Built-in mutable sequence.
2717 
2718 If no argument is given, the constructor creates a new empty list.
2719 The argument must be an iterable if specified.
2720 [clinic start generated code]*/
2721 
2722 static int
list___init___impl(PyListObject * self,PyObject * iterable)2723 list___init___impl(PyListObject *self, PyObject *iterable)
2724 /*[clinic end generated code: output=0f3c21379d01de48 input=b3f3fe7206af8f6b]*/
2725 {
2726     /* Verify list invariants established by PyType_GenericAlloc() */
2727     assert(0 <= Py_SIZE(self));
2728     assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2729     assert(self->ob_item != NULL ||
2730            self->allocated == 0 || self->allocated == -1);
2731 
2732     /* Empty previous contents */
2733     if (self->ob_item != NULL) {
2734         (void)_list_clear(self);
2735     }
2736     if (iterable != NULL) {
2737         if (_PyObject_HasLen(iterable)) {
2738             Py_ssize_t iter_len = PyObject_Size(iterable);
2739             if (iter_len == -1) {
2740                 if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
2741                     return -1;
2742                 }
2743                 PyErr_Clear();
2744             }
2745             if (iter_len > 0 && self->ob_item == NULL
2746                 && list_preallocate_exact(self, iter_len)) {
2747                 return -1;
2748             }
2749         }
2750         PyObject *rv = list_extend(self, iterable);
2751         if (rv == NULL)
2752             return -1;
2753         Py_DECREF(rv);
2754     }
2755     return 0;
2756 }
2757 
2758 /*[clinic input]
2759 list.__sizeof__
2760 
2761 Return the size of the list in memory, in bytes.
2762 [clinic start generated code]*/
2763 
2764 static PyObject *
list___sizeof___impl(PyListObject * self)2765 list___sizeof___impl(PyListObject *self)
2766 /*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
2767 {
2768     Py_ssize_t res;
2769 
2770     res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*);
2771     return PyLong_FromSsize_t(res);
2772 }
2773 
2774 static PyObject *list_iter(PyObject *seq);
2775 static PyObject *list_subscript(PyListObject*, PyObject*);
2776 
2777 static PyMethodDef list_methods[] = {
2778     {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, "x.__getitem__(y) <==> x[y]"},
2779     LIST___REVERSED___METHODDEF
2780     LIST___SIZEOF___METHODDEF
2781     LIST_CLEAR_METHODDEF
2782     LIST_COPY_METHODDEF
2783     LIST_APPEND_METHODDEF
2784     LIST_INSERT_METHODDEF
2785     LIST_EXTEND_METHODDEF
2786     LIST_POP_METHODDEF
2787     LIST_REMOVE_METHODDEF
2788     LIST_INDEX_METHODDEF
2789     LIST_COUNT_METHODDEF
2790     LIST_REVERSE_METHODDEF
2791     LIST_SORT_METHODDEF
2792     {NULL,              NULL}           /* sentinel */
2793 };
2794 
2795 static PySequenceMethods list_as_sequence = {
2796     (lenfunc)list_length,                       /* sq_length */
2797     (binaryfunc)list_concat,                    /* sq_concat */
2798     (ssizeargfunc)list_repeat,                  /* sq_repeat */
2799     (ssizeargfunc)list_item,                    /* sq_item */
2800     0,                                          /* sq_slice */
2801     (ssizeobjargproc)list_ass_item,             /* sq_ass_item */
2802     0,                                          /* sq_ass_slice */
2803     (objobjproc)list_contains,                  /* sq_contains */
2804     (binaryfunc)list_inplace_concat,            /* sq_inplace_concat */
2805     (ssizeargfunc)list_inplace_repeat,          /* sq_inplace_repeat */
2806 };
2807 
2808 static PyObject *
list_subscript(PyListObject * self,PyObject * item)2809 list_subscript(PyListObject* self, PyObject* item)
2810 {
2811     if (PyIndex_Check(item)) {
2812         Py_ssize_t i;
2813         i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2814         if (i == -1 && PyErr_Occurred())
2815             return NULL;
2816         if (i < 0)
2817             i += PyList_GET_SIZE(self);
2818         return list_item(self, i);
2819     }
2820     else if (PySlice_Check(item)) {
2821         Py_ssize_t start, stop, step, slicelength, cur, i;
2822         PyObject* result;
2823         PyObject* it;
2824         PyObject **src, **dest;
2825 
2826         if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
2827             return NULL;
2828         }
2829         slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2830                                             step);
2831 
2832         if (slicelength <= 0) {
2833             return PyList_New(0);
2834         }
2835         else if (step == 1) {
2836             return list_slice(self, start, stop);
2837         }
2838         else {
2839             result = list_new_prealloc(slicelength);
2840             if (!result) return NULL;
2841 
2842             src = self->ob_item;
2843             dest = ((PyListObject *)result)->ob_item;
2844             for (cur = start, i = 0; i < slicelength;
2845                  cur += (size_t)step, i++) {
2846                 it = src[cur];
2847                 Py_INCREF(it);
2848                 dest[i] = it;
2849             }
2850             Py_SIZE(result) = slicelength;
2851             return result;
2852         }
2853     }
2854     else {
2855         PyErr_Format(PyExc_TypeError,
2856                      "list indices must be integers or slices, not %.200s",
2857                      item->ob_type->tp_name);
2858         return NULL;
2859     }
2860 }
2861 
2862 static int
list_ass_subscript(PyListObject * self,PyObject * item,PyObject * value)2863 list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2864 {
2865     if (PyIndex_Check(item)) {
2866         Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2867         if (i == -1 && PyErr_Occurred())
2868             return -1;
2869         if (i < 0)
2870             i += PyList_GET_SIZE(self);
2871         return list_ass_item(self, i, value);
2872     }
2873     else if (PySlice_Check(item)) {
2874         Py_ssize_t start, stop, step, slicelength;
2875 
2876         if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
2877             return -1;
2878         }
2879         slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2880                                             step);
2881 
2882         if (step == 1)
2883             return list_ass_slice(self, start, stop, value);
2884 
2885         /* Make sure s[5:2] = [..] inserts at the right place:
2886            before 5, not before 2. */
2887         if ((step < 0 && start < stop) ||
2888             (step > 0 && start > stop))
2889             stop = start;
2890 
2891         if (value == NULL) {
2892             /* delete slice */
2893             PyObject **garbage;
2894             size_t cur;
2895             Py_ssize_t i;
2896             int res;
2897 
2898             if (slicelength <= 0)
2899                 return 0;
2900 
2901             if (step < 0) {
2902                 stop = start + 1;
2903                 start = stop + step*(slicelength - 1) - 1;
2904                 step = -step;
2905             }
2906 
2907             garbage = (PyObject**)
2908                 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2909             if (!garbage) {
2910                 PyErr_NoMemory();
2911                 return -1;
2912             }
2913 
2914             /* drawing pictures might help understand these for
2915                loops. Basically, we memmove the parts of the
2916                list that are *not* part of the slice: step-1
2917                items for each item that is part of the slice,
2918                and then tail end of the list that was not
2919                covered by the slice */
2920             for (cur = start, i = 0;
2921                  cur < (size_t)stop;
2922                  cur += step, i++) {
2923                 Py_ssize_t lim = step - 1;
2924 
2925                 garbage[i] = PyList_GET_ITEM(self, cur);
2926 
2927                 if (cur + step >= (size_t)Py_SIZE(self)) {
2928                     lim = Py_SIZE(self) - cur - 1;
2929                 }
2930 
2931                 memmove(self->ob_item + cur - i,
2932                     self->ob_item + cur + 1,
2933                     lim * sizeof(PyObject *));
2934             }
2935             cur = start + (size_t)slicelength * step;
2936             if (cur < (size_t)Py_SIZE(self)) {
2937                 memmove(self->ob_item + cur - slicelength,
2938                     self->ob_item + cur,
2939                     (Py_SIZE(self) - cur) *
2940                      sizeof(PyObject *));
2941             }
2942 
2943             Py_SIZE(self) -= slicelength;
2944             res = list_resize(self, Py_SIZE(self));
2945 
2946             for (i = 0; i < slicelength; i++) {
2947                 Py_DECREF(garbage[i]);
2948             }
2949             PyMem_FREE(garbage);
2950 
2951             return res;
2952         }
2953         else {
2954             /* assign slice */
2955             PyObject *ins, *seq;
2956             PyObject **garbage, **seqitems, **selfitems;
2957             Py_ssize_t cur, i;
2958 
2959             /* protect against a[::-1] = a */
2960             if (self == (PyListObject*)value) {
2961                 seq = list_slice((PyListObject*)value, 0,
2962                                    PyList_GET_SIZE(value));
2963             }
2964             else {
2965                 seq = PySequence_Fast(value,
2966                                       "must assign iterable "
2967                                       "to extended slice");
2968             }
2969             if (!seq)
2970                 return -1;
2971 
2972             if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2973                 PyErr_Format(PyExc_ValueError,
2974                     "attempt to assign sequence of "
2975                     "size %zd to extended slice of "
2976                     "size %zd",
2977                          PySequence_Fast_GET_SIZE(seq),
2978                          slicelength);
2979                 Py_DECREF(seq);
2980                 return -1;
2981             }
2982 
2983             if (!slicelength) {
2984                 Py_DECREF(seq);
2985                 return 0;
2986             }
2987 
2988             garbage = (PyObject**)
2989                 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2990             if (!garbage) {
2991                 Py_DECREF(seq);
2992                 PyErr_NoMemory();
2993                 return -1;
2994             }
2995 
2996             selfitems = self->ob_item;
2997             seqitems = PySequence_Fast_ITEMS(seq);
2998             for (cur = start, i = 0; i < slicelength;
2999                  cur += (size_t)step, i++) {
3000                 garbage[i] = selfitems[cur];
3001                 ins = seqitems[i];
3002                 Py_INCREF(ins);
3003                 selfitems[cur] = ins;
3004             }
3005 
3006             for (i = 0; i < slicelength; i++) {
3007                 Py_DECREF(garbage[i]);
3008             }
3009 
3010             PyMem_FREE(garbage);
3011             Py_DECREF(seq);
3012 
3013             return 0;
3014         }
3015     }
3016     else {
3017         PyErr_Format(PyExc_TypeError,
3018                      "list indices must be integers or slices, not %.200s",
3019                      item->ob_type->tp_name);
3020         return -1;
3021     }
3022 }
3023 
3024 static PyMappingMethods list_as_mapping = {
3025     (lenfunc)list_length,
3026     (binaryfunc)list_subscript,
3027     (objobjargproc)list_ass_subscript
3028 };
3029 
3030 PyTypeObject PyList_Type = {
3031     PyVarObject_HEAD_INIT(&PyType_Type, 0)
3032     "list",
3033     sizeof(PyListObject),
3034     0,
3035     (destructor)list_dealloc,                   /* tp_dealloc */
3036     0,                                          /* tp_vectorcall_offset */
3037     0,                                          /* tp_getattr */
3038     0,                                          /* tp_setattr */
3039     0,                                          /* tp_as_async */
3040     (reprfunc)list_repr,                        /* tp_repr */
3041     0,                                          /* tp_as_number */
3042     &list_as_sequence,                          /* tp_as_sequence */
3043     &list_as_mapping,                           /* tp_as_mapping */
3044     PyObject_HashNotImplemented,                /* tp_hash */
3045     0,                                          /* tp_call */
3046     0,                                          /* tp_str */
3047     PyObject_GenericGetAttr,                    /* tp_getattro */
3048     0,                                          /* tp_setattro */
3049     0,                                          /* tp_as_buffer */
3050     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3051         Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
3052     list___init____doc__,                       /* tp_doc */
3053     (traverseproc)list_traverse,                /* tp_traverse */
3054     (inquiry)_list_clear,                       /* tp_clear */
3055     list_richcompare,                           /* tp_richcompare */
3056     0,                                          /* tp_weaklistoffset */
3057     list_iter,                                  /* tp_iter */
3058     0,                                          /* tp_iternext */
3059     list_methods,                               /* tp_methods */
3060     0,                                          /* tp_members */
3061     0,                                          /* tp_getset */
3062     0,                                          /* tp_base */
3063     0,                                          /* tp_dict */
3064     0,                                          /* tp_descr_get */
3065     0,                                          /* tp_descr_set */
3066     0,                                          /* tp_dictoffset */
3067     (initproc)list___init__,                    /* tp_init */
3068     PyType_GenericAlloc,                        /* tp_alloc */
3069     PyType_GenericNew,                          /* tp_new */
3070     PyObject_GC_Del,                            /* tp_free */
3071 };
3072 
3073 /*********************** List Iterator **************************/
3074 
3075 typedef struct {
3076     PyObject_HEAD
3077     Py_ssize_t it_index;
3078     PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
3079 } listiterobject;
3080 
3081 static void listiter_dealloc(listiterobject *);
3082 static int listiter_traverse(listiterobject *, visitproc, void *);
3083 static PyObject *listiter_next(listiterobject *);
3084 static PyObject *listiter_len(listiterobject *, PyObject *);
3085 static PyObject *listiter_reduce_general(void *_it, int forward);
3086 static PyObject *listiter_reduce(listiterobject *, PyObject *);
3087 static PyObject *listiter_setstate(listiterobject *, PyObject *state);
3088 
3089 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
3090 PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3091 PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
3092 
3093 static PyMethodDef listiter_methods[] = {
3094     {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
3095     {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
3096     {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
3097     {NULL,              NULL}           /* sentinel */
3098 };
3099 
3100 PyTypeObject PyListIter_Type = {
3101     PyVarObject_HEAD_INIT(&PyType_Type, 0)
3102     "list_iterator",                            /* tp_name */
3103     sizeof(listiterobject),                     /* tp_basicsize */
3104     0,                                          /* tp_itemsize */
3105     /* methods */
3106     (destructor)listiter_dealloc,               /* tp_dealloc */
3107     0,                                          /* tp_vectorcall_offset */
3108     0,                                          /* tp_getattr */
3109     0,                                          /* tp_setattr */
3110     0,                                          /* tp_as_async */
3111     0,                                          /* tp_repr */
3112     0,                                          /* tp_as_number */
3113     0,                                          /* tp_as_sequence */
3114     0,                                          /* tp_as_mapping */
3115     0,                                          /* tp_hash */
3116     0,                                          /* tp_call */
3117     0,                                          /* tp_str */
3118     PyObject_GenericGetAttr,                    /* tp_getattro */
3119     0,                                          /* tp_setattro */
3120     0,                                          /* tp_as_buffer */
3121     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3122     0,                                          /* tp_doc */
3123     (traverseproc)listiter_traverse,            /* tp_traverse */
3124     0,                                          /* tp_clear */
3125     0,                                          /* tp_richcompare */
3126     0,                                          /* tp_weaklistoffset */
3127     PyObject_SelfIter,                          /* tp_iter */
3128     (iternextfunc)listiter_next,                /* tp_iternext */
3129     listiter_methods,                           /* tp_methods */
3130     0,                                          /* tp_members */
3131 };
3132 
3133 
3134 static PyObject *
list_iter(PyObject * seq)3135 list_iter(PyObject *seq)
3136 {
3137     listiterobject *it;
3138 
3139     if (!PyList_Check(seq)) {
3140         PyErr_BadInternalCall();
3141         return NULL;
3142     }
3143     it = PyObject_GC_New(listiterobject, &PyListIter_Type);
3144     if (it == NULL)
3145         return NULL;
3146     it->it_index = 0;
3147     Py_INCREF(seq);
3148     it->it_seq = (PyListObject *)seq;
3149     _PyObject_GC_TRACK(it);
3150     return (PyObject *)it;
3151 }
3152 
3153 static void
listiter_dealloc(listiterobject * it)3154 listiter_dealloc(listiterobject *it)
3155 {
3156     _PyObject_GC_UNTRACK(it);
3157     Py_XDECREF(it->it_seq);
3158     PyObject_GC_Del(it);
3159 }
3160 
3161 static int
listiter_traverse(listiterobject * it,visitproc visit,void * arg)3162 listiter_traverse(listiterobject *it, visitproc visit, void *arg)
3163 {
3164     Py_VISIT(it->it_seq);
3165     return 0;
3166 }
3167 
3168 static PyObject *
listiter_next(listiterobject * it)3169 listiter_next(listiterobject *it)
3170 {
3171     PyListObject *seq;
3172     PyObject *item;
3173 
3174     assert(it != NULL);
3175     seq = it->it_seq;
3176     if (seq == NULL)
3177         return NULL;
3178     assert(PyList_Check(seq));
3179 
3180     if (it->it_index < PyList_GET_SIZE(seq)) {
3181         item = PyList_GET_ITEM(seq, it->it_index);
3182         ++it->it_index;
3183         Py_INCREF(item);
3184         return item;
3185     }
3186 
3187     it->it_seq = NULL;
3188     Py_DECREF(seq);
3189     return NULL;
3190 }
3191 
3192 static PyObject *
listiter_len(listiterobject * it,PyObject * Py_UNUSED (ignored))3193 listiter_len(listiterobject *it, PyObject *Py_UNUSED(ignored))
3194 {
3195     Py_ssize_t len;
3196     if (it->it_seq) {
3197         len = PyList_GET_SIZE(it->it_seq) - it->it_index;
3198         if (len >= 0)
3199             return PyLong_FromSsize_t(len);
3200     }
3201     return PyLong_FromLong(0);
3202 }
3203 
3204 static PyObject *
listiter_reduce(listiterobject * it,PyObject * Py_UNUSED (ignored))3205 listiter_reduce(listiterobject *it, PyObject *Py_UNUSED(ignored))
3206 {
3207     return listiter_reduce_general(it, 1);
3208 }
3209 
3210 static PyObject *
listiter_setstate(listiterobject * it,PyObject * state)3211 listiter_setstate(listiterobject *it, PyObject *state)
3212 {
3213     Py_ssize_t index = PyLong_AsSsize_t(state);
3214     if (index == -1 && PyErr_Occurred())
3215         return NULL;
3216     if (it->it_seq != NULL) {
3217         if (index < 0)
3218             index = 0;
3219         else if (index > PyList_GET_SIZE(it->it_seq))
3220             index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
3221         it->it_index = index;
3222     }
3223     Py_RETURN_NONE;
3224 }
3225 
3226 /*********************** List Reverse Iterator **************************/
3227 
3228 typedef struct {
3229     PyObject_HEAD
3230     Py_ssize_t it_index;
3231     PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
3232 } listreviterobject;
3233 
3234 static void listreviter_dealloc(listreviterobject *);
3235 static int listreviter_traverse(listreviterobject *, visitproc, void *);
3236 static PyObject *listreviter_next(listreviterobject *);
3237 static PyObject *listreviter_len(listreviterobject *, PyObject *);
3238 static PyObject *listreviter_reduce(listreviterobject *, PyObject *);
3239 static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
3240 
3241 static PyMethodDef listreviter_methods[] = {
3242     {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
3243     {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
3244     {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
3245     {NULL,              NULL}           /* sentinel */
3246 };
3247 
3248 PyTypeObject PyListRevIter_Type = {
3249     PyVarObject_HEAD_INIT(&PyType_Type, 0)
3250     "list_reverseiterator",                     /* tp_name */
3251     sizeof(listreviterobject),                  /* tp_basicsize */
3252     0,                                          /* tp_itemsize */
3253     /* methods */
3254     (destructor)listreviter_dealloc,            /* tp_dealloc */
3255     0,                                          /* tp_vectorcall_offset */
3256     0,                                          /* tp_getattr */
3257     0,                                          /* tp_setattr */
3258     0,                                          /* tp_as_async */
3259     0,                                          /* tp_repr */
3260     0,                                          /* tp_as_number */
3261     0,                                          /* tp_as_sequence */
3262     0,                                          /* tp_as_mapping */
3263     0,                                          /* tp_hash */
3264     0,                                          /* tp_call */
3265     0,                                          /* tp_str */
3266     PyObject_GenericGetAttr,                    /* tp_getattro */
3267     0,                                          /* tp_setattro */
3268     0,                                          /* tp_as_buffer */
3269     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3270     0,                                          /* tp_doc */
3271     (traverseproc)listreviter_traverse,         /* tp_traverse */
3272     0,                                          /* tp_clear */
3273     0,                                          /* tp_richcompare */
3274     0,                                          /* tp_weaklistoffset */
3275     PyObject_SelfIter,                          /* tp_iter */
3276     (iternextfunc)listreviter_next,             /* tp_iternext */
3277     listreviter_methods,                /* tp_methods */
3278     0,
3279 };
3280 
3281 /*[clinic input]
3282 list.__reversed__
3283 
3284 Return a reverse iterator over the list.
3285 [clinic start generated code]*/
3286 
3287 static PyObject *
list___reversed___impl(PyListObject * self)3288 list___reversed___impl(PyListObject *self)
3289 /*[clinic end generated code: output=b166f073208c888c input=eadb6e17f8a6a280]*/
3290 {
3291     listreviterobject *it;
3292 
3293     it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
3294     if (it == NULL)
3295         return NULL;
3296     assert(PyList_Check(self));
3297     it->it_index = PyList_GET_SIZE(self) - 1;
3298     Py_INCREF(self);
3299     it->it_seq = self;
3300     PyObject_GC_Track(it);
3301     return (PyObject *)it;
3302 }
3303 
3304 static void
listreviter_dealloc(listreviterobject * it)3305 listreviter_dealloc(listreviterobject *it)
3306 {
3307     PyObject_GC_UnTrack(it);
3308     Py_XDECREF(it->it_seq);
3309     PyObject_GC_Del(it);
3310 }
3311 
3312 static int
listreviter_traverse(listreviterobject * it,visitproc visit,void * arg)3313 listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
3314 {
3315     Py_VISIT(it->it_seq);
3316     return 0;
3317 }
3318 
3319 static PyObject *
listreviter_next(listreviterobject * it)3320 listreviter_next(listreviterobject *it)
3321 {
3322     PyObject *item;
3323     Py_ssize_t index;
3324     PyListObject *seq;
3325 
3326     assert(it != NULL);
3327     seq = it->it_seq;
3328     if (seq == NULL) {
3329         return NULL;
3330     }
3331     assert(PyList_Check(seq));
3332 
3333     index = it->it_index;
3334     if (index>=0 && index < PyList_GET_SIZE(seq)) {
3335         item = PyList_GET_ITEM(seq, index);
3336         it->it_index--;
3337         Py_INCREF(item);
3338         return item;
3339     }
3340     it->it_index = -1;
3341     it->it_seq = NULL;
3342     Py_DECREF(seq);
3343     return NULL;
3344 }
3345 
3346 static PyObject *
listreviter_len(listreviterobject * it,PyObject * Py_UNUSED (ignored))3347 listreviter_len(listreviterobject *it, PyObject *Py_UNUSED(ignored))
3348 {
3349     Py_ssize_t len = it->it_index + 1;
3350     if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
3351         len = 0;
3352     return PyLong_FromSsize_t(len);
3353 }
3354 
3355 static PyObject *
listreviter_reduce(listreviterobject * it,PyObject * Py_UNUSED (ignored))3356 listreviter_reduce(listreviterobject *it, PyObject *Py_UNUSED(ignored))
3357 {
3358     return listiter_reduce_general(it, 0);
3359 }
3360 
3361 static PyObject *
listreviter_setstate(listreviterobject * it,PyObject * state)3362 listreviter_setstate(listreviterobject *it, PyObject *state)
3363 {
3364     Py_ssize_t index = PyLong_AsSsize_t(state);
3365     if (index == -1 && PyErr_Occurred())
3366         return NULL;
3367     if (it->it_seq != NULL) {
3368         if (index < -1)
3369             index = -1;
3370         else if (index > PyList_GET_SIZE(it->it_seq) - 1)
3371             index = PyList_GET_SIZE(it->it_seq) - 1;
3372         it->it_index = index;
3373     }
3374     Py_RETURN_NONE;
3375 }
3376 
3377 /* common pickling support */
3378 
3379 static PyObject *
listiter_reduce_general(void * _it,int forward)3380 listiter_reduce_general(void *_it, int forward)
3381 {
3382     _Py_IDENTIFIER(iter);
3383     _Py_IDENTIFIER(reversed);
3384     PyObject *list;
3385 
3386     /* the objects are not the same, index is of different types! */
3387     if (forward) {
3388         listiterobject *it = (listiterobject *)_it;
3389         if (it->it_seq)
3390             return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
3391                                  it->it_seq, it->it_index);
3392     } else {
3393         listreviterobject *it = (listreviterobject *)_it;
3394         if (it->it_seq)
3395             return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_reversed),
3396                                  it->it_seq, it->it_index);
3397     }
3398     /* empty iterator, create an empty list */
3399     list = PyList_New(0);
3400     if (list == NULL)
3401         return NULL;
3402     return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
3403 }
3404