Coverage Report

Created: 2026-12-01 06:09

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