1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4
5 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6 # include "pycore_gc.h" // PyGC_Head
7 # include "pycore_runtime.h" // _Py_ID()
8 #endif
9 #include "pycore_abstract.h" // _PyNumber_Index()
10 #include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION()
11 #include "pycore_modsupport.h" // _PyArg_CheckPositional()
12
13 PyDoc_STRVAR(list_insert__doc__,
14 "insert($self, index, object, /)\n"
15 "--\n"
16 "\n"
17 "Insert object before index.");
18
19 #define LIST_INSERT_METHODDEF \
20 {"insert", _PyCFunction_CAST(list_insert), METH_FASTCALL, list_insert__doc__},
21
22 static PyObject *
23 list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object);
24
25 static PyObject *
list_insert(PyListObject * self,PyObject * const * args,Py_ssize_t nargs)26 list_insert(PyListObject *self, PyObject *const *args, Py_ssize_t nargs)
27 {
28 PyObject *return_value = NULL;
29 Py_ssize_t index;
30 PyObject *object;
31
32 if (!_PyArg_CheckPositional("insert", nargs, 2, 2)) {
33 goto exit;
34 }
35 {
36 Py_ssize_t ival = -1;
37 PyObject *iobj = _PyNumber_Index(args[0]);
38 if (iobj != NULL) {
39 ival = PyLong_AsSsize_t(iobj);
40 Py_DECREF(iobj);
41 }
42 if (ival == -1 && PyErr_Occurred()) {
43 goto exit;
44 }
45 index = ival;
46 }
47 object = args[1];
48 Py_BEGIN_CRITICAL_SECTION(self);
49 return_value = list_insert_impl(self, index, object);
50 Py_END_CRITICAL_SECTION();
51
52 exit:
53 return return_value;
54 }
55
56 PyDoc_STRVAR(py_list_clear__doc__,
57 "clear($self, /)\n"
58 "--\n"
59 "\n"
60 "Remove all items from list.");
61
62 #define PY_LIST_CLEAR_METHODDEF \
63 {"clear", (PyCFunction)py_list_clear, METH_NOARGS, py_list_clear__doc__},
64
65 static PyObject *
66 py_list_clear_impl(PyListObject *self);
67
68 static PyObject *
py_list_clear(PyListObject * self,PyObject * Py_UNUSED (ignored))69 py_list_clear(PyListObject *self, PyObject *Py_UNUSED(ignored))
70 {
71 PyObject *return_value = NULL;
72
73 Py_BEGIN_CRITICAL_SECTION(self);
74 return_value = py_list_clear_impl(self);
75 Py_END_CRITICAL_SECTION();
76
77 return return_value;
78 }
79
80 PyDoc_STRVAR(list_copy__doc__,
81 "copy($self, /)\n"
82 "--\n"
83 "\n"
84 "Return a shallow copy of the list.");
85
86 #define LIST_COPY_METHODDEF \
87 {"copy", (PyCFunction)list_copy, METH_NOARGS, list_copy__doc__},
88
89 static PyObject *
90 list_copy_impl(PyListObject *self);
91
92 static PyObject *
list_copy(PyListObject * self,PyObject * Py_UNUSED (ignored))93 list_copy(PyListObject *self, PyObject *Py_UNUSED(ignored))
94 {
95 PyObject *return_value = NULL;
96
97 Py_BEGIN_CRITICAL_SECTION(self);
98 return_value = list_copy_impl(self);
99 Py_END_CRITICAL_SECTION();
100
101 return return_value;
102 }
103
104 PyDoc_STRVAR(list_append__doc__,
105 "append($self, object, /)\n"
106 "--\n"
107 "\n"
108 "Append object to the end of the list.");
109
110 #define LIST_APPEND_METHODDEF \
111 {"append", (PyCFunction)list_append, METH_O, list_append__doc__},
112
113 static PyObject *
114 list_append_impl(PyListObject *self, PyObject *object);
115
116 static PyObject *
list_append(PyListObject * self,PyObject * object)117 list_append(PyListObject *self, PyObject *object)
118 {
119 PyObject *return_value = NULL;
120
121 Py_BEGIN_CRITICAL_SECTION(self);
122 return_value = list_append_impl(self, object);
123 Py_END_CRITICAL_SECTION();
124
125 return return_value;
126 }
127
128 PyDoc_STRVAR(list_extend__doc__,
129 "extend($self, iterable, /)\n"
130 "--\n"
131 "\n"
132 "Extend list by appending elements from the iterable.");
133
134 #define LIST_EXTEND_METHODDEF \
135 {"extend", (PyCFunction)list_extend, METH_O, list_extend__doc__},
136
137 PyDoc_STRVAR(list_pop__doc__,
138 "pop($self, index=-1, /)\n"
139 "--\n"
140 "\n"
141 "Remove and return item at index (default last).\n"
142 "\n"
143 "Raises IndexError if list is empty or index is out of range.");
144
145 #define LIST_POP_METHODDEF \
146 {"pop", _PyCFunction_CAST(list_pop), METH_FASTCALL, list_pop__doc__},
147
148 static PyObject *
149 list_pop_impl(PyListObject *self, Py_ssize_t index);
150
151 static PyObject *
list_pop(PyListObject * self,PyObject * const * args,Py_ssize_t nargs)152 list_pop(PyListObject *self, PyObject *const *args, Py_ssize_t nargs)
153 {
154 PyObject *return_value = NULL;
155 Py_ssize_t index = -1;
156
157 if (!_PyArg_CheckPositional("pop", nargs, 0, 1)) {
158 goto exit;
159 }
160 if (nargs < 1) {
161 goto skip_optional;
162 }
163 {
164 Py_ssize_t ival = -1;
165 PyObject *iobj = _PyNumber_Index(args[0]);
166 if (iobj != NULL) {
167 ival = PyLong_AsSsize_t(iobj);
168 Py_DECREF(iobj);
169 }
170 if (ival == -1 && PyErr_Occurred()) {
171 goto exit;
172 }
173 index = ival;
174 }
175 skip_optional:
176 Py_BEGIN_CRITICAL_SECTION(self);
177 return_value = list_pop_impl(self, index);
178 Py_END_CRITICAL_SECTION();
179
180 exit:
181 return return_value;
182 }
183
184 PyDoc_STRVAR(list_sort__doc__,
185 "sort($self, /, *, key=None, reverse=False)\n"
186 "--\n"
187 "\n"
188 "Sort the list in ascending order and return None.\n"
189 "\n"
190 "The sort is in-place (i.e. the list itself is modified) and stable (i.e. the\n"
191 "order of two equal elements is maintained).\n"
192 "\n"
193 "If a key function is given, apply it once to each list item and sort them,\n"
194 "ascending or descending, according to their function values.\n"
195 "\n"
196 "The reverse flag can be set to sort in descending order.");
197
198 #define LIST_SORT_METHODDEF \
199 {"sort", _PyCFunction_CAST(list_sort), METH_FASTCALL|METH_KEYWORDS, list_sort__doc__},
200
201 static PyObject *
202 list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse);
203
204 static PyObject *
list_sort(PyListObject * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)205 list_sort(PyListObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
206 {
207 PyObject *return_value = NULL;
208 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
209
210 #define NUM_KEYWORDS 2
211 static struct {
212 PyGC_Head _this_is_not_used;
213 PyObject_VAR_HEAD
214 PyObject *ob_item[NUM_KEYWORDS];
215 } _kwtuple = {
216 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
217 .ob_item = { &_Py_ID(key), &_Py_ID(reverse), },
218 };
219 #undef NUM_KEYWORDS
220 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
221
222 #else // !Py_BUILD_CORE
223 # define KWTUPLE NULL
224 #endif // !Py_BUILD_CORE
225
226 static const char * const _keywords[] = {"key", "reverse", NULL};
227 static _PyArg_Parser _parser = {
228 .keywords = _keywords,
229 .fname = "sort",
230 .kwtuple = KWTUPLE,
231 };
232 #undef KWTUPLE
233 PyObject *argsbuf[2];
234 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
235 PyObject *keyfunc = Py_None;
236 int reverse = 0;
237
238 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, argsbuf);
239 if (!args) {
240 goto exit;
241 }
242 if (!noptargs) {
243 goto skip_optional_kwonly;
244 }
245 if (args[0]) {
246 keyfunc = args[0];
247 if (!--noptargs) {
248 goto skip_optional_kwonly;
249 }
250 }
251 reverse = PyObject_IsTrue(args[1]);
252 if (reverse < 0) {
253 goto exit;
254 }
255 skip_optional_kwonly:
256 Py_BEGIN_CRITICAL_SECTION(self);
257 return_value = list_sort_impl(self, keyfunc, reverse);
258 Py_END_CRITICAL_SECTION();
259
260 exit:
261 return return_value;
262 }
263
264 PyDoc_STRVAR(list_reverse__doc__,
265 "reverse($self, /)\n"
266 "--\n"
267 "\n"
268 "Reverse *IN PLACE*.");
269
270 #define LIST_REVERSE_METHODDEF \
271 {"reverse", (PyCFunction)list_reverse, METH_NOARGS, list_reverse__doc__},
272
273 static PyObject *
274 list_reverse_impl(PyListObject *self);
275
276 static PyObject *
list_reverse(PyListObject * self,PyObject * Py_UNUSED (ignored))277 list_reverse(PyListObject *self, PyObject *Py_UNUSED(ignored))
278 {
279 PyObject *return_value = NULL;
280
281 Py_BEGIN_CRITICAL_SECTION(self);
282 return_value = list_reverse_impl(self);
283 Py_END_CRITICAL_SECTION();
284
285 return return_value;
286 }
287
288 PyDoc_STRVAR(list_index__doc__,
289 "index($self, value, start=0, stop=sys.maxsize, /)\n"
290 "--\n"
291 "\n"
292 "Return first index of value.\n"
293 "\n"
294 "Raises ValueError if the value is not present.");
295
296 #define LIST_INDEX_METHODDEF \
297 {"index", _PyCFunction_CAST(list_index), METH_FASTCALL, list_index__doc__},
298
299 static PyObject *
300 list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
301 Py_ssize_t stop);
302
303 static PyObject *
list_index(PyListObject * self,PyObject * const * args,Py_ssize_t nargs)304 list_index(PyListObject *self, PyObject *const *args, Py_ssize_t nargs)
305 {
306 PyObject *return_value = NULL;
307 PyObject *value;
308 Py_ssize_t start = 0;
309 Py_ssize_t stop = PY_SSIZE_T_MAX;
310
311 if (!_PyArg_CheckPositional("index", nargs, 1, 3)) {
312 goto exit;
313 }
314 value = args[0];
315 if (nargs < 2) {
316 goto skip_optional;
317 }
318 if (!_PyEval_SliceIndexNotNone(args[1], &start)) {
319 goto exit;
320 }
321 if (nargs < 3) {
322 goto skip_optional;
323 }
324 if (!_PyEval_SliceIndexNotNone(args[2], &stop)) {
325 goto exit;
326 }
327 skip_optional:
328 return_value = list_index_impl(self, value, start, stop);
329
330 exit:
331 return return_value;
332 }
333
334 PyDoc_STRVAR(list_count__doc__,
335 "count($self, value, /)\n"
336 "--\n"
337 "\n"
338 "Return number of occurrences of value.");
339
340 #define LIST_COUNT_METHODDEF \
341 {"count", (PyCFunction)list_count, METH_O, list_count__doc__},
342
343 PyDoc_STRVAR(list_remove__doc__,
344 "remove($self, value, /)\n"
345 "--\n"
346 "\n"
347 "Remove first occurrence of value.\n"
348 "\n"
349 "Raises ValueError if the value is not present.");
350
351 #define LIST_REMOVE_METHODDEF \
352 {"remove", (PyCFunction)list_remove, METH_O, list_remove__doc__},
353
354 static PyObject *
355 list_remove_impl(PyListObject *self, PyObject *value);
356
357 static PyObject *
list_remove(PyListObject * self,PyObject * value)358 list_remove(PyListObject *self, PyObject *value)
359 {
360 PyObject *return_value = NULL;
361
362 Py_BEGIN_CRITICAL_SECTION(self);
363 return_value = list_remove_impl(self, value);
364 Py_END_CRITICAL_SECTION();
365
366 return return_value;
367 }
368
369 PyDoc_STRVAR(list___init____doc__,
370 "list(iterable=(), /)\n"
371 "--\n"
372 "\n"
373 "Built-in mutable sequence.\n"
374 "\n"
375 "If no argument is given, the constructor creates a new empty list.\n"
376 "The argument must be an iterable if specified.");
377
378 static int
379 list___init___impl(PyListObject *self, PyObject *iterable);
380
381 static int
list___init__(PyObject * self,PyObject * args,PyObject * kwargs)382 list___init__(PyObject *self, PyObject *args, PyObject *kwargs)
383 {
384 int return_value = -1;
385 PyTypeObject *base_tp = &PyList_Type;
386 PyObject *iterable = NULL;
387
388 if ((Py_IS_TYPE(self, base_tp) ||
389 Py_TYPE(self)->tp_new == base_tp->tp_new) &&
390 !_PyArg_NoKeywords("list", kwargs)) {
391 goto exit;
392 }
393 if (!_PyArg_CheckPositional("list", PyTuple_GET_SIZE(args), 0, 1)) {
394 goto exit;
395 }
396 if (PyTuple_GET_SIZE(args) < 1) {
397 goto skip_optional;
398 }
399 iterable = PyTuple_GET_ITEM(args, 0);
400 skip_optional:
401 return_value = list___init___impl((PyListObject *)self, iterable);
402
403 exit:
404 return return_value;
405 }
406
407 PyDoc_STRVAR(list___sizeof____doc__,
408 "__sizeof__($self, /)\n"
409 "--\n"
410 "\n"
411 "Return the size of the list in memory, in bytes.");
412
413 #define LIST___SIZEOF___METHODDEF \
414 {"__sizeof__", (PyCFunction)list___sizeof__, METH_NOARGS, list___sizeof____doc__},
415
416 static PyObject *
417 list___sizeof___impl(PyListObject *self);
418
419 static PyObject *
list___sizeof__(PyListObject * self,PyObject * Py_UNUSED (ignored))420 list___sizeof__(PyListObject *self, PyObject *Py_UNUSED(ignored))
421 {
422 return list___sizeof___impl(self);
423 }
424
425 PyDoc_STRVAR(list___reversed____doc__,
426 "__reversed__($self, /)\n"
427 "--\n"
428 "\n"
429 "Return a reverse iterator over the list.");
430
431 #define LIST___REVERSED___METHODDEF \
432 {"__reversed__", (PyCFunction)list___reversed__, METH_NOARGS, list___reversed____doc__},
433
434 static PyObject *
435 list___reversed___impl(PyListObject *self);
436
437 static PyObject *
list___reversed__(PyListObject * self,PyObject * Py_UNUSED (ignored))438 list___reversed__(PyListObject *self, PyObject *Py_UNUSED(ignored))
439 {
440 return list___reversed___impl(self);
441 }
442 /*[clinic end generated code: output=854957a1d4a89bbd input=a9049054013a1b77]*/
443