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