/src/cpython/Objects/clinic/listobject.c.h
Line | Count | Source (jump to first uncovered line) |
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 * |
26 | | list_insert(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
27 | 60 | { |
28 | 60 | PyObject *return_value = NULL; |
29 | 60 | Py_ssize_t index; |
30 | 60 | PyObject *object; |
31 | | |
32 | 60 | if (!_PyArg_CheckPositional("insert", nargs, 2, 2)) { |
33 | 0 | goto exit; |
34 | 0 | } |
35 | 60 | { |
36 | 60 | Py_ssize_t ival = -1; |
37 | 60 | PyObject *iobj = _PyNumber_Index(args[0]); |
38 | 60 | if (iobj != NULL) { |
39 | 60 | ival = PyLong_AsSsize_t(iobj); |
40 | 60 | Py_DECREF(iobj); |
41 | 60 | } |
42 | 60 | if (ival == -1 && PyErr_Occurred()) { |
43 | 0 | goto exit; |
44 | 0 | } |
45 | 60 | index = ival; |
46 | 60 | } |
47 | 0 | object = args[1]; |
48 | 60 | Py_BEGIN_CRITICAL_SECTION(self); |
49 | 60 | return_value = list_insert_impl((PyListObject *)self, index, object); |
50 | 60 | Py_END_CRITICAL_SECTION(); |
51 | | |
52 | 60 | exit: |
53 | 60 | return return_value; |
54 | 60 | } |
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 * |
69 | | py_list_clear(PyObject *self, PyObject *Py_UNUSED(ignored)) |
70 | 148 | { |
71 | 148 | PyObject *return_value = NULL; |
72 | | |
73 | 148 | Py_BEGIN_CRITICAL_SECTION(self); |
74 | 148 | return_value = py_list_clear_impl((PyListObject *)self); |
75 | 148 | Py_END_CRITICAL_SECTION(); |
76 | | |
77 | 148 | return return_value; |
78 | 148 | } |
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 * |
93 | | list_copy(PyObject *self, PyObject *Py_UNUSED(ignored)) |
94 | 0 | { |
95 | 0 | PyObject *return_value = NULL; |
96 | |
|
97 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
98 | 0 | return_value = list_copy_impl((PyListObject *)self); |
99 | 0 | Py_END_CRITICAL_SECTION(); |
100 | |
|
101 | 0 | return return_value; |
102 | 0 | } |
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 * |
117 | | list_append(PyObject *self, PyObject *object) |
118 | 19.9M | { |
119 | 19.9M | PyObject *return_value = NULL; |
120 | | |
121 | 19.9M | Py_BEGIN_CRITICAL_SECTION(self); |
122 | 19.9M | return_value = list_append_impl((PyListObject *)self, object); |
123 | 19.9M | Py_END_CRITICAL_SECTION(); |
124 | | |
125 | 19.9M | return return_value; |
126 | 19.9M | } |
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 | | static PyObject * |
138 | | list_extend_impl(PyListObject *self, PyObject *iterable); |
139 | | |
140 | | static PyObject * |
141 | | list_extend(PyObject *self, PyObject *iterable) |
142 | 21.5M | { |
143 | 21.5M | PyObject *return_value = NULL; |
144 | | |
145 | 21.5M | return_value = list_extend_impl((PyListObject *)self, iterable); |
146 | | |
147 | 21.5M | return return_value; |
148 | 21.5M | } |
149 | | |
150 | | PyDoc_STRVAR(list_pop__doc__, |
151 | | "pop($self, index=-1, /)\n" |
152 | | "--\n" |
153 | | "\n" |
154 | | "Remove and return item at index (default last).\n" |
155 | | "\n" |
156 | | "Raises IndexError if list is empty or index is out of range."); |
157 | | |
158 | | #define LIST_POP_METHODDEF \ |
159 | | {"pop", _PyCFunction_CAST(list_pop), METH_FASTCALL, list_pop__doc__}, |
160 | | |
161 | | static PyObject * |
162 | | list_pop_impl(PyListObject *self, Py_ssize_t index); |
163 | | |
164 | | static PyObject * |
165 | | list_pop(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
166 | 37.9M | { |
167 | 37.9M | PyObject *return_value = NULL; |
168 | 37.9M | Py_ssize_t index = -1; |
169 | | |
170 | 37.9M | if (!_PyArg_CheckPositional("pop", nargs, 0, 1)) { |
171 | 0 | goto exit; |
172 | 0 | } |
173 | 37.9M | if (nargs < 1) { |
174 | 14.6M | goto skip_optional; |
175 | 14.6M | } |
176 | 23.2M | { |
177 | 23.2M | Py_ssize_t ival = -1; |
178 | 23.2M | PyObject *iobj = _PyNumber_Index(args[0]); |
179 | 23.2M | if (iobj != NULL) { |
180 | 23.2M | ival = PyLong_AsSsize_t(iobj); |
181 | 23.2M | Py_DECREF(iobj); |
182 | 23.2M | } |
183 | 23.2M | if (ival == -1 && PyErr_Occurred()) { |
184 | 0 | goto exit; |
185 | 0 | } |
186 | 23.2M | index = ival; |
187 | 23.2M | } |
188 | 37.9M | skip_optional: |
189 | 37.9M | Py_BEGIN_CRITICAL_SECTION(self); |
190 | 37.9M | return_value = list_pop_impl((PyListObject *)self, index); |
191 | 37.9M | Py_END_CRITICAL_SECTION(); |
192 | | |
193 | 37.9M | exit: |
194 | 37.9M | return return_value; |
195 | 37.9M | } |
196 | | |
197 | | PyDoc_STRVAR(list_sort__doc__, |
198 | | "sort($self, /, *, key=None, reverse=False)\n" |
199 | | "--\n" |
200 | | "\n" |
201 | | "Sort the list in ascending order and return None.\n" |
202 | | "\n" |
203 | | "The sort is in-place (i.e. the list itself is modified) and stable (i.e. the\n" |
204 | | "order of two equal elements is maintained).\n" |
205 | | "\n" |
206 | | "If a key function is given, apply it once to each list item and sort them,\n" |
207 | | "ascending or descending, according to their function values.\n" |
208 | | "\n" |
209 | | "The reverse flag can be set to sort in descending order."); |
210 | | |
211 | | #define LIST_SORT_METHODDEF \ |
212 | | {"sort", _PyCFunction_CAST(list_sort), METH_FASTCALL|METH_KEYWORDS, list_sort__doc__}, |
213 | | |
214 | | static PyObject * |
215 | | list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse); |
216 | | |
217 | | static PyObject * |
218 | | list_sort(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
219 | 764k | { |
220 | 764k | PyObject *return_value = NULL; |
221 | 764k | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
222 | | |
223 | 764k | #define NUM_KEYWORDS 2 |
224 | 764k | static struct { |
225 | 764k | PyGC_Head _this_is_not_used; |
226 | 764k | PyObject_VAR_HEAD |
227 | 764k | Py_hash_t ob_hash; |
228 | 764k | PyObject *ob_item[NUM_KEYWORDS]; |
229 | 764k | } _kwtuple = { |
230 | 764k | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
231 | 764k | .ob_hash = -1, |
232 | 764k | .ob_item = { &_Py_ID(key), &_Py_ID(reverse), }, |
233 | 764k | }; |
234 | 764k | #undef NUM_KEYWORDS |
235 | 764k | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
236 | | |
237 | | #else // !Py_BUILD_CORE |
238 | | # define KWTUPLE NULL |
239 | | #endif // !Py_BUILD_CORE |
240 | | |
241 | 764k | static const char * const _keywords[] = {"key", "reverse", NULL}; |
242 | 764k | static _PyArg_Parser _parser = { |
243 | 764k | .keywords = _keywords, |
244 | 764k | .fname = "sort", |
245 | 764k | .kwtuple = KWTUPLE, |
246 | 764k | }; |
247 | 764k | #undef KWTUPLE |
248 | 764k | PyObject *argsbuf[2]; |
249 | 764k | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
250 | 764k | PyObject *keyfunc = Py_None; |
251 | 764k | int reverse = 0; |
252 | | |
253 | 764k | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
254 | 764k | /*minpos*/ 0, /*maxpos*/ 0, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
255 | 764k | if (!args) { |
256 | 0 | goto exit; |
257 | 0 | } |
258 | 764k | if (!noptargs) { |
259 | 251k | goto skip_optional_kwonly; |
260 | 251k | } |
261 | 513k | if (args[0]) { |
262 | 513k | keyfunc = args[0]; |
263 | 513k | if (!--noptargs) { |
264 | 513k | goto skip_optional_kwonly; |
265 | 513k | } |
266 | 513k | } |
267 | 0 | reverse = PyObject_IsTrue(args[1]); |
268 | 0 | if (reverse < 0) { |
269 | 0 | goto exit; |
270 | 0 | } |
271 | 764k | skip_optional_kwonly: |
272 | 764k | Py_BEGIN_CRITICAL_SECTION(self); |
273 | 764k | return_value = list_sort_impl((PyListObject *)self, keyfunc, reverse); |
274 | 764k | Py_END_CRITICAL_SECTION(); |
275 | | |
276 | 764k | exit: |
277 | 764k | return return_value; |
278 | 764k | } |
279 | | |
280 | | PyDoc_STRVAR(list_reverse__doc__, |
281 | | "reverse($self, /)\n" |
282 | | "--\n" |
283 | | "\n" |
284 | | "Reverse *IN PLACE*."); |
285 | | |
286 | | #define LIST_REVERSE_METHODDEF \ |
287 | | {"reverse", (PyCFunction)list_reverse, METH_NOARGS, list_reverse__doc__}, |
288 | | |
289 | | static PyObject * |
290 | | list_reverse_impl(PyListObject *self); |
291 | | |
292 | | static PyObject * |
293 | | list_reverse(PyObject *self, PyObject *Py_UNUSED(ignored)) |
294 | 0 | { |
295 | 0 | PyObject *return_value = NULL; |
296 | |
|
297 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
298 | 0 | return_value = list_reverse_impl((PyListObject *)self); |
299 | 0 | Py_END_CRITICAL_SECTION(); |
300 | |
|
301 | 0 | return return_value; |
302 | 0 | } |
303 | | |
304 | | PyDoc_STRVAR(list_index__doc__, |
305 | | "index($self, value, start=0, stop=sys.maxsize, /)\n" |
306 | | "--\n" |
307 | | "\n" |
308 | | "Return first index of value.\n" |
309 | | "\n" |
310 | | "Raises ValueError if the value is not present."); |
311 | | |
312 | | #define LIST_INDEX_METHODDEF \ |
313 | | {"index", _PyCFunction_CAST(list_index), METH_FASTCALL, list_index__doc__}, |
314 | | |
315 | | static PyObject * |
316 | | list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start, |
317 | | Py_ssize_t stop); |
318 | | |
319 | | static PyObject * |
320 | | list_index(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
321 | 0 | { |
322 | 0 | PyObject *return_value = NULL; |
323 | 0 | PyObject *value; |
324 | 0 | Py_ssize_t start = 0; |
325 | 0 | Py_ssize_t stop = PY_SSIZE_T_MAX; |
326 | |
|
327 | 0 | if (!_PyArg_CheckPositional("index", nargs, 1, 3)) { |
328 | 0 | goto exit; |
329 | 0 | } |
330 | 0 | value = args[0]; |
331 | 0 | if (nargs < 2) { |
332 | 0 | goto skip_optional; |
333 | 0 | } |
334 | 0 | if (!_PyEval_SliceIndexNotNone(args[1], &start)) { |
335 | 0 | goto exit; |
336 | 0 | } |
337 | 0 | if (nargs < 3) { |
338 | 0 | goto skip_optional; |
339 | 0 | } |
340 | 0 | if (!_PyEval_SliceIndexNotNone(args[2], &stop)) { |
341 | 0 | goto exit; |
342 | 0 | } |
343 | 0 | skip_optional: |
344 | 0 | return_value = list_index_impl((PyListObject *)self, value, start, stop); |
345 | |
|
346 | 0 | exit: |
347 | 0 | return return_value; |
348 | 0 | } |
349 | | |
350 | | PyDoc_STRVAR(list_count__doc__, |
351 | | "count($self, value, /)\n" |
352 | | "--\n" |
353 | | "\n" |
354 | | "Return number of occurrences of value."); |
355 | | |
356 | | #define LIST_COUNT_METHODDEF \ |
357 | | {"count", (PyCFunction)list_count, METH_O, list_count__doc__}, |
358 | | |
359 | | static PyObject * |
360 | | list_count_impl(PyListObject *self, PyObject *value); |
361 | | |
362 | | static PyObject * |
363 | | list_count(PyObject *self, PyObject *value) |
364 | 0 | { |
365 | 0 | PyObject *return_value = NULL; |
366 | |
|
367 | 0 | return_value = list_count_impl((PyListObject *)self, value); |
368 | |
|
369 | 0 | return return_value; |
370 | 0 | } |
371 | | |
372 | | PyDoc_STRVAR(list_remove__doc__, |
373 | | "remove($self, value, /)\n" |
374 | | "--\n" |
375 | | "\n" |
376 | | "Remove first occurrence of value.\n" |
377 | | "\n" |
378 | | "Raises ValueError if the value is not present."); |
379 | | |
380 | | #define LIST_REMOVE_METHODDEF \ |
381 | | {"remove", (PyCFunction)list_remove, METH_O, list_remove__doc__}, |
382 | | |
383 | | static PyObject * |
384 | | list_remove_impl(PyListObject *self, PyObject *value); |
385 | | |
386 | | static PyObject * |
387 | | list_remove(PyObject *self, PyObject *value) |
388 | 2.91k | { |
389 | 2.91k | PyObject *return_value = NULL; |
390 | | |
391 | 2.91k | Py_BEGIN_CRITICAL_SECTION(self); |
392 | 2.91k | return_value = list_remove_impl((PyListObject *)self, value); |
393 | 2.91k | Py_END_CRITICAL_SECTION(); |
394 | | |
395 | 2.91k | return return_value; |
396 | 2.91k | } |
397 | | |
398 | | PyDoc_STRVAR(list___init____doc__, |
399 | | "list(iterable=(), /)\n" |
400 | | "--\n" |
401 | | "\n" |
402 | | "Built-in mutable sequence.\n" |
403 | | "\n" |
404 | | "If no argument is given, the constructor creates a new empty list.\n" |
405 | | "The argument must be an iterable if specified."); |
406 | | |
407 | | static int |
408 | | list___init___impl(PyListObject *self, PyObject *iterable); |
409 | | |
410 | | static int |
411 | | list___init__(PyObject *self, PyObject *args, PyObject *kwargs) |
412 | 11.2M | { |
413 | 11.2M | int return_value = -1; |
414 | 11.2M | PyTypeObject *base_tp = &PyList_Type; |
415 | 11.2M | PyObject *iterable = NULL; |
416 | | |
417 | 11.2M | if ((Py_IS_TYPE(self, base_tp) || |
418 | 11.2M | Py_TYPE(self)->tp_new == base_tp->tp_new) && |
419 | 11.2M | !_PyArg_NoKeywords("list", kwargs)) { |
420 | 0 | goto exit; |
421 | 0 | } |
422 | 11.2M | if (!_PyArg_CheckPositional("list", PyTuple_GET_SIZE(args), 0, 1)) { |
423 | 0 | goto exit; |
424 | 0 | } |
425 | 11.2M | if (PyTuple_GET_SIZE(args) < 1) { |
426 | 11.2M | goto skip_optional; |
427 | 11.2M | } |
428 | 0 | iterable = PyTuple_GET_ITEM(args, 0); |
429 | 11.2M | skip_optional: |
430 | 11.2M | return_value = list___init___impl((PyListObject *)self, iterable); |
431 | | |
432 | 11.2M | exit: |
433 | 11.2M | return return_value; |
434 | 11.2M | } |
435 | | |
436 | | PyDoc_STRVAR(list___sizeof____doc__, |
437 | | "__sizeof__($self, /)\n" |
438 | | "--\n" |
439 | | "\n" |
440 | | "Return the size of the list in memory, in bytes."); |
441 | | |
442 | | #define LIST___SIZEOF___METHODDEF \ |
443 | | {"__sizeof__", (PyCFunction)list___sizeof__, METH_NOARGS, list___sizeof____doc__}, |
444 | | |
445 | | static PyObject * |
446 | | list___sizeof___impl(PyListObject *self); |
447 | | |
448 | | static PyObject * |
449 | | list___sizeof__(PyObject *self, PyObject *Py_UNUSED(ignored)) |
450 | 0 | { |
451 | 0 | return list___sizeof___impl((PyListObject *)self); |
452 | 0 | } |
453 | | |
454 | | PyDoc_STRVAR(list___reversed____doc__, |
455 | | "__reversed__($self, /)\n" |
456 | | "--\n" |
457 | | "\n" |
458 | | "Return a reverse iterator over the list."); |
459 | | |
460 | | #define LIST___REVERSED___METHODDEF \ |
461 | | {"__reversed__", (PyCFunction)list___reversed__, METH_NOARGS, list___reversed____doc__}, |
462 | | |
463 | | static PyObject * |
464 | | list___reversed___impl(PyListObject *self); |
465 | | |
466 | | static PyObject * |
467 | | list___reversed__(PyObject *self, PyObject *Py_UNUSED(ignored)) |
468 | 41.4M | { |
469 | 41.4M | return list___reversed___impl((PyListObject *)self); |
470 | 41.4M | } |
471 | | /*[clinic end generated code: output=ae13fc2b56dc27c2 input=a9049054013a1b77]*/ |