blob: 2c07ceb0d412cef3c9267779c4b4c610501d7e55 [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* List object implementation */
2
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003#include "Python.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +01004#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01005#include "pycore_pystate.h"
Sergey Fedoseev234531b2019-02-25 21:59:12 +05006#include "pycore_tupleobject.h"
Victor Stinnere281f7d2018-11-01 02:30:36 +01007#include "pycore_accu.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00008
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00009#ifdef STDC_HEADERS
10#include <stddef.h>
11#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000012#include <sys/types.h> /* For size_t */
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000013#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014
Serhiy Storchakafdd42c42017-03-11 09:19:20 +020015/*[clinic input]
16class 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
Tim Peters8d9eb102004-07-31 02:24:20 +000022/* Ensure ob_item has room for at least newsize elements, and set
23 * ob_size to newsize. If newsize > ob_size on entry, the content
24 * of the new slots at exit is undefined heap trash; it's the caller's
Ezio Melotti13925002011-03-16 11:05:33 +020025 * responsibility to overwrite them with sane values.
Tim Peters8d9eb102004-07-31 02:24:20 +000026 * The number of allocated elements may grow, shrink, or stay the same.
27 * Failure is impossible if newsize <= self.allocated on entry, although
28 * that partly relies on an assumption that the system realloc() never
29 * fails when passed a number of bytes <= the number of bytes last
30 * allocated (the C standard doesn't guarantee this, but it's hard to
31 * imagine a realloc implementation where it wouldn't be true).
32 * Note that self->ob_item may change, and even if newsize is less
33 * than ob_size on entry.
34 */
Guido van Rossuma46d51d1995-01-26 22:59:43 +000035static int
Martin v. Löwis18e16552006-02-15 17:27:45 +000036list_resize(PyListObject *self, Py_ssize_t newsize)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 PyObject **items;
Xiang Zhang4cee0492017-02-22 12:32:30 +080039 size_t new_allocated, num_allocated_bytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 Py_ssize_t allocated = self->allocated;
Tim Peters65b8b842001-05-26 05:28:40 +000041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 /* Bypass realloc() when a previous overallocation is large enough
43 to accommodate the newsize. If the newsize falls lower than half
44 the allocated size, then proceed with the realloc() to shrink the list.
45 */
46 if (allocated >= newsize && newsize >= (allocated >> 1)) {
47 assert(self->ob_item != NULL || newsize == 0);
48 Py_SIZE(self) = newsize;
49 return 0;
50 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +000051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 /* This over-allocates proportional to the list size, making room
53 * for additional growth. The over-allocation is mild, but is
54 * enough to give linear-time amortized behavior over a long
55 * sequence of appends() in the presence of a poorly-performing
56 * system realloc().
57 * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
Xiang Zhang4cee0492017-02-22 12:32:30 +080058 * Note: new_allocated won't overflow because the largest possible value
59 * is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 */
Xiang Zhang4cee0492017-02-22 12:32:30 +080061 new_allocated = (size_t)newsize + (newsize >> 3) + (newsize < 9 ? 3 : 6);
62 if (new_allocated > (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 PyErr_NoMemory();
64 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 if (newsize == 0)
68 new_allocated = 0;
Xiang Zhang4cee0492017-02-22 12:32:30 +080069 num_allocated_bytes = new_allocated * sizeof(PyObject *);
70 items = (PyObject **)PyMem_Realloc(self->ob_item, num_allocated_bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 if (items == NULL) {
72 PyErr_NoMemory();
73 return -1;
74 }
75 self->ob_item = items;
76 Py_SIZE(self) = newsize;
77 self->allocated = new_allocated;
78 return 0;
Raymond Hettinger4bb95402004-02-13 11:36:39 +000079}
Guido van Rossuma46d51d1995-01-26 22:59:43 +000080
Pablo Galindo372d7052018-10-28 20:16:26 +000081static int
82list_preallocate_exact(PyListObject *self, Py_ssize_t size)
83{
84 assert(self->ob_item == NULL);
Sergey Fedoseev0e5f7712018-12-30 03:31:36 +050085 assert(size > 0);
Pablo Galindo372d7052018-10-28 20:16:26 +000086
Sergey Fedoseev0e5f7712018-12-30 03:31:36 +050087 PyObject **items = PyMem_New(PyObject*, size);
Pablo Galindo372d7052018-10-28 20:16:26 +000088 if (items == NULL) {
89 PyErr_NoMemory();
90 return -1;
91 }
92 self->ob_item = items;
Sergey Fedoseev0e5f7712018-12-30 03:31:36 +050093 self->allocated = size;
Pablo Galindo372d7052018-10-28 20:16:26 +000094 return 0;
95}
96
Christian Heimes77c02eb2008-02-09 02:18:51 +000097/* Debug statistic to compare allocations with reuse through the free list */
98#undef SHOW_ALLOC_COUNT
99#ifdef SHOW_ALLOC_COUNT
100static size_t count_alloc = 0;
101static size_t count_reuse = 0;
102
103static void
104show_alloc(void)
105{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200106 PyInterpreterState *interp = _PyInterpreterState_Get();
Victor Stinner331a6a52019-05-27 16:39:22 +0200107 if (!interp->config.show_alloc_count) {
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +0300108 return;
Victor Stinner25420fe2017-11-20 18:12:22 -0800109 }
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +0300110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
112 count_alloc);
113 fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T
114 "d\n", count_reuse);
115 fprintf(stderr, "%.2f%% reuse rate\n\n",
116 (100.0*count_reuse/(count_alloc+count_reuse)));
Christian Heimes77c02eb2008-02-09 02:18:51 +0000117}
118#endif
119
Raymond Hettinger0468e412004-05-05 05:37:53 +0000120/* Empty list reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000121#ifndef PyList_MAXFREELIST
122#define PyList_MAXFREELIST 80
123#endif
124static PyListObject *free_list[PyList_MAXFREELIST];
125static int numfree = 0;
Raymond Hettinger0468e412004-05-05 05:37:53 +0000126
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100127int
128PyList_ClearFreeList(void)
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 PyListObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100131 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 while (numfree) {
133 op = free_list[--numfree];
134 assert(PyList_CheckExact(op));
135 PyObject_GC_Del(op);
136 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100137 return ret;
138}
139
140void
Victor Stinnerbed48172019-08-27 00:12:32 +0200141_PyList_Fini(void)
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100142{
143 PyList_ClearFreeList();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000144}
145
David Malcolm49526f42012-06-22 14:55:41 -0400146/* Print summary info about the state of the optimized allocator */
147void
148_PyList_DebugMallocStats(FILE *out)
149{
150 _PyDebugAllocatorStats(out,
151 "free PyListObject",
152 numfree, sizeof(PyListObject));
153}
154
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000155PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000156PyList_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 PyListObject *op;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000159#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 static int initialized = 0;
161 if (!initialized) {
162 Py_AtExit(show_alloc);
163 initialized = 1;
164 }
Christian Heimes77c02eb2008-02-09 02:18:51 +0000165#endif
Tim Peters3986d4e2004-07-29 02:28:42 +0000166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 if (size < 0) {
168 PyErr_BadInternalCall();
169 return NULL;
170 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 if (numfree) {
172 numfree--;
173 op = free_list[numfree];
174 _Py_NewReference((PyObject *)op);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000175#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 count_reuse++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000177#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 } else {
179 op = PyObject_GC_New(PyListObject, &PyList_Type);
180 if (op == NULL)
181 return NULL;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000182#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 count_alloc++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000184#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 }
186 if (size <= 0)
187 op->ob_item = NULL;
188 else {
Mark Dickinson5d132382016-08-21 08:55:15 +0100189 op->ob_item = (PyObject **) PyMem_Calloc(size, sizeof(PyObject *));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 if (op->ob_item == NULL) {
191 Py_DECREF(op);
192 return PyErr_NoMemory();
193 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 }
195 Py_SIZE(op) = size;
196 op->allocated = size;
197 _PyObject_GC_TRACK(op);
198 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000199}
200
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500201static PyObject *
202list_new_prealloc(Py_ssize_t size)
203{
204 PyListObject *op = (PyListObject *) PyList_New(0);
205 if (size == 0 || op == NULL) {
206 return (PyObject *) op;
207 }
208 assert(op->ob_item == NULL);
209 op->ob_item = PyMem_New(PyObject *, size);
210 if (op->ob_item == NULL) {
211 Py_DECREF(op);
212 return PyErr_NoMemory();
213 }
214 op->allocated = size;
215 return (PyObject *) op;
216}
217
Martin v. Löwis18e16552006-02-15 17:27:45 +0000218Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000219PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 if (!PyList_Check(op)) {
222 PyErr_BadInternalCall();
223 return -1;
224 }
225 else
226 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000227}
228
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700229static inline int
230valid_index(Py_ssize_t i, Py_ssize_t limit)
231{
232 /* The cast to size_t lets us use just a single comparison
233 to check whether i is in the range: 0 <= i < limit.
234
235 See: Section 14.2 "Bounds Checking" in the Agner Fog
236 optimization manual found at:
237 https://www.agner.org/optimize/optimizing_cpp.pdf
238 */
239 return (size_t) i < (size_t) limit;
240}
241
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000242static PyObject *indexerr = NULL;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000243
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000244PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000245PyList_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 if (!PyList_Check(op)) {
248 PyErr_BadInternalCall();
249 return NULL;
250 }
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700251 if (!valid_index(i, Py_SIZE(op))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 if (indexerr == NULL) {
253 indexerr = PyUnicode_FromString(
254 "list index out of range");
255 if (indexerr == NULL)
256 return NULL;
257 }
258 PyErr_SetObject(PyExc_IndexError, indexerr);
259 return NULL;
260 }
261 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262}
263
264int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200265PyList_SetItem(PyObject *op, Py_ssize_t i,
266 PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200268 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 if (!PyList_Check(op)) {
270 Py_XDECREF(newitem);
271 PyErr_BadInternalCall();
272 return -1;
273 }
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700274 if (!valid_index(i, Py_SIZE(op))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 Py_XDECREF(newitem);
276 PyErr_SetString(PyExc_IndexError,
277 "list assignment index out of range");
278 return -1;
279 }
280 p = ((PyListObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300281 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000283}
284
285static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000286ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 Py_ssize_t i, n = Py_SIZE(self);
289 PyObject **items;
290 if (v == NULL) {
291 PyErr_BadInternalCall();
292 return -1;
293 }
294 if (n == PY_SSIZE_T_MAX) {
295 PyErr_SetString(PyExc_OverflowError,
296 "cannot add more objects to list");
297 return -1;
298 }
Tim Petersb38e2b62004-07-29 02:29:26 +0000299
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800300 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 return -1;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 if (where < 0) {
304 where += n;
305 if (where < 0)
306 where = 0;
307 }
308 if (where > n)
309 where = n;
310 items = self->ob_item;
311 for (i = n; --i >= where; )
312 items[i+1] = items[i];
313 Py_INCREF(v);
314 items[where] = v;
315 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000316}
317
318int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000319PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 if (!PyList_Check(op)) {
322 PyErr_BadInternalCall();
323 return -1;
324 }
325 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326}
327
Raymond Hettinger40a03822004-04-12 13:05:09 +0000328static int
329app1(PyListObject *self, PyObject *v)
330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 Py_ssize_t n = PyList_GET_SIZE(self);
Raymond Hettinger40a03822004-04-12 13:05:09 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 assert (v != NULL);
334 if (n == PY_SSIZE_T_MAX) {
335 PyErr_SetString(PyExc_OverflowError,
336 "cannot add more objects to list");
337 return -1;
338 }
Raymond Hettinger40a03822004-04-12 13:05:09 +0000339
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800340 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 return -1;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 Py_INCREF(v);
344 PyList_SET_ITEM(self, n, v);
345 return 0;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000346}
347
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000348int
Fred Drakea2f55112000-07-09 15:16:51 +0000349PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 if (PyList_Check(op) && (newitem != NULL))
352 return app1((PyListObject *)op, newitem);
353 PyErr_BadInternalCall();
354 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355}
356
357/* Methods */
358
359static void
Fred Drakea2f55112000-07-09 15:16:51 +0000360list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 Py_ssize_t i;
363 PyObject_GC_UnTrack(op);
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200364 Py_TRASHCAN_BEGIN(op, list_dealloc)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 if (op->ob_item != NULL) {
366 /* Do it backwards, for Christian Tismer.
367 There's a simple test case where somehow this reduces
368 thrashing when a *very* large list is created and
369 immediately deleted. */
370 i = Py_SIZE(op);
371 while (--i >= 0) {
372 Py_XDECREF(op->ob_item[i]);
373 }
374 PyMem_FREE(op->ob_item);
375 }
376 if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
377 free_list[numfree++] = op;
378 else
379 Py_TYPE(op)->tp_free((PyObject *)op);
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200380 Py_TRASHCAN_END
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381}
382
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000383static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000384list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 Py_ssize_t i;
Victor Stinner5c733472013-11-18 21:11:57 +0100387 PyObject *s;
Victor Stinner5c733472013-11-18 21:11:57 +0100388 _PyUnicodeWriter writer;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200389
390 if (Py_SIZE(v) == 0) {
391 return PyUnicode_FromString("[]");
392 }
393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 i = Py_ReprEnter((PyObject*)v);
395 if (i != 0) {
396 return i > 0 ? PyUnicode_FromString("[...]") : NULL;
397 }
Tim Petersa7259592001-06-16 05:11:17 +0000398
Victor Stinner5c733472013-11-18 21:11:57 +0100399 _PyUnicodeWriter_Init(&writer);
400 writer.overallocate = 1;
Victor Stinnerb8fb1972013-11-18 22:15:44 +0100401 /* "[" + "1" + ", 2" * (len - 1) + "]" */
402 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +0000403
Victor Stinner5c733472013-11-18 21:11:57 +0100404 if (_PyUnicodeWriter_WriteChar(&writer, '[') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200405 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 /* Do repr() on each element. Note that this may mutate the list,
408 so must refetch the list size on each iteration. */
409 for (i = 0; i < Py_SIZE(v); ++i) {
Victor Stinner5c733472013-11-18 21:11:57 +0100410 if (i > 0) {
Victor Stinner4a587072013-11-19 12:54:53 +0100411 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
Victor Stinner5c733472013-11-18 21:11:57 +0100412 goto error;
413 }
414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 s = PyObject_Repr(v->ob_item[i]);
Victor Stinner5c733472013-11-18 21:11:57 +0100416 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200417 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100418
419 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
420 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200421 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100422 }
423 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 }
Victor Stinner5c733472013-11-18 21:11:57 +0100425
Victor Stinner4d3f1092013-11-19 12:09:00 +0100426 writer.overallocate = 0;
Victor Stinner5c733472013-11-18 21:11:57 +0100427 if (_PyUnicodeWriter_WriteChar(&writer, ']') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200428 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 Py_ReprLeave((PyObject *)v);
Victor Stinner5c733472013-11-18 21:11:57 +0100431 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200432
433error:
Victor Stinner5c733472013-11-18 21:11:57 +0100434 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200435 Py_ReprLeave((PyObject *)v);
436 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437}
438
Martin v. Löwis18e16552006-02-15 17:27:45 +0000439static Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000440list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443}
444
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000445static int
Fred Drakea2f55112000-07-09 15:16:51 +0000446list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000447{
Dong-hee Na4dbf2d82020-01-28 00:02:23 +0900448 PyObject *item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 Py_ssize_t i;
450 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000451
Dong-hee Na4dbf2d82020-01-28 00:02:23 +0900452 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i) {
453 item = PyList_GET_ITEM(a, i);
454 Py_INCREF(item);
Dong-hee Na9e1ed512020-01-28 02:04:25 +0900455 cmp = PyObject_RichCompareBool(item, el, Py_EQ);
Dong-hee Na4dbf2d82020-01-28 00:02:23 +0900456 Py_DECREF(item);
457 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000459}
460
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000461static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000462list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000463{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700464 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 if (indexerr == NULL) {
466 indexerr = PyUnicode_FromString(
467 "list index out of range");
468 if (indexerr == NULL)
469 return NULL;
470 }
471 PyErr_SetObject(PyExc_IndexError, indexerr);
472 return NULL;
473 }
474 Py_INCREF(a->ob_item[i]);
475 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000476}
477
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000478static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000479list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 PyListObject *np;
482 PyObject **src, **dest;
483 Py_ssize_t i, len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 len = ihigh - ilow;
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500485 np = (PyListObject *) list_new_prealloc(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 if (np == NULL)
487 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 src = a->ob_item + ilow;
490 dest = np->ob_item;
491 for (i = 0; i < len; i++) {
492 PyObject *v = src[i];
493 Py_INCREF(v);
494 dest[i] = v;
495 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500496 Py_SIZE(np) = len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000498}
499
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000500PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000501PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 if (!PyList_Check(a)) {
504 PyErr_BadInternalCall();
505 return NULL;
506 }
Sergey Fedoseevef1b88b2019-02-21 11:51:52 +0500507 if (ilow < 0) {
508 ilow = 0;
509 }
510 else if (ilow > Py_SIZE(a)) {
511 ilow = Py_SIZE(a);
512 }
513 if (ihigh < ilow) {
514 ihigh = ilow;
515 }
516 else if (ihigh > Py_SIZE(a)) {
517 ihigh = Py_SIZE(a);
518 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000520}
521
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000522static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000523list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 Py_ssize_t size;
526 Py_ssize_t i;
527 PyObject **src, **dest;
528 PyListObject *np;
529 if (!PyList_Check(bb)) {
530 PyErr_Format(PyExc_TypeError,
531 "can only concatenate list (not \"%.200s\") to list",
532 bb->ob_type->tp_name);
533 return NULL;
534 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000535#define b ((PyListObject *)bb)
Martin Panterb93d8632016-07-25 02:39:20 +0000536 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000538 size = Py_SIZE(a) + Py_SIZE(b);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500539 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 if (np == NULL) {
541 return NULL;
542 }
543 src = a->ob_item;
544 dest = np->ob_item;
545 for (i = 0; i < Py_SIZE(a); i++) {
546 PyObject *v = src[i];
547 Py_INCREF(v);
548 dest[i] = v;
549 }
550 src = b->ob_item;
551 dest = np->ob_item + Py_SIZE(a);
552 for (i = 0; i < Py_SIZE(b); i++) {
553 PyObject *v = src[i];
554 Py_INCREF(v);
555 dest[i] = v;
556 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500557 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000559#undef b
560}
561
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000562static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000563list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000564{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 Py_ssize_t i, j;
566 Py_ssize_t size;
567 PyListObject *np;
568 PyObject **p, **items;
569 PyObject *elem;
570 if (n < 0)
571 n = 0;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100572 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 return PyErr_NoMemory();
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100574 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 if (size == 0)
576 return PyList_New(0);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500577 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 if (np == NULL)
579 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 if (Py_SIZE(a) == 1) {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500582 items = np->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 elem = a->ob_item[0];
584 for (i = 0; i < n; i++) {
585 items[i] = elem;
586 Py_INCREF(elem);
587 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500589 else {
590 p = np->ob_item;
591 items = a->ob_item;
592 for (i = 0; i < n; i++) {
593 for (j = 0; j < Py_SIZE(a); j++) {
594 *p = items[j];
595 Py_INCREF(*p);
596 p++;
597 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 }
599 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500600 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000602}
603
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000604static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200605_list_clear(PyListObject *a)
Armin Rigo93677f02004-07-29 12:40:23 +0000606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 Py_ssize_t i;
608 PyObject **item = a->ob_item;
609 if (item != NULL) {
610 /* Because XDECREF can recursively invoke operations on
611 this list, we make it empty first. */
612 i = Py_SIZE(a);
613 Py_SIZE(a) = 0;
614 a->ob_item = NULL;
615 a->allocated = 0;
616 while (--i >= 0) {
617 Py_XDECREF(item[i]);
618 }
619 PyMem_FREE(item);
620 }
621 /* Never fails; the return value can be ignored.
622 Note that there is no guarantee that the list is actually empty
623 at this point, because XDECREF may have populated it again! */
624 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000625}
626
Tim Peters8fc4a912004-07-31 21:53:19 +0000627/* a[ilow:ihigh] = v if v != NULL.
628 * del a[ilow:ihigh] if v == NULL.
629 *
630 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
631 * guaranteed the call cannot fail.
632 */
Armin Rigo93677f02004-07-29 12:40:23 +0000633static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000634list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 /* Because [X]DECREF can recursively invoke list operations on
637 this list, we must postpone all [X]DECREF activity until
638 after the list is back in its canonical shape. Therefore
639 we must allocate an additional array, 'recycle', into which
640 we temporarily copy the items that are deleted from the
641 list. :-( */
642 PyObject *recycle_on_stack[8];
643 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
644 PyObject **item;
645 PyObject **vitem = NULL;
646 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
647 Py_ssize_t n; /* # of elements in replacement list */
648 Py_ssize_t norig; /* # of elements in list getting replaced */
649 Py_ssize_t d; /* Change in size */
650 Py_ssize_t k;
651 size_t s;
652 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000653#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 if (v == NULL)
655 n = 0;
656 else {
657 if (a == b) {
658 /* Special case "a[i:j] = a" -- copy b first */
659 v = list_slice(b, 0, Py_SIZE(b));
660 if (v == NULL)
661 return result;
662 result = list_ass_slice(a, ilow, ihigh, v);
663 Py_DECREF(v);
664 return result;
665 }
666 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
667 if(v_as_SF == NULL)
668 goto Error;
669 n = PySequence_Fast_GET_SIZE(v_as_SF);
670 vitem = PySequence_Fast_ITEMS(v_as_SF);
671 }
672 if (ilow < 0)
673 ilow = 0;
674 else if (ilow > Py_SIZE(a))
675 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 if (ihigh < ilow)
678 ihigh = ilow;
679 else if (ihigh > Py_SIZE(a))
680 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 norig = ihigh - ilow;
683 assert(norig >= 0);
684 d = n - norig;
685 if (Py_SIZE(a) + d == 0) {
686 Py_XDECREF(v_as_SF);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200687 return _list_clear(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 }
689 item = a->ob_item;
690 /* recycle the items that we are about to remove */
691 s = norig * sizeof(PyObject *);
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700692 /* If norig == 0, item might be NULL, in which case we may not memcpy from it. */
693 if (s) {
694 if (s > sizeof(recycle_on_stack)) {
695 recycle = (PyObject **)PyMem_MALLOC(s);
696 if (recycle == NULL) {
697 PyErr_NoMemory();
698 goto Error;
699 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 }
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700701 memcpy(recycle, &item[ilow], s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 }
Tim Peters8d9eb102004-07-31 02:24:20 +0000703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 if (d < 0) { /* Delete -d items */
Victor Stinner2c40f642013-07-19 23:06:21 +0200705 Py_ssize_t tail;
706 tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
707 memmove(&item[ihigh+d], &item[ihigh], tail);
708 if (list_resize(a, Py_SIZE(a) + d) < 0) {
709 memmove(&item[ihigh], &item[ihigh+d], tail);
710 memcpy(&item[ilow], recycle, s);
711 goto Error;
712 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 item = a->ob_item;
714 }
715 else if (d > 0) { /* Insert d items */
716 k = Py_SIZE(a);
717 if (list_resize(a, k+d) < 0)
718 goto Error;
719 item = a->ob_item;
720 memmove(&item[ihigh+d], &item[ihigh],
721 (k - ihigh)*sizeof(PyObject *));
722 }
723 for (k = 0; k < n; k++, ilow++) {
724 PyObject *w = vitem[k];
725 Py_XINCREF(w);
726 item[ilow] = w;
727 }
728 for (k = norig - 1; k >= 0; --k)
729 Py_XDECREF(recycle[k]);
730 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000731 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 if (recycle != recycle_on_stack)
733 PyMem_FREE(recycle);
734 Py_XDECREF(v_as_SF);
735 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000736#undef b
737}
738
Guido van Rossum234f9421993-06-17 12:35:49 +0000739int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000740PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 if (!PyList_Check(a)) {
743 PyErr_BadInternalCall();
744 return -1;
745 }
746 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000747}
748
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000749static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000750list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 PyObject **items;
753 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000754
755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 size = PyList_GET_SIZE(self);
757 if (size == 0 || n == 1) {
758 Py_INCREF(self);
759 return (PyObject *)self;
760 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 if (n < 1) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200763 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 Py_INCREF(self);
765 return (PyObject *)self;
766 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 if (size > PY_SSIZE_T_MAX / n) {
769 return PyErr_NoMemory();
770 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000771
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800772 if (list_resize(self, size*n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 p = size;
776 items = self->ob_item;
777 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
778 for (j = 0; j < size; j++) {
779 PyObject *o = items[j];
780 Py_INCREF(o);
781 items[p++] = o;
782 }
783 }
784 Py_INCREF(self);
785 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000786}
787
Guido van Rossum4a450d01991-04-03 19:05:18 +0000788static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000789list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000790{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700791 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 PyErr_SetString(PyExc_IndexError,
793 "list assignment index out of range");
794 return -1;
795 }
796 if (v == NULL)
797 return list_ass_slice(a, i, i+1, v);
798 Py_INCREF(v);
Serhiy Storchakaf01e4082016-04-10 18:12:01 +0300799 Py_SETREF(a->ob_item[i], v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000801}
802
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200803/*[clinic input]
804list.insert
805
806 index: Py_ssize_t
807 object: object
808 /
809
810Insert object before index.
811[clinic start generated code]*/
812
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000813static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200814list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object)
815/*[clinic end generated code: output=7f35e32f60c8cb78 input=858514cf894c7eab]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000816{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200817 if (ins1(self, index, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 Py_RETURN_NONE;
819 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000820}
821
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200822/*[clinic input]
823list.clear
824
825Remove all items from list.
826[clinic start generated code]*/
827
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000828static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200829list_clear_impl(PyListObject *self)
830/*[clinic end generated code: output=67a1896c01f74362 input=ca3c1646856742f6]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000831{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200832 _list_clear(self);
Eli Benderskycbbaa962011-02-25 05:47:53 +0000833 Py_RETURN_NONE;
834}
835
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200836/*[clinic input]
837list.copy
838
839Return a shallow copy of the list.
840[clinic start generated code]*/
841
Eli Benderskycbbaa962011-02-25 05:47:53 +0000842static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200843list_copy_impl(PyListObject *self)
844/*[clinic end generated code: output=ec6b72d6209d418e input=6453ab159e84771f]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000845{
846 return list_slice(self, 0, Py_SIZE(self));
847}
848
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200849/*[clinic input]
850list.append
851
852 object: object
853 /
854
855Append object to the end of the list.
856[clinic start generated code]*/
857
Eli Benderskycbbaa962011-02-25 05:47:53 +0000858static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200859list_append(PyListObject *self, PyObject *object)
860/*[clinic end generated code: output=7c096003a29c0eae input=43a3fe48a7066e91]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000861{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200862 if (app1(self, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 Py_RETURN_NONE;
864 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000865}
866
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200867/*[clinic input]
868list.extend
869
870 iterable: object
871 /
872
873Extend list by appending elements from the iterable.
874[clinic start generated code]*/
875
Barry Warsawdedf6d61998-10-09 16:37:25 +0000876static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200877list_extend(PyListObject *self, PyObject *iterable)
878/*[clinic end generated code: output=630fb3bca0c8e789 input=9ec5ba3a81be3a4d]*/
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 PyObject *it; /* iter(v) */
881 Py_ssize_t m; /* size of self */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200882 Py_ssize_t n; /* guess for size of iterable */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 Py_ssize_t mn; /* m + n */
884 Py_ssize_t i;
885 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 /* Special cases:
888 1) lists and tuples which can use PySequence_Fast ops
889 2) extending self to self requires making a copy first
890 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200891 if (PyList_CheckExact(iterable) || PyTuple_CheckExact(iterable) ||
892 (PyObject *)self == iterable) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 PyObject **src, **dest;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200894 iterable = PySequence_Fast(iterable, "argument must be iterable");
895 if (!iterable)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200897 n = PySequence_Fast_GET_SIZE(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 if (n == 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200899 /* short circuit when iterable is empty */
900 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 Py_RETURN_NONE;
902 }
903 m = Py_SIZE(self);
Martin Panter94b39ce2017-01-14 06:30:37 +0000904 /* It should not be possible to allocate a list large enough to cause
905 an overflow on any relevant platform */
906 assert(m < PY_SSIZE_T_MAX - n);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800907 if (list_resize(self, m + n) < 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200908 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 return NULL;
910 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200911 /* note that we may still have self == iterable here for the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 * situation a.extend(a), but the following code works
913 * in that case too. Just make sure to resize self
914 * before calling PySequence_Fast_ITEMS.
915 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200916 /* populate the end of self with iterable's items */
917 src = PySequence_Fast_ITEMS(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 dest = self->ob_item + m;
919 for (i = 0; i < n; i++) {
920 PyObject *o = src[i];
921 Py_INCREF(o);
922 dest[i] = o;
923 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200924 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 Py_RETURN_NONE;
926 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000927
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200928 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 if (it == NULL)
930 return NULL;
931 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 /* Guess a result list size. */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200934 n = PyObject_LengthHint(iterable, 8);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800935 if (n < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 Py_DECREF(it);
937 return NULL;
938 }
939 m = Py_SIZE(self);
Martin Panterb93d8632016-07-25 02:39:20 +0000940 if (m > PY_SSIZE_T_MAX - n) {
941 /* m + n overflowed; on the chance that n lied, and there really
942 * is enough room, ignore it. If n was telling the truth, we'll
943 * eventually run out of memory during the loop.
944 */
945 }
946 else {
947 mn = m + n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 /* Make room. */
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800949 if (list_resize(self, mn) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 goto error;
951 /* Make the list sane again. */
952 Py_SIZE(self) = m;
953 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 /* Run iterator to exhaustion. */
956 for (;;) {
957 PyObject *item = iternext(it);
958 if (item == NULL) {
959 if (PyErr_Occurred()) {
960 if (PyErr_ExceptionMatches(PyExc_StopIteration))
961 PyErr_Clear();
962 else
963 goto error;
964 }
965 break;
966 }
967 if (Py_SIZE(self) < self->allocated) {
968 /* steals ref */
969 PyList_SET_ITEM(self, Py_SIZE(self), item);
970 ++Py_SIZE(self);
971 }
972 else {
973 int status = app1(self, item);
974 Py_DECREF(item); /* append creates a new ref */
975 if (status < 0)
976 goto error;
977 }
978 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 /* Cut back result list if initial guess was too large. */
Victor Stinner32fd6ea2013-07-16 21:45:58 +0200981 if (Py_SIZE(self) < self->allocated) {
982 if (list_resize(self, Py_SIZE(self)) < 0)
983 goto error;
984 }
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 Py_DECREF(it);
987 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000988
989 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 Py_DECREF(it);
991 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000992}
993
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000994PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200995_PyList_Extend(PyListObject *self, PyObject *iterable)
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000996{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200997 return list_extend(self, iterable);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000998}
999
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001000static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001001list_inplace_concat(PyListObject *self, PyObject *other)
1002{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001004
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001005 result = list_extend(self, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 if (result == NULL)
1007 return result;
1008 Py_DECREF(result);
1009 Py_INCREF(self);
1010 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001011}
1012
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001013/*[clinic input]
1014list.pop
1015
1016 index: Py_ssize_t = -1
1017 /
1018
1019Remove and return item at index (default last).
1020
1021Raises IndexError if list is empty or index is out of range.
1022[clinic start generated code]*/
1023
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001024static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001025list_pop_impl(PyListObject *self, Py_ssize_t index)
1026/*[clinic end generated code: output=6bd69dcb3f17eca8 input=b83675976f329e6f]*/
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 PyObject *v;
1029 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +00001030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 if (Py_SIZE(self) == 0) {
1032 /* Special-case most common failure cause */
1033 PyErr_SetString(PyExc_IndexError, "pop from empty list");
1034 return NULL;
1035 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001036 if (index < 0)
1037 index += Py_SIZE(self);
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -07001038 if (!valid_index(index, Py_SIZE(self))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1040 return NULL;
1041 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001042 v = self->ob_item[index];
1043 if (index == Py_SIZE(self) - 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 status = list_resize(self, Py_SIZE(self) - 1);
Victor Stinnerb27cd3e2013-07-08 22:20:44 +02001045 if (status >= 0)
1046 return v; /* and v now owns the reference the list had */
1047 else
1048 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 }
1050 Py_INCREF(v);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001051 status = list_ass_slice(self, index, index+1, (PyObject *)NULL);
Victor Stinner095d99f2013-07-17 21:58:01 +02001052 if (status < 0) {
1053 Py_DECREF(v);
1054 return NULL;
1055 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001057}
1058
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001059/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
1060static void
1061reverse_slice(PyObject **lo, PyObject **hi)
1062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 --hi;
1066 while (lo < hi) {
1067 PyObject *t = *lo;
1068 *lo = *hi;
1069 *hi = t;
1070 ++lo;
1071 --hi;
1072 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001073}
1074
Tim Petersa64dc242002-08-01 02:13:36 +00001075/* Lots of code for an adaptive, stable, natural mergesort. There are many
1076 * pieces to this algorithm; read listsort.txt for overviews and details.
1077 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001078
Daniel Stutzbach98338222010-12-02 21:55:33 +00001079/* A sortslice contains a pointer to an array of keys and a pointer to
1080 * an array of corresponding values. In other words, keys[i]
1081 * corresponds with values[i]. If values == NULL, then the keys are
1082 * also the values.
1083 *
1084 * Several convenience routines are provided here, so that keys and
1085 * values are always moved in sync.
1086 */
1087
1088typedef struct {
1089 PyObject **keys;
1090 PyObject **values;
1091} sortslice;
1092
1093Py_LOCAL_INLINE(void)
1094sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
1095{
1096 s1->keys[i] = s2->keys[j];
1097 if (s1->values != NULL)
1098 s1->values[i] = s2->values[j];
1099}
1100
1101Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001102sortslice_copy_incr(sortslice *dst, sortslice *src)
1103{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001104 *dst->keys++ = *src->keys++;
1105 if (dst->values != NULL)
1106 *dst->values++ = *src->values++;
1107}
1108
1109Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001110sortslice_copy_decr(sortslice *dst, sortslice *src)
1111{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001112 *dst->keys-- = *src->keys--;
1113 if (dst->values != NULL)
1114 *dst->values-- = *src->values--;
1115}
1116
1117
1118Py_LOCAL_INLINE(void)
1119sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001120 Py_ssize_t n)
1121{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001122 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1123 if (s1->values != NULL)
1124 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1125}
1126
1127Py_LOCAL_INLINE(void)
1128sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001129 Py_ssize_t n)
1130{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001131 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1132 if (s1->values != NULL)
1133 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1134}
1135
1136Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001137sortslice_advance(sortslice *slice, Py_ssize_t n)
1138{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001139 slice->keys += n;
1140 if (slice->values != NULL)
1141 slice->values += n;
1142}
1143
embg1e34da42018-01-28 20:03:23 -07001144/* Comparison function: ms->key_compare, which is set at run-time in
1145 * listsort_impl to optimize for various special cases.
Tim Petersa64dc242002-08-01 02:13:36 +00001146 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1147 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001148
embg1e34da42018-01-28 20:03:23 -07001149#define ISLT(X, Y) (*(ms->key_compare))(X, Y, ms)
Tim Peters66860f62002-08-04 17:47:26 +00001150
1151/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001152 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1153 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1154*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001155#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001157
embg1e34da42018-01-28 20:03:23 -07001158/* The maximum number of entries in a MergeState's pending-runs stack.
1159 * This is enough to sort arrays of size up to about
1160 * 32 * phi ** MAX_MERGE_PENDING
1161 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1162 * with 2**64 elements.
1163 */
1164#define MAX_MERGE_PENDING 85
1165
1166/* When we get into galloping mode, we stay there until both runs win less
1167 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
1168 */
1169#define MIN_GALLOP 7
1170
1171/* Avoid malloc for small temp arrays. */
1172#define MERGESTATE_TEMP_SIZE 256
1173
1174/* One MergeState exists on the stack per invocation of mergesort. It's just
1175 * a convenient way to pass state around among the helper functions.
1176 */
1177struct s_slice {
1178 sortslice base;
1179 Py_ssize_t len;
1180};
1181
1182typedef struct s_MergeState MergeState;
1183struct s_MergeState {
1184 /* This controls when we get *into* galloping mode. It's initialized
1185 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1186 * random data, and lower for highly structured data.
1187 */
1188 Py_ssize_t min_gallop;
1189
1190 /* 'a' is temp storage to help with merges. It contains room for
1191 * alloced entries.
1192 */
1193 sortslice a; /* may point to temparray below */
1194 Py_ssize_t alloced;
1195
1196 /* A stack of n pending runs yet to be merged. Run #i starts at
1197 * address base[i] and extends for len[i] elements. It's always
1198 * true (so long as the indices are in bounds) that
1199 *
1200 * pending[i].base + pending[i].len == pending[i+1].base
1201 *
1202 * so we could cut the storage for this, but it's a minor amount,
1203 * and keeping all the info explicit simplifies the code.
1204 */
1205 int n;
1206 struct s_slice pending[MAX_MERGE_PENDING];
1207
1208 /* 'a' points to this when possible, rather than muck with malloc. */
1209 PyObject *temparray[MERGESTATE_TEMP_SIZE];
1210
1211 /* This is the function we will use to compare two keys,
1212 * even when none of our special cases apply and we have to use
1213 * safe_object_compare. */
1214 int (*key_compare)(PyObject *, PyObject *, MergeState *);
1215
1216 /* This function is used by unsafe_object_compare to optimize comparisons
1217 * when we know our list is type-homogeneous but we can't assume anything else.
1218 * In the pre-sort check it is set equal to key->ob_type->tp_richcompare */
1219 PyObject *(*key_richcompare)(PyObject *, PyObject *, int);
1220
1221 /* This function is used by unsafe_tuple_compare to compare the first elements
1222 * of tuples. It may be set to safe_object_compare, but the idea is that hopefully
1223 * we can assume more, and use one of the special-case compares. */
1224 int (*tuple_elem_compare)(PyObject *, PyObject *, MergeState *);
1225};
1226
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001227/* binarysort is the best method for sorting small arrays: it does
1228 few compares, but can do data movement quadratic in the number of
1229 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001230 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001231 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001232 On entry, must have lo <= start <= hi, and that [lo, start) is already
1233 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001234 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001235 Even in case of error, the output slice will be some permutation of
1236 the input (nothing is lost or duplicated).
1237*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001238static int
embg1e34da42018-01-28 20:03:23 -07001239binarysort(MergeState *ms, sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001240{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001241 Py_ssize_t k;
1242 PyObject **l, **p, **r;
1243 PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001244
Daniel Stutzbach98338222010-12-02 21:55:33 +00001245 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001247 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 ++start;
1249 for (; start < hi; ++start) {
1250 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001251 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 r = start;
1253 pivot = *r;
1254 /* Invariants:
1255 * pivot >= all in [lo, l).
1256 * pivot < all in [r, start).
1257 * The second is vacuously true at the start.
1258 */
1259 assert(l < r);
1260 do {
1261 p = l + ((r - l) >> 1);
1262 IFLT(pivot, *p)
1263 r = p;
1264 else
1265 l = p+1;
1266 } while (l < r);
1267 assert(l == r);
1268 /* The invariants still hold, so pivot >= all in [lo, l) and
1269 pivot < all in [l, start), so pivot belongs at l. Note
1270 that if there are elements equal to pivot, l points to the
1271 first slot after them -- that's why this sort is stable.
1272 Slide over to make room.
1273 Caution: using memmove is much slower under MSVC 5;
1274 we're not usually moving many slots. */
1275 for (p = start; p > l; --p)
1276 *p = *(p-1);
1277 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001278 if (lo.values != NULL) {
1279 Py_ssize_t offset = lo.values - lo.keys;
1280 p = start + offset;
1281 pivot = *p;
1282 l += offset;
1283 for (p = start + offset; p > l; --p)
1284 *p = *(p-1);
1285 *l = pivot;
1286 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 }
1288 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001289
1290 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001292}
1293
Tim Petersa64dc242002-08-01 02:13:36 +00001294/*
1295Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1296is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001297
Tim Petersa64dc242002-08-01 02:13:36 +00001298 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001299
Tim Petersa64dc242002-08-01 02:13:36 +00001300or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001301
Tim Petersa64dc242002-08-01 02:13:36 +00001302 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001303
Tim Petersa64dc242002-08-01 02:13:36 +00001304Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1305For its intended use in a stable mergesort, the strictness of the defn of
1306"descending" is needed so that the caller can safely reverse a descending
1307sequence without violating stability (strict > ensures there are no equal
1308elements to get out of order).
1309
1310Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001311*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001312static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001313count_run(MergeState *ms, PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 Py_ssize_t k;
1316 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 assert(lo < hi);
1319 *descending = 0;
1320 ++lo;
1321 if (lo == hi)
1322 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 n = 2;
1325 IFLT(*lo, *(lo-1)) {
1326 *descending = 1;
1327 for (lo = lo+1; lo < hi; ++lo, ++n) {
1328 IFLT(*lo, *(lo-1))
1329 ;
1330 else
1331 break;
1332 }
1333 }
1334 else {
1335 for (lo = lo+1; lo < hi; ++lo, ++n) {
1336 IFLT(*lo, *(lo-1))
1337 break;
1338 }
1339 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001342fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001344}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001345
Tim Petersa64dc242002-08-01 02:13:36 +00001346/*
1347Locate the proper position of key in a sorted vector; if the vector contains
1348an element equal to key, return the position immediately to the left of
1349the leftmost equal element. [gallop_right() does the same except returns
1350the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001351
Tim Petersa64dc242002-08-01 02:13:36 +00001352"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001353
Tim Petersa64dc242002-08-01 02:13:36 +00001354"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1355hint is to the final result, the faster this runs.
1356
1357The return value is the int k in 0..n such that
1358
1359 a[k-1] < key <= a[k]
1360
1361pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1362key belongs at index k; or, IOW, the first k elements of a should precede
1363key, and the last n-k should follow key.
1364
1365Returns -1 on error. See listsort.txt for info on the method.
1366*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001367static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001368gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 Py_ssize_t ofs;
1371 Py_ssize_t lastofs;
1372 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 a += hint;
1377 lastofs = 0;
1378 ofs = 1;
1379 IFLT(*a, key) {
1380 /* a[hint] < key -- gallop right, until
1381 * a[hint + lastofs] < key <= a[hint + ofs]
1382 */
1383 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1384 while (ofs < maxofs) {
1385 IFLT(a[ofs], key) {
1386 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001387 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 }
1390 else /* key <= a[hint + ofs] */
1391 break;
1392 }
1393 if (ofs > maxofs)
1394 ofs = maxofs;
1395 /* Translate back to offsets relative to &a[0]. */
1396 lastofs += hint;
1397 ofs += hint;
1398 }
1399 else {
1400 /* key <= a[hint] -- gallop left, until
1401 * a[hint - ofs] < key <= a[hint - lastofs]
1402 */
1403 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1404 while (ofs < maxofs) {
1405 IFLT(*(a-ofs), key)
1406 break;
1407 /* key <= a[hint - ofs] */
1408 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001409 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 }
1412 if (ofs > maxofs)
1413 ofs = maxofs;
1414 /* Translate back to positive offsets relative to &a[0]. */
1415 k = lastofs;
1416 lastofs = hint - ofs;
1417 ofs = hint - k;
1418 }
1419 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1422 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1423 * right of lastofs but no farther right than ofs. Do a binary
1424 * search, with invariant a[lastofs-1] < key <= a[ofs].
1425 */
1426 ++lastofs;
1427 while (lastofs < ofs) {
1428 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 IFLT(a[m], key)
1431 lastofs = m+1; /* a[m] < key */
1432 else
1433 ofs = m; /* key <= a[m] */
1434 }
1435 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1436 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001437
1438fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001440}
1441
1442/*
1443Exactly like gallop_left(), except that if key already exists in a[0:n],
1444finds the position immediately to the right of the rightmost equal value.
1445
1446The return value is the int k in 0..n such that
1447
1448 a[k-1] <= key < a[k]
1449
1450or -1 if error.
1451
1452The code duplication is massive, but this is enough different given that
1453we're sticking to "<" comparisons that it's much harder to follow if
1454written as one routine with yet another "left or right?" flag.
1455*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001456static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001457gallop_right(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 Py_ssize_t ofs;
1460 Py_ssize_t lastofs;
1461 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 a += hint;
1466 lastofs = 0;
1467 ofs = 1;
1468 IFLT(key, *a) {
1469 /* key < a[hint] -- gallop left, until
1470 * a[hint - ofs] <= key < a[hint - lastofs]
1471 */
1472 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1473 while (ofs < maxofs) {
1474 IFLT(key, *(a-ofs)) {
1475 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001476 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 }
1479 else /* a[hint - ofs] <= key */
1480 break;
1481 }
1482 if (ofs > maxofs)
1483 ofs = maxofs;
1484 /* Translate back to positive offsets relative to &a[0]. */
1485 k = lastofs;
1486 lastofs = hint - ofs;
1487 ofs = hint - k;
1488 }
1489 else {
1490 /* a[hint] <= key -- gallop right, until
1491 * a[hint + lastofs] <= key < a[hint + ofs]
1492 */
1493 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1494 while (ofs < maxofs) {
1495 IFLT(key, a[ofs])
1496 break;
1497 /* a[hint + ofs] <= key */
1498 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001499 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 }
1502 if (ofs > maxofs)
1503 ofs = maxofs;
1504 /* Translate back to offsets relative to &a[0]. */
1505 lastofs += hint;
1506 ofs += hint;
1507 }
1508 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1511 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1512 * right of lastofs but no farther right than ofs. Do a binary
1513 * search, with invariant a[lastofs-1] <= key < a[ofs].
1514 */
1515 ++lastofs;
1516 while (lastofs < ofs) {
1517 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 IFLT(key, a[m])
1520 ofs = m; /* key < a[m] */
1521 else
1522 lastofs = m+1; /* a[m] <= key */
1523 }
1524 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1525 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001526
1527fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001529}
1530
Tim Petersa64dc242002-08-01 02:13:36 +00001531/* Conceptually a MergeState's constructor. */
1532static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001533merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001536 if (has_keyfunc) {
1537 /* The temporary space for merging will need at most half the list
1538 * size rounded up. Use the minimum possible space so we can use the
1539 * rest of temparray for other things. In particular, if there is
1540 * enough extra space, listsort() will use it to store the keys.
1541 */
1542 ms->alloced = (list_size + 1) / 2;
1543
1544 /* ms->alloced describes how many keys will be stored at
1545 ms->temparray, but we also need to store the values. Hence,
1546 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1547 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1548 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1549 ms->a.values = &ms->temparray[ms->alloced];
1550 }
1551 else {
1552 ms->alloced = MERGESTATE_TEMP_SIZE;
1553 ms->a.values = NULL;
1554 }
1555 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 ms->n = 0;
1557 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001558}
1559
1560/* Free all the temp memory owned by the MergeState. This must be called
1561 * when you're done with a MergeState, and may be called before then if
1562 * you want to free the temp memory early.
1563 */
1564static void
1565merge_freemem(MergeState *ms)
1566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001568 if (ms->a.keys != ms->temparray)
1569 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001570}
1571
1572/* Ensure enough temp memory for 'need' array slots is available.
1573 * Returns 0 on success and -1 if the memory can't be gotten.
1574 */
1575static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001576merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001577{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001578 int multiplier;
1579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 assert(ms != NULL);
1581 if (need <= ms->alloced)
1582 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001583
1584 multiplier = ms->a.values != NULL ? 2 : 1;
1585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 /* Don't realloc! That can cost cycles to copy the old data, but
1587 * we don't care what's in the block.
1588 */
1589 merge_freemem(ms);
embg1e34da42018-01-28 20:03:23 -07001590 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject *) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 PyErr_NoMemory();
1592 return -1;
1593 }
embg1e34da42018-01-28 20:03:23 -07001594 ms->a.keys = (PyObject **)PyMem_Malloc(multiplier * need
Daniel Stutzbach98338222010-12-02 21:55:33 +00001595 * sizeof(PyObject *));
1596 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001598 if (ms->a.values != NULL)
1599 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 return 0;
1601 }
1602 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001604}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1606 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001607
Daniel Stutzbach98338222010-12-02 21:55:33 +00001608/* Merge the na elements starting at ssa with the nb elements starting at
1609 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1610 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1611 * should have na <= nb. See listsort.txt for more info. Return 0 if
1612 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001613 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001614static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001615merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1616 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001619 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 int result = -1; /* guilty until proved innocent */
1621 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001622
Daniel Stutzbach98338222010-12-02 21:55:33 +00001623 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1624 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 if (MERGE_GETMEM(ms, na) < 0)
1626 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001627 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1628 dest = ssa;
1629 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001630
Daniel Stutzbach98338222010-12-02 21:55:33 +00001631 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 --nb;
1633 if (nb == 0)
1634 goto Succeed;
1635 if (na == 1)
1636 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 min_gallop = ms->min_gallop;
1639 for (;;) {
1640 Py_ssize_t acount = 0; /* # of times A won in a row */
1641 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 /* Do the straightforward thing until (if ever) one run
1644 * appears to win consistently.
1645 */
1646 for (;;) {
1647 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001648 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 if (k) {
1650 if (k < 0)
1651 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001652 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 ++bcount;
1654 acount = 0;
1655 --nb;
1656 if (nb == 0)
1657 goto Succeed;
1658 if (bcount >= min_gallop)
1659 break;
1660 }
1661 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001662 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 ++acount;
1664 bcount = 0;
1665 --na;
1666 if (na == 1)
1667 goto CopyB;
1668 if (acount >= min_gallop)
1669 break;
1670 }
1671 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 /* One run is winning so consistently that galloping may
1674 * be a huge win. So try that, and continue galloping until
1675 * (if ever) neither run appears to be winning consistently
1676 * anymore.
1677 */
1678 ++min_gallop;
1679 do {
1680 assert(na > 1 && nb > 0);
1681 min_gallop -= min_gallop > 1;
1682 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001683 k = gallop_right(ms, ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 acount = k;
1685 if (k) {
1686 if (k < 0)
1687 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001688 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1689 sortslice_advance(&dest, k);
1690 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 na -= k;
1692 if (na == 1)
1693 goto CopyB;
1694 /* na==0 is impossible now if the comparison
1695 * function is consistent, but we can't assume
1696 * that it is.
1697 */
1698 if (na == 0)
1699 goto Succeed;
1700 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001701 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 --nb;
1703 if (nb == 0)
1704 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001705
embg1e34da42018-01-28 20:03:23 -07001706 k = gallop_left(ms, ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 bcount = k;
1708 if (k) {
1709 if (k < 0)
1710 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001711 sortslice_memmove(&dest, 0, &ssb, 0, k);
1712 sortslice_advance(&dest, k);
1713 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 nb -= k;
1715 if (nb == 0)
1716 goto Succeed;
1717 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001718 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 --na;
1720 if (na == 1)
1721 goto CopyB;
1722 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1723 ++min_gallop; /* penalize it for leaving galloping mode */
1724 ms->min_gallop = min_gallop;
1725 }
Tim Petersa64dc242002-08-01 02:13:36 +00001726Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001728Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001730 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001732CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001734 /* The last element of ssa belongs at the end of the merge. */
1735 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1736 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001738}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001739
Daniel Stutzbach98338222010-12-02 21:55:33 +00001740/* Merge the na elements starting at pa with the nb elements starting at
1741 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1742 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1743 * should have na >= nb. See listsort.txt for more info. Return 0 if
1744 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001745 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001746static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001747merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1748 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001751 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001754
Daniel Stutzbach98338222010-12-02 21:55:33 +00001755 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1756 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 if (MERGE_GETMEM(ms, nb) < 0)
1758 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001759 dest = ssb;
1760 sortslice_advance(&dest, nb-1);
1761 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1762 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001764 ssb.keys = ms->a.keys + nb - 1;
1765 if (ssb.values != NULL)
1766 ssb.values = ms->a.values + nb - 1;
1767 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001768
Daniel Stutzbach98338222010-12-02 21:55:33 +00001769 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 --na;
1771 if (na == 0)
1772 goto Succeed;
1773 if (nb == 1)
1774 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 min_gallop = ms->min_gallop;
1777 for (;;) {
1778 Py_ssize_t acount = 0; /* # of times A won in a row */
1779 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 /* Do the straightforward thing until (if ever) one run
1782 * appears to win consistently.
1783 */
1784 for (;;) {
1785 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001786 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 if (k) {
1788 if (k < 0)
1789 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001790 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 ++acount;
1792 bcount = 0;
1793 --na;
1794 if (na == 0)
1795 goto Succeed;
1796 if (acount >= min_gallop)
1797 break;
1798 }
1799 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001800 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 ++bcount;
1802 acount = 0;
1803 --nb;
1804 if (nb == 1)
1805 goto CopyA;
1806 if (bcount >= min_gallop)
1807 break;
1808 }
1809 }
Tim Petersa64dc242002-08-01 02:13:36 +00001810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 /* One run is winning so consistently that galloping may
1812 * be a huge win. So try that, and continue galloping until
1813 * (if ever) neither run appears to be winning consistently
1814 * anymore.
1815 */
1816 ++min_gallop;
1817 do {
1818 assert(na > 0 && nb > 1);
1819 min_gallop -= min_gallop > 1;
1820 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001821 k = gallop_right(ms, ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 if (k < 0)
1823 goto Fail;
1824 k = na - k;
1825 acount = k;
1826 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001827 sortslice_advance(&dest, -k);
1828 sortslice_advance(&ssa, -k);
1829 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 na -= k;
1831 if (na == 0)
1832 goto Succeed;
1833 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001834 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 --nb;
1836 if (nb == 1)
1837 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001838
embg1e34da42018-01-28 20:03:23 -07001839 k = gallop_left(ms, ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 if (k < 0)
1841 goto Fail;
1842 k = nb - k;
1843 bcount = k;
1844 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001845 sortslice_advance(&dest, -k);
1846 sortslice_advance(&ssb, -k);
1847 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 nb -= k;
1849 if (nb == 1)
1850 goto CopyA;
1851 /* nb==0 is impossible now if the comparison
1852 * function is consistent, but we can't assume
1853 * that it is.
1854 */
1855 if (nb == 0)
1856 goto Succeed;
1857 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001858 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 --na;
1860 if (na == 0)
1861 goto Succeed;
1862 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1863 ++min_gallop; /* penalize it for leaving galloping mode */
1864 ms->min_gallop = min_gallop;
1865 }
Tim Petersa64dc242002-08-01 02:13:36 +00001866Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001868Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001870 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001872CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001874 /* The first element of ssb belongs at the front of the merge. */
1875 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1876 sortslice_advance(&dest, -na);
1877 sortslice_advance(&ssa, -na);
1878 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001880}
1881
1882/* Merge the two runs at stack indices i and i+1.
1883 * Returns 0 on success, -1 on error.
1884 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001885static Py_ssize_t
1886merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001887{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001888 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 Py_ssize_t na, nb;
1890 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 assert(ms != NULL);
1893 assert(ms->n >= 2);
1894 assert(i >= 0);
1895 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001896
Daniel Stutzbach98338222010-12-02 21:55:33 +00001897 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001899 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 nb = ms->pending[i+1].len;
1901 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001902 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 /* Record the length of the combined runs; if i is the 3rd-last
1905 * run now, also slide over the last run (which isn't involved
1906 * in this merge). The current run i+1 goes away in any case.
1907 */
1908 ms->pending[i].len = na + nb;
1909 if (i == ms->n - 3)
1910 ms->pending[i+1] = ms->pending[i+2];
1911 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 /* Where does b start in a? Elements in a before that can be
1914 * ignored (already in place).
1915 */
embg1e34da42018-01-28 20:03:23 -07001916 k = gallop_right(ms, *ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 if (k < 0)
1918 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001919 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 na -= k;
1921 if (na == 0)
1922 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 /* Where does a end in b? Elements in b after that can be
1925 * ignored (already in place).
1926 */
embg1e34da42018-01-28 20:03:23 -07001927 nb = gallop_left(ms, ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 if (nb <= 0)
1929 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 /* Merge what remains of the runs, using a temp array with
1932 * min(na, nb) elements.
1933 */
1934 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001935 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001937 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001938}
1939
1940/* Examine the stack of runs waiting to be merged, merging adjacent runs
1941 * until the stack invariants are re-established:
1942 *
1943 * 1. len[-3] > len[-2] + len[-1]
1944 * 2. len[-2] > len[-1]
1945 *
1946 * See listsort.txt for more info.
1947 *
1948 * Returns 0 on success, -1 on error.
1949 */
1950static int
1951merge_collapse(MergeState *ms)
1952{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 assert(ms);
1956 while (ms->n > 1) {
1957 Py_ssize_t n = ms->n - 2;
Benjamin Petersonb808d592015-02-25 10:12:26 -05001958 if ((n > 0 && p[n-1].len <= p[n].len + p[n+1].len) ||
1959 (n > 1 && p[n-2].len <= p[n-1].len + p[n].len)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 if (p[n-1].len < p[n+1].len)
1961 --n;
1962 if (merge_at(ms, n) < 0)
1963 return -1;
1964 }
1965 else if (p[n].len <= p[n+1].len) {
embg1e34da42018-01-28 20:03:23 -07001966 if (merge_at(ms, n) < 0)
1967 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 }
1969 else
1970 break;
1971 }
1972 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001973}
1974
1975/* Regardless of invariants, merge all runs on the stack until only one
1976 * remains. This is used at the end of the mergesort.
1977 *
1978 * Returns 0 on success, -1 on error.
1979 */
1980static int
1981merge_force_collapse(MergeState *ms)
1982{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 assert(ms);
1986 while (ms->n > 1) {
1987 Py_ssize_t n = ms->n - 2;
1988 if (n > 0 && p[n-1].len < p[n+1].len)
1989 --n;
1990 if (merge_at(ms, n) < 0)
1991 return -1;
1992 }
1993 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001994}
1995
1996/* Compute a good value for the minimum run length; natural runs shorter
1997 * than this are boosted artificially via binary insertion.
1998 *
1999 * If n < 64, return n (it's too small to bother with fancy stuff).
2000 * Else if n is an exact power of 2, return 32.
2001 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
2002 * strictly less than, an exact power of 2.
2003 *
2004 * See listsort.txt for more info.
2005 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002006static Py_ssize_t
2007merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00002008{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00002010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 assert(n >= 0);
2012 while (n >= 64) {
2013 r |= n & 1;
2014 n >>= 1;
2015 }
2016 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002017}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00002018
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002019static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00002020reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002021{
Daniel Stutzbach98338222010-12-02 21:55:33 +00002022 reverse_slice(s->keys, &s->keys[n]);
2023 if (s->values != NULL)
2024 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002025}
2026
embg1e34da42018-01-28 20:03:23 -07002027/* Here we define custom comparison functions to optimize for the cases one commonly
2028 * encounters in practice: homogeneous lists, often of one of the basic types. */
2029
2030/* This struct holds the comparison function and helper functions
2031 * selected in the pre-sort check. */
2032
2033/* These are the special case compare functions.
2034 * ms->key_compare will always point to one of these: */
2035
2036/* Heterogeneous compare: default, always safe to fall back on. */
2037static int
2038safe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2039{
2040 /* No assumptions necessary! */
2041 return PyObject_RichCompareBool(v, w, Py_LT);
2042}
2043
2044/* Homogeneous compare: safe for any two compareable objects of the same type.
2045 * (ms->key_richcompare is set to ob_type->tp_richcompare in the
2046 * pre-sort check.)
2047 */
2048static int
2049unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2050{
2051 PyObject *res_obj; int res;
2052
2053 /* No assumptions, because we check first: */
2054 if (v->ob_type->tp_richcompare != ms->key_richcompare)
2055 return PyObject_RichCompareBool(v, w, Py_LT);
2056
2057 assert(ms->key_richcompare != NULL);
2058 res_obj = (*(ms->key_richcompare))(v, w, Py_LT);
2059
2060 if (res_obj == Py_NotImplemented) {
2061 Py_DECREF(res_obj);
2062 return PyObject_RichCompareBool(v, w, Py_LT);
2063 }
2064 if (res_obj == NULL)
2065 return -1;
2066
2067 if (PyBool_Check(res_obj)) {
2068 res = (res_obj == Py_True);
2069 }
2070 else {
2071 res = PyObject_IsTrue(res_obj);
2072 }
2073 Py_DECREF(res_obj);
2074
2075 /* Note that we can't assert
2076 * res == PyObject_RichCompareBool(v, w, Py_LT);
2077 * because of evil compare functions like this:
2078 * lambda a, b: int(random.random() * 3) - 1)
2079 * (which is actually in test_sort.py) */
2080 return res;
2081}
2082
2083/* Latin string compare: safe for any two latin (one byte per char) strings. */
2084static int
2085unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
2086{
Victor Stinner8017b802018-01-29 13:47:06 +01002087 Py_ssize_t len;
2088 int res;
embg1e34da42018-01-28 20:03:23 -07002089
2090 /* Modified from Objects/unicodeobject.c:unicode_compare, assuming: */
2091 assert(v->ob_type == w->ob_type);
2092 assert(v->ob_type == &PyUnicode_Type);
2093 assert(PyUnicode_KIND(v) == PyUnicode_KIND(w));
2094 assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
2095
2096 len = Py_MIN(PyUnicode_GET_LENGTH(v), PyUnicode_GET_LENGTH(w));
2097 res = memcmp(PyUnicode_DATA(v), PyUnicode_DATA(w), len);
2098
2099 res = (res != 0 ?
2100 res < 0 :
2101 PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w));
2102
2103 assert(res == PyObject_RichCompareBool(v, w, Py_LT));;
2104 return res;
2105}
2106
2107/* Bounded int compare: compare any two longs that fit in a single machine word. */
2108static int
2109unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
2110{
2111 PyLongObject *vl, *wl; sdigit v0, w0; int res;
2112
2113 /* Modified from Objects/longobject.c:long_compare, assuming: */
2114 assert(v->ob_type == w->ob_type);
2115 assert(v->ob_type == &PyLong_Type);
2116 assert(Py_ABS(Py_SIZE(v)) <= 1);
2117 assert(Py_ABS(Py_SIZE(w)) <= 1);
2118
2119 vl = (PyLongObject*)v;
2120 wl = (PyLongObject*)w;
2121
2122 v0 = Py_SIZE(vl) == 0 ? 0 : (sdigit)vl->ob_digit[0];
2123 w0 = Py_SIZE(wl) == 0 ? 0 : (sdigit)wl->ob_digit[0];
2124
2125 if (Py_SIZE(vl) < 0)
2126 v0 = -v0;
2127 if (Py_SIZE(wl) < 0)
2128 w0 = -w0;
2129
2130 res = v0 < w0;
2131 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2132 return res;
2133}
2134
2135/* Float compare: compare any two floats. */
2136static int
2137unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms)
2138{
2139 int res;
2140
2141 /* Modified from Objects/floatobject.c:float_richcompare, assuming: */
2142 assert(v->ob_type == w->ob_type);
2143 assert(v->ob_type == &PyFloat_Type);
2144
2145 res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w);
2146 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2147 return res;
2148}
2149
2150/* Tuple compare: compare *any* two tuples, using
2151 * ms->tuple_elem_compare to compare the first elements, which is set
2152 * using the same pre-sort check as we use for ms->key_compare,
2153 * but run on the list [x[0] for x in L]. This allows us to optimize compares
2154 * on two levels (as long as [x[0] for x in L] is type-homogeneous.) The idea is
2155 * that most tuple compares don't involve x[1:]. */
2156static int
2157unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
2158{
2159 PyTupleObject *vt, *wt;
2160 Py_ssize_t i, vlen, wlen;
2161 int k;
2162
2163 /* Modified from Objects/tupleobject.c:tuplerichcompare, assuming: */
2164 assert(v->ob_type == w->ob_type);
2165 assert(v->ob_type == &PyTuple_Type);
2166 assert(Py_SIZE(v) > 0);
2167 assert(Py_SIZE(w) > 0);
2168
2169 vt = (PyTupleObject *)v;
2170 wt = (PyTupleObject *)w;
2171
2172 vlen = Py_SIZE(vt);
2173 wlen = Py_SIZE(wt);
2174
2175 for (i = 0; i < vlen && i < wlen; i++) {
2176 k = PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_EQ);
2177 if (k < 0)
2178 return -1;
2179 if (!k)
2180 break;
2181 }
2182
2183 if (i >= vlen || i >= wlen)
2184 return vlen < wlen;
2185
2186 if (i == 0)
2187 return ms->tuple_elem_compare(vt->ob_item[i], wt->ob_item[i], ms);
2188 else
2189 return PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_LT);
2190}
2191
Tim Petersa64dc242002-08-01 02:13:36 +00002192/* An adaptive, stable, natural mergesort. See listsort.txt.
2193 * Returns Py_None on success, NULL on error. Even in case of error, the
2194 * list will be some permutation of its input state (nothing is lost or
2195 * duplicated).
2196 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002197/*[clinic input]
2198list.sort
2199
2200 *
2201 key as keyfunc: object = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002202 reverse: bool(accept={int}) = False
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002203
Tim Hoffmann5c224762019-06-01 06:10:02 +02002204Sort the list in ascending order and return None.
2205
2206The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
2207order of two equal elements is maintained).
2208
2209If a key function is given, apply it once to each list item and sort them,
2210ascending or descending, according to their function values.
2211
2212The reverse flag can be set to sort in descending order.
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002213[clinic start generated code]*/
2214
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002215static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002216list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
Tim Hoffmann5c224762019-06-01 06:10:02 +02002217/*[clinic end generated code: output=57b9f9c5e23fbe42 input=cb56cd179a713060]*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00002218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 Py_ssize_t nremaining;
2221 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002222 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 Py_ssize_t saved_ob_size, saved_allocated;
2224 PyObject **saved_ob_item;
2225 PyObject **final_ob_item;
2226 PyObject *result = NULL; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 Py_ssize_t i;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002228 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 assert(self != NULL);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002231 assert(PyList_Check(self));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 if (keyfunc == Py_None)
2233 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 /* The list is temporarily made empty, so that mutations performed
2236 * by comparison functions can't affect the slice of memory we're
2237 * sorting (allowing mutations during sorting is a core-dump
2238 * factory, since ob_item may change).
2239 */
2240 saved_ob_size = Py_SIZE(self);
2241 saved_ob_item = self->ob_item;
2242 saved_allocated = self->allocated;
2243 Py_SIZE(self) = 0;
2244 self->ob_item = NULL;
2245 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00002246
Daniel Stutzbach98338222010-12-02 21:55:33 +00002247 if (keyfunc == NULL) {
2248 keys = NULL;
2249 lo.keys = saved_ob_item;
2250 lo.values = NULL;
2251 }
2252 else {
2253 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
2254 /* Leverage stack space we allocated but won't otherwise use */
2255 keys = &ms.temparray[saved_ob_size+1];
2256 else {
2257 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
Benjamin Peterson0823ffb2015-04-23 17:04:36 -04002258 if (keys == NULL) {
2259 PyErr_NoMemory();
2260 goto keyfunc_fail;
2261 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00002263
2264 for (i = 0; i < saved_ob_size ; i++) {
Jeroen Demeyer196a5302019-07-04 12:31:34 +02002265 keys[i] = _PyObject_CallOneArg(keyfunc, saved_ob_item[i]);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002266 if (keys[i] == NULL) {
2267 for (i=i-1 ; i>=0 ; i--)
2268 Py_DECREF(keys[i]);
Benjamin Peterson4a42cd42014-03-15 12:21:28 -05002269 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00002270 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002271 goto keyfunc_fail;
2272 }
2273 }
2274
2275 lo.keys = keys;
2276 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002278
embg1e34da42018-01-28 20:03:23 -07002279
2280 /* The pre-sort check: here's where we decide which compare function to use.
2281 * How much optimization is safe? We test for homogeneity with respect to
2282 * several properties that are expensive to check at compare-time, and
2283 * set ms appropriately. */
2284 if (saved_ob_size > 1) {
2285 /* Assume the first element is representative of the whole list. */
2286 int keys_are_in_tuples = (lo.keys[0]->ob_type == &PyTuple_Type &&
2287 Py_SIZE(lo.keys[0]) > 0);
2288
2289 PyTypeObject* key_type = (keys_are_in_tuples ?
2290 PyTuple_GET_ITEM(lo.keys[0], 0)->ob_type :
2291 lo.keys[0]->ob_type);
2292
2293 int keys_are_all_same_type = 1;
2294 int strings_are_latin = 1;
2295 int ints_are_bounded = 1;
2296
2297 /* Prove that assumption by checking every key. */
embg1e34da42018-01-28 20:03:23 -07002298 for (i=0; i < saved_ob_size; i++) {
2299
2300 if (keys_are_in_tuples &&
2301 !(lo.keys[i]->ob_type == &PyTuple_Type && Py_SIZE(lo.keys[i]) != 0)) {
2302 keys_are_in_tuples = 0;
2303 keys_are_all_same_type = 0;
2304 break;
2305 }
2306
2307 /* Note: for lists of tuples, key is the first element of the tuple
2308 * lo.keys[i], not lo.keys[i] itself! We verify type-homogeneity
2309 * for lists of tuples in the if-statement directly above. */
2310 PyObject *key = (keys_are_in_tuples ?
2311 PyTuple_GET_ITEM(lo.keys[i], 0) :
2312 lo.keys[i]);
2313
2314 if (key->ob_type != key_type) {
2315 keys_are_all_same_type = 0;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002316 /* If keys are in tuple we must loop over the whole list to make
2317 sure all items are tuples */
2318 if (!keys_are_in_tuples) {
2319 break;
2320 }
embg1e34da42018-01-28 20:03:23 -07002321 }
2322
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002323 if (keys_are_all_same_type) {
2324 if (key_type == &PyLong_Type &&
2325 ints_are_bounded &&
2326 Py_ABS(Py_SIZE(key)) > 1) {
2327
embg1e34da42018-01-28 20:03:23 -07002328 ints_are_bounded = 0;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002329 }
2330 else if (key_type == &PyUnicode_Type &&
2331 strings_are_latin &&
2332 PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND) {
2333
2334 strings_are_latin = 0;
2335 }
2336 }
embg1e34da42018-01-28 20:03:23 -07002337 }
embg1e34da42018-01-28 20:03:23 -07002338
2339 /* Choose the best compare, given what we now know about the keys. */
2340 if (keys_are_all_same_type) {
2341
2342 if (key_type == &PyUnicode_Type && strings_are_latin) {
2343 ms.key_compare = unsafe_latin_compare;
2344 }
2345 else if (key_type == &PyLong_Type && ints_are_bounded) {
2346 ms.key_compare = unsafe_long_compare;
2347 }
2348 else if (key_type == &PyFloat_Type) {
2349 ms.key_compare = unsafe_float_compare;
2350 }
2351 else if ((ms.key_richcompare = key_type->tp_richcompare) != NULL) {
2352 ms.key_compare = unsafe_object_compare;
2353 }
Zackery Spytzebc793d2019-02-21 00:47:14 -07002354 else {
2355 ms.key_compare = safe_object_compare;
2356 }
embg1e34da42018-01-28 20:03:23 -07002357 }
2358 else {
2359 ms.key_compare = safe_object_compare;
2360 }
2361
2362 if (keys_are_in_tuples) {
2363 /* Make sure we're not dealing with tuples of tuples
2364 * (remember: here, key_type refers list [key[0] for key in keys]) */
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002365 if (key_type == &PyTuple_Type) {
embg1e34da42018-01-28 20:03:23 -07002366 ms.tuple_elem_compare = safe_object_compare;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002367 }
2368 else {
embg1e34da42018-01-28 20:03:23 -07002369 ms.tuple_elem_compare = ms.key_compare;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002370 }
embg1e34da42018-01-28 20:03:23 -07002371
2372 ms.key_compare = unsafe_tuple_compare;
2373 }
2374 }
2375 /* End of pre-sort check: ms is now set properly! */
2376
Daniel Stutzbach98338222010-12-02 21:55:33 +00002377 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 nremaining = saved_ob_size;
2380 if (nremaining < 2)
2381 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00002382
Benjamin Peterson05380642010-08-23 19:35:39 +00002383 /* Reverse sort stability achieved by initially reversing the list,
2384 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002385 if (reverse) {
2386 if (keys != NULL)
2387 reverse_slice(&keys[0], &keys[saved_ob_size]);
2388 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
2389 }
Benjamin Peterson05380642010-08-23 19:35:39 +00002390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 /* March over the array once, left to right, finding natural runs,
2392 * and extending short natural runs to minrun elements.
2393 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 minrun = merge_compute_minrun(nremaining);
2395 do {
2396 int descending;
2397 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00002398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 /* Identify next run. */
embg1e34da42018-01-28 20:03:23 -07002400 n = count_run(&ms, lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 if (n < 0)
2402 goto fail;
2403 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002404 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 /* If short, extend to min(minrun, nremaining). */
2406 if (n < minrun) {
2407 const Py_ssize_t force = nremaining <= minrun ?
2408 nremaining : minrun;
embg1e34da42018-01-28 20:03:23 -07002409 if (binarysort(&ms, lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 goto fail;
2411 n = force;
2412 }
2413 /* Push run onto pending-runs stack, and maybe merge. */
2414 assert(ms.n < MAX_MERGE_PENDING);
2415 ms.pending[ms.n].base = lo;
2416 ms.pending[ms.n].len = n;
2417 ++ms.n;
2418 if (merge_collapse(&ms) < 0)
2419 goto fail;
2420 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002421 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 nremaining -= n;
2423 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 if (merge_force_collapse(&ms) < 0)
2426 goto fail;
2427 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002428 assert(keys == NULL
2429 ? ms.pending[0].base.keys == saved_ob_item
2430 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002432 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002433
2434succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002436fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002437 if (keys != NULL) {
2438 for (i = 0; i < saved_ob_size; i++)
2439 Py_DECREF(keys[i]);
Benjamin Petersonef87f8c2014-03-14 21:54:31 -05002440 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002441 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 if (self->allocated != -1 && result != NULL) {
2445 /* The user mucked with the list during the sort,
2446 * and we don't already have another error to report.
2447 */
2448 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2449 result = NULL;
2450 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 if (reverse && saved_ob_size > 1)
2453 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002456
Daniel Stutzbach98338222010-12-02 21:55:33 +00002457keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 final_ob_item = self->ob_item;
2459 i = Py_SIZE(self);
2460 Py_SIZE(self) = saved_ob_size;
2461 self->ob_item = saved_ob_item;
2462 self->allocated = saved_allocated;
2463 if (final_ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002464 /* we cannot use _list_clear() for this because it does not
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 guarantee that the list is really empty when it returns */
2466 while (--i >= 0) {
2467 Py_XDECREF(final_ob_item[i]);
2468 }
2469 PyMem_FREE(final_ob_item);
2470 }
2471 Py_XINCREF(result);
2472 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002473}
Tim Peters330f9e92002-07-19 07:05:44 +00002474#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002475#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002476
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002477int
Fred Drakea2f55112000-07-09 15:16:51 +00002478PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 if (v == NULL || !PyList_Check(v)) {
2481 PyErr_BadInternalCall();
2482 return -1;
2483 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002484 v = list_sort_impl((PyListObject *)v, NULL, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 if (v == NULL)
2486 return -1;
2487 Py_DECREF(v);
2488 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002489}
2490
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002491/*[clinic input]
2492list.reverse
2493
2494Reverse *IN PLACE*.
2495[clinic start generated code]*/
2496
Guido van Rossumb86c5492001-02-12 22:06:02 +00002497static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002498list_reverse_impl(PyListObject *self)
2499/*[clinic end generated code: output=482544fc451abea9 input=eefd4c3ae1bc9887]*/
Guido van Rossumb86c5492001-02-12 22:06:02 +00002500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002501 if (Py_SIZE(self) > 1)
2502 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2503 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002504}
2505
Guido van Rossum84c76f51990-10-30 13:32:20 +00002506int
Fred Drakea2f55112000-07-09 15:16:51 +00002507PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 if (v == NULL || !PyList_Check(v)) {
2512 PyErr_BadInternalCall();
2513 return -1;
2514 }
2515 if (Py_SIZE(self) > 1)
2516 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2517 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002518}
2519
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002520PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002521PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 if (v == NULL || !PyList_Check(v)) {
2524 PyErr_BadInternalCall();
2525 return NULL;
2526 }
Sergey Fedoseev234531b2019-02-25 21:59:12 +05002527 return _PyTuple_FromArray(((PyListObject *)v)->ob_item, Py_SIZE(v));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002528}
2529
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002530/*[clinic input]
2531list.index
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002532
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002533 value: object
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002534 start: slice_index(accept={int}) = 0
2535 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002536 /
2537
2538Return first index of value.
2539
2540Raises ValueError if the value is not present.
2541[clinic start generated code]*/
2542
2543static PyObject *
2544list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
2545 Py_ssize_t stop)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002546/*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002547{
2548 Py_ssize_t i;
2549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 if (start < 0) {
2551 start += Py_SIZE(self);
2552 if (start < 0)
2553 start = 0;
2554 }
2555 if (stop < 0) {
2556 stop += Py_SIZE(self);
2557 if (stop < 0)
2558 stop = 0;
2559 }
2560 for (i = start; i < stop && i < Py_SIZE(self); i++) {
Zackery Spytzd9e561d2019-12-30 12:32:58 -07002561 PyObject *obj = self->ob_item[i];
2562 Py_INCREF(obj);
2563 int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
2564 Py_DECREF(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 if (cmp > 0)
2566 return PyLong_FromSsize_t(i);
2567 else if (cmp < 0)
2568 return NULL;
2569 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002570 PyErr_Format(PyExc_ValueError, "%R is not in list", value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002571 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002572}
2573
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002574/*[clinic input]
2575list.count
2576
2577 value: object
2578 /
2579
2580Return number of occurrences of value.
2581[clinic start generated code]*/
2582
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002583static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002584list_count(PyListObject *self, PyObject *value)
2585/*[clinic end generated code: output=b1f5d284205ae714 input=3bdc3a5e6f749565]*/
Guido van Rossume6f7d181991-10-20 20:20:40 +00002586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 Py_ssize_t count = 0;
2588 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002590 for (i = 0; i < Py_SIZE(self); i++) {
Zackery Spytzd9e561d2019-12-30 12:32:58 -07002591 PyObject *obj = self->ob_item[i];
Dong-hee Na14d80d02020-01-23 02:36:54 +09002592 if (obj == value) {
2593 count++;
2594 continue;
2595 }
Zackery Spytzd9e561d2019-12-30 12:32:58 -07002596 Py_INCREF(obj);
2597 int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
2598 Py_DECREF(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002599 if (cmp > 0)
2600 count++;
2601 else if (cmp < 0)
2602 return NULL;
2603 }
2604 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002605}
2606
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002607/*[clinic input]
2608list.remove
2609
2610 value: object
2611 /
2612
2613Remove first occurrence of value.
2614
2615Raises ValueError if the value is not present.
2616[clinic start generated code]*/
2617
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002618static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002619list_remove(PyListObject *self, PyObject *value)
2620/*[clinic end generated code: output=f087e1951a5e30d1 input=2dc2ba5bb2fb1f82]*/
Guido van Rossumed98d481991-03-06 13:07:53 +00002621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002622 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002624 for (i = 0; i < Py_SIZE(self); i++) {
Zackery Spytzd9e561d2019-12-30 12:32:58 -07002625 PyObject *obj = self->ob_item[i];
2626 Py_INCREF(obj);
2627 int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
2628 Py_DECREF(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 if (cmp > 0) {
2630 if (list_ass_slice(self, i, i+1,
2631 (PyObject *)NULL) == 0)
2632 Py_RETURN_NONE;
2633 return NULL;
2634 }
2635 else if (cmp < 0)
2636 return NULL;
2637 }
2638 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2639 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002640}
2641
Jeremy Hylton8caad492000-06-23 14:18:11 +00002642static int
2643list_traverse(PyListObject *o, visitproc visit, void *arg)
2644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 for (i = Py_SIZE(o); --i >= 0; )
2648 Py_VISIT(o->ob_item[i]);
2649 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002650}
2651
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002652static PyObject *
2653list_richcompare(PyObject *v, PyObject *w, int op)
2654{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 PyListObject *vl, *wl;
2656 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002657
Brian Curtindfc80e32011-08-10 20:28:54 -05002658 if (!PyList_Check(v) || !PyList_Check(w))
2659 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 vl = (PyListObject *)v;
2662 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2665 /* Shortcut: if the lengths differ, the lists differ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 if (op == Py_EQ)
stratakise8b19652017-11-02 11:32:54 +01002667 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 else
stratakise8b19652017-11-02 11:32:54 +01002669 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 /* Search for the first index where items are different */
2673 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002674 PyObject *vitem = vl->ob_item[i];
2675 PyObject *witem = wl->ob_item[i];
Inada Naokidfef9862019-12-31 10:58:37 +09002676 if (vitem == witem) {
2677 continue;
2678 }
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002679
2680 Py_INCREF(vitem);
2681 Py_INCREF(witem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002682 int k = PyObject_RichCompareBool(vl->ob_item[i],
2683 wl->ob_item[i], Py_EQ);
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002684 Py_DECREF(vitem);
2685 Py_DECREF(witem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 if (k < 0)
2687 return NULL;
2688 if (!k)
2689 break;
2690 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002692 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2693 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +01002694 Py_RETURN_RICHCOMPARE(Py_SIZE(vl), Py_SIZE(wl), op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002695 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 /* We have an item that differs -- shortcuts for EQ/NE */
2698 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002699 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 }
2701 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002702 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 /* Compare the final item again using the proper operator */
2706 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002707}
2708
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002709/*[clinic input]
2710list.__init__
2711
2712 iterable: object(c_default="NULL") = ()
2713 /
2714
2715Built-in mutable sequence.
2716
2717If no argument is given, the constructor creates a new empty list.
2718The argument must be an iterable if specified.
2719[clinic start generated code]*/
2720
Tim Peters6d6c1a32001-08-02 04:15:00 +00002721static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002722list___init___impl(PyListObject *self, PyObject *iterable)
2723/*[clinic end generated code: output=0f3c21379d01de48 input=b3f3fe7206af8f6b]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +00002724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 /* Verify list invariants established by PyType_GenericAlloc() */
2726 assert(0 <= Py_SIZE(self));
2727 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2728 assert(self->ob_item != NULL ||
2729 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 /* Empty previous contents */
2732 if (self->ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002733 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002734 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002735 if (iterable != NULL) {
Pablo Galindo372d7052018-10-28 20:16:26 +00002736 if (_PyObject_HasLen(iterable)) {
2737 Py_ssize_t iter_len = PyObject_Size(iterable);
2738 if (iter_len == -1) {
2739 if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
2740 return -1;
2741 }
2742 PyErr_Clear();
2743 }
2744 if (iter_len > 0 && self->ob_item == NULL
2745 && list_preallocate_exact(self, iter_len)) {
2746 return -1;
2747 }
2748 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002749 PyObject *rv = list_extend(self, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 if (rv == NULL)
2751 return -1;
2752 Py_DECREF(rv);
2753 }
2754 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002755}
2756
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002757/*[clinic input]
2758list.__sizeof__
2759
2760Return the size of the list in memory, in bytes.
2761[clinic start generated code]*/
2762
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002763static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002764list___sizeof___impl(PyListObject *self)
2765/*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002766{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002768
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002769 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002770 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002771}
2772
Raymond Hettinger1021c442003-11-07 15:38:09 +00002773static PyObject *list_iter(PyObject *seq);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002774static PyObject *list_subscript(PyListObject*, PyObject*);
2775
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002776static PyMethodDef list_methods[] = {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002777 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, "x.__getitem__(y) <==> x[y]"},
2778 LIST___REVERSED___METHODDEF
2779 LIST___SIZEOF___METHODDEF
2780 LIST_CLEAR_METHODDEF
2781 LIST_COPY_METHODDEF
2782 LIST_APPEND_METHODDEF
2783 LIST_INSERT_METHODDEF
2784 LIST_EXTEND_METHODDEF
2785 LIST_POP_METHODDEF
2786 LIST_REMOVE_METHODDEF
2787 LIST_INDEX_METHODDEF
2788 LIST_COUNT_METHODDEF
2789 LIST_REVERSE_METHODDEF
2790 LIST_SORT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002792};
2793
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002794static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002795 (lenfunc)list_length, /* sq_length */
2796 (binaryfunc)list_concat, /* sq_concat */
2797 (ssizeargfunc)list_repeat, /* sq_repeat */
2798 (ssizeargfunc)list_item, /* sq_item */
2799 0, /* sq_slice */
2800 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2801 0, /* sq_ass_slice */
2802 (objobjproc)list_contains, /* sq_contains */
2803 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2804 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002805};
2806
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002807static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002808list_subscript(PyListObject* self, PyObject* item)
2809{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 if (PyIndex_Check(item)) {
2811 Py_ssize_t i;
2812 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2813 if (i == -1 && PyErr_Occurred())
2814 return NULL;
2815 if (i < 0)
2816 i += PyList_GET_SIZE(self);
2817 return list_item(self, i);
2818 }
2819 else if (PySlice_Check(item)) {
HongWeipeng3c87a662019-09-08 18:15:56 +08002820 Py_ssize_t start, stop, step, slicelength, i;
2821 size_t cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 PyObject* result;
2823 PyObject* it;
2824 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002825
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002826 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 return NULL;
2828 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002829 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2830 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 if (slicelength <= 0) {
2833 return PyList_New(0);
2834 }
2835 else if (step == 1) {
2836 return list_slice(self, start, stop);
2837 }
2838 else {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002839 result = list_new_prealloc(slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 src = self->ob_item;
2843 dest = ((PyListObject *)result)->ob_item;
2844 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002845 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 it = src[cur];
2847 Py_INCREF(it);
2848 dest[i] = it;
2849 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002850 Py_SIZE(result) = slicelength;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 return result;
2852 }
2853 }
2854 else {
2855 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002856 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 item->ob_type->tp_name);
2858 return NULL;
2859 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002860}
2861
Tim Peters3b01a122002-07-19 02:35:45 +00002862static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002863list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2864{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 if (PyIndex_Check(item)) {
2866 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2867 if (i == -1 && PyErr_Occurred())
2868 return -1;
2869 if (i < 0)
2870 i += PyList_GET_SIZE(self);
2871 return list_ass_item(self, i, value);
2872 }
2873 else if (PySlice_Check(item)) {
2874 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002875
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002876 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 return -1;
2878 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002879 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2880 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 if (step == 1)
2883 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 /* Make sure s[5:2] = [..] inserts at the right place:
2886 before 5, not before 2. */
2887 if ((step < 0 && start < stop) ||
2888 (step > 0 && start > stop))
2889 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 if (value == NULL) {
2892 /* delete slice */
2893 PyObject **garbage;
2894 size_t cur;
2895 Py_ssize_t i;
Victor Stinner35f28032013-11-21 12:16:35 +01002896 int res;
Tim Peters3b01a122002-07-19 02:35:45 +00002897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 if (slicelength <= 0)
2899 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 if (step < 0) {
2902 stop = start + 1;
2903 start = stop + step*(slicelength - 1) - 1;
2904 step = -step;
2905 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 garbage = (PyObject**)
2908 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2909 if (!garbage) {
2910 PyErr_NoMemory();
2911 return -1;
2912 }
Tim Peters3b01a122002-07-19 02:35:45 +00002913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 /* drawing pictures might help understand these for
2915 loops. Basically, we memmove the parts of the
2916 list that are *not* part of the slice: step-1
2917 items for each item that is part of the slice,
2918 and then tail end of the list that was not
2919 covered by the slice */
2920 for (cur = start, i = 0;
2921 cur < (size_t)stop;
2922 cur += step, i++) {
2923 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 if (cur + step >= (size_t)Py_SIZE(self)) {
2928 lim = Py_SIZE(self) - cur - 1;
2929 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931 memmove(self->ob_item + cur - i,
2932 self->ob_item + cur + 1,
2933 lim * sizeof(PyObject *));
2934 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002935 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 if (cur < (size_t)Py_SIZE(self)) {
2937 memmove(self->ob_item + cur - slicelength,
2938 self->ob_item + cur,
2939 (Py_SIZE(self) - cur) *
2940 sizeof(PyObject *));
2941 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 Py_SIZE(self) -= slicelength;
Victor Stinner35f28032013-11-21 12:16:35 +01002944 res = list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 for (i = 0; i < slicelength; i++) {
2947 Py_DECREF(garbage[i]);
2948 }
2949 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002950
Victor Stinner35f28032013-11-21 12:16:35 +01002951 return res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 }
2953 else {
2954 /* assign slice */
2955 PyObject *ins, *seq;
2956 PyObject **garbage, **seqitems, **selfitems;
HongWeipeng3c87a662019-09-08 18:15:56 +08002957 Py_ssize_t i;
2958 size_t cur;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002960 /* protect against a[::-1] = a */
2961 if (self == (PyListObject*)value) {
2962 seq = list_slice((PyListObject*)value, 0,
2963 PyList_GET_SIZE(value));
2964 }
2965 else {
2966 seq = PySequence_Fast(value,
2967 "must assign iterable "
2968 "to extended slice");
2969 }
2970 if (!seq)
2971 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002973 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2974 PyErr_Format(PyExc_ValueError,
2975 "attempt to assign sequence of "
2976 "size %zd to extended slice of "
2977 "size %zd",
2978 PySequence_Fast_GET_SIZE(seq),
2979 slicelength);
2980 Py_DECREF(seq);
2981 return -1;
2982 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002984 if (!slicelength) {
2985 Py_DECREF(seq);
2986 return 0;
2987 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 garbage = (PyObject**)
2990 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2991 if (!garbage) {
2992 Py_DECREF(seq);
2993 PyErr_NoMemory();
2994 return -1;
2995 }
Tim Peters3b01a122002-07-19 02:35:45 +00002996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 selfitems = self->ob_item;
2998 seqitems = PySequence_Fast_ITEMS(seq);
2999 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01003000 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 garbage[i] = selfitems[cur];
3002 ins = seqitems[i];
3003 Py_INCREF(ins);
3004 selfitems[cur] = ins;
3005 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00003006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003007 for (i = 0; i < slicelength; i++) {
3008 Py_DECREF(garbage[i]);
3009 }
Tim Peters3b01a122002-07-19 02:35:45 +00003010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 PyMem_FREE(garbage);
3012 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00003013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003014 return 0;
3015 }
3016 }
3017 else {
3018 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04003019 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 item->ob_type->tp_name);
3021 return -1;
3022 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00003023}
3024
3025static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026 (lenfunc)list_length,
3027 (binaryfunc)list_subscript,
3028 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00003029};
3030
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003031PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3033 "list",
3034 sizeof(PyListObject),
3035 0,
3036 (destructor)list_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003037 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038 0, /* tp_getattr */
3039 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003040 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 (reprfunc)list_repr, /* tp_repr */
3042 0, /* tp_as_number */
3043 &list_as_sequence, /* tp_as_sequence */
3044 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003045 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 0, /* tp_call */
3047 0, /* tp_str */
3048 PyObject_GenericGetAttr, /* tp_getattro */
3049 0, /* tp_setattro */
3050 0, /* tp_as_buffer */
3051 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003052 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
3053 list___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 (traverseproc)list_traverse, /* tp_traverse */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003055 (inquiry)_list_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003056 list_richcompare, /* tp_richcompare */
3057 0, /* tp_weaklistoffset */
3058 list_iter, /* tp_iter */
3059 0, /* tp_iternext */
3060 list_methods, /* tp_methods */
3061 0, /* tp_members */
3062 0, /* tp_getset */
3063 0, /* tp_base */
3064 0, /* tp_dict */
3065 0, /* tp_descr_get */
3066 0, /* tp_descr_set */
3067 0, /* tp_dictoffset */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003068 (initproc)list___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 PyType_GenericAlloc, /* tp_alloc */
3070 PyType_GenericNew, /* tp_new */
3071 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003072};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00003073
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003074/*********************** List Iterator **************************/
3075
3076typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 PyObject_HEAD
Victor Stinner7660b882013-06-24 23:59:24 +02003078 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003080} listiterobject;
3081
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003082static void listiter_dealloc(listiterobject *);
3083static int listiter_traverse(listiterobject *, visitproc, void *);
3084static PyObject *listiter_next(listiterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303085static PyObject *listiter_len(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003086static PyObject *listiter_reduce_general(void *_it, int forward);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303087static PyObject *listiter_reduce(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003088static PyObject *listiter_setstate(listiterobject *, PyObject *state);
Raymond Hettinger435bf582004-03-18 22:43:10 +00003089
Armin Rigof5b3e362006-02-11 21:32:43 +00003090PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003091PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3092PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003093
3094static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003095 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003096 {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
3097 {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00003099};
3100
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003101PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003102 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3103 "list_iterator", /* tp_name */
3104 sizeof(listiterobject), /* tp_basicsize */
3105 0, /* tp_itemsize */
3106 /* methods */
3107 (destructor)listiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003108 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109 0, /* tp_getattr */
3110 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003111 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003112 0, /* tp_repr */
3113 0, /* tp_as_number */
3114 0, /* tp_as_sequence */
3115 0, /* tp_as_mapping */
3116 0, /* tp_hash */
3117 0, /* tp_call */
3118 0, /* tp_str */
3119 PyObject_GenericGetAttr, /* tp_getattro */
3120 0, /* tp_setattro */
3121 0, /* tp_as_buffer */
3122 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3123 0, /* tp_doc */
3124 (traverseproc)listiter_traverse, /* tp_traverse */
3125 0, /* tp_clear */
3126 0, /* tp_richcompare */
3127 0, /* tp_weaklistoffset */
3128 PyObject_SelfIter, /* tp_iter */
3129 (iternextfunc)listiter_next, /* tp_iternext */
3130 listiter_methods, /* tp_methods */
3131 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003132};
Raymond Hettinger1021c442003-11-07 15:38:09 +00003133
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003134
3135static PyObject *
3136list_iter(PyObject *seq)
3137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 if (!PyList_Check(seq)) {
3141 PyErr_BadInternalCall();
3142 return NULL;
3143 }
3144 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
3145 if (it == NULL)
3146 return NULL;
3147 it->it_index = 0;
3148 Py_INCREF(seq);
3149 it->it_seq = (PyListObject *)seq;
3150 _PyObject_GC_TRACK(it);
3151 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003152}
3153
3154static void
3155listiter_dealloc(listiterobject *it)
3156{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157 _PyObject_GC_UNTRACK(it);
3158 Py_XDECREF(it->it_seq);
3159 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003160}
3161
3162static int
3163listiter_traverse(listiterobject *it, visitproc visit, void *arg)
3164{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 Py_VISIT(it->it_seq);
3166 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003167}
3168
3169static PyObject *
3170listiter_next(listiterobject *it)
3171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003172 PyListObject *seq;
3173 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 assert(it != NULL);
3176 seq = it->it_seq;
3177 if (seq == NULL)
3178 return NULL;
3179 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 if (it->it_index < PyList_GET_SIZE(seq)) {
3182 item = PyList_GET_ITEM(seq, it->it_index);
3183 ++it->it_index;
3184 Py_INCREF(item);
3185 return item;
3186 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003189 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003191}
3192
3193static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303194listiter_len(listiterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003196 Py_ssize_t len;
3197 if (it->it_seq) {
3198 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
3199 if (len >= 0)
3200 return PyLong_FromSsize_t(len);
3201 }
3202 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003203}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003204
3205static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303206listiter_reduce(listiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003207{
3208 return listiter_reduce_general(it, 1);
3209}
3210
3211static PyObject *
3212listiter_setstate(listiterobject *it, PyObject *state)
3213{
Victor Stinner7660b882013-06-24 23:59:24 +02003214 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003215 if (index == -1 && PyErr_Occurred())
3216 return NULL;
3217 if (it->it_seq != NULL) {
3218 if (index < 0)
3219 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00003220 else if (index > PyList_GET_SIZE(it->it_seq))
3221 index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003222 it->it_index = index;
3223 }
3224 Py_RETURN_NONE;
3225}
3226
Raymond Hettinger1021c442003-11-07 15:38:09 +00003227/*********************** List Reverse Iterator **************************/
3228
3229typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 PyObject_HEAD
3231 Py_ssize_t it_index;
3232 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00003233} listreviterobject;
3234
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003235static void listreviter_dealloc(listreviterobject *);
3236static int listreviter_traverse(listreviterobject *, visitproc, void *);
3237static PyObject *listreviter_next(listreviterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303238static PyObject *listreviter_len(listreviterobject *, PyObject *);
3239static PyObject *listreviter_reduce(listreviterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003240static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003241
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003242static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003244 {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
3245 {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003246 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003247};
3248
Raymond Hettinger1021c442003-11-07 15:38:09 +00003249PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3251 "list_reverseiterator", /* tp_name */
3252 sizeof(listreviterobject), /* tp_basicsize */
3253 0, /* tp_itemsize */
3254 /* methods */
3255 (destructor)listreviter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003256 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 0, /* tp_getattr */
3258 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003259 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 0, /* tp_repr */
3261 0, /* tp_as_number */
3262 0, /* tp_as_sequence */
3263 0, /* tp_as_mapping */
3264 0, /* tp_hash */
3265 0, /* tp_call */
3266 0, /* tp_str */
3267 PyObject_GenericGetAttr, /* tp_getattro */
3268 0, /* tp_setattro */
3269 0, /* tp_as_buffer */
3270 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3271 0, /* tp_doc */
3272 (traverseproc)listreviter_traverse, /* tp_traverse */
3273 0, /* tp_clear */
3274 0, /* tp_richcompare */
3275 0, /* tp_weaklistoffset */
3276 PyObject_SelfIter, /* tp_iter */
3277 (iternextfunc)listreviter_next, /* tp_iternext */
3278 listreviter_methods, /* tp_methods */
3279 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00003280};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003281
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003282/*[clinic input]
3283list.__reversed__
3284
3285Return a reverse iterator over the list.
3286[clinic start generated code]*/
3287
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003288static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003289list___reversed___impl(PyListObject *self)
3290/*[clinic end generated code: output=b166f073208c888c input=eadb6e17f8a6a280]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003292 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
3295 if (it == NULL)
3296 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003297 assert(PyList_Check(self));
3298 it->it_index = PyList_GET_SIZE(self) - 1;
3299 Py_INCREF(self);
3300 it->it_seq = self;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003301 PyObject_GC_Track(it);
3302 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003303}
3304
3305static void
3306listreviter_dealloc(listreviterobject *it)
3307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 PyObject_GC_UnTrack(it);
3309 Py_XDECREF(it->it_seq);
3310 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003311}
3312
3313static int
3314listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
3315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 Py_VISIT(it->it_seq);
3317 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003318}
3319
3320static PyObject *
3321listreviter_next(listreviterobject *it)
3322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 PyObject *item;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003324 Py_ssize_t index;
3325 PyListObject *seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003326
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003327 assert(it != NULL);
3328 seq = it->it_seq;
3329 if (seq == NULL) {
3330 return NULL;
3331 }
3332 assert(PyList_Check(seq));
3333
3334 index = it->it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 if (index>=0 && index < PyList_GET_SIZE(seq)) {
3336 item = PyList_GET_ITEM(seq, index);
3337 it->it_index--;
3338 Py_INCREF(item);
3339 return item;
3340 }
3341 it->it_index = -1;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003342 it->it_seq = NULL;
3343 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003344 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003345}
3346
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003347static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303348listreviter_len(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 Py_ssize_t len = it->it_index + 1;
3351 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
3352 len = 0;
3353 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003354}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003355
3356static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303357listreviter_reduce(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003358{
3359 return listiter_reduce_general(it, 0);
3360}
3361
3362static PyObject *
3363listreviter_setstate(listreviterobject *it, PyObject *state)
3364{
3365 Py_ssize_t index = PyLong_AsSsize_t(state);
3366 if (index == -1 && PyErr_Occurred())
3367 return NULL;
3368 if (it->it_seq != NULL) {
3369 if (index < -1)
3370 index = -1;
3371 else if (index > PyList_GET_SIZE(it->it_seq) - 1)
3372 index = PyList_GET_SIZE(it->it_seq) - 1;
3373 it->it_index = index;
3374 }
3375 Py_RETURN_NONE;
3376}
3377
3378/* common pickling support */
3379
3380static PyObject *
3381listiter_reduce_general(void *_it, int forward)
3382{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003383 _Py_IDENTIFIER(iter);
3384 _Py_IDENTIFIER(reversed);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003385 PyObject *list;
3386
3387 /* the objects are not the same, index is of different types! */
3388 if (forward) {
3389 listiterobject *it = (listiterobject *)_it;
3390 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003391 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003392 it->it_seq, it->it_index);
3393 } else {
3394 listreviterobject *it = (listreviterobject *)_it;
3395 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003396 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_reversed),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003397 it->it_seq, it->it_index);
3398 }
3399 /* empty iterator, create an empty list */
3400 list = PyList_New(0);
3401 if (list == NULL)
3402 return NULL;
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003403 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003404}