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