Coverage Report

Created: 2026-10-31 06:21

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
23.3k
{
34
23.3k
    PyObject *return_value = NULL;
35
36
23.3k
    Py_BEGIN_CRITICAL_SECTION(a);
37
23.3k
    return_value = list_repeat_impl((PyListObject *)a, n);
38
23.3k
    Py_END_CRITICAL_SECTION();
39
40
23.3k
    return return_value;
41
23.3k
}
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.29k
{
64
3.29k
    int return_value = -1;
65
66
3.29k
    Py_BEGIN_CRITICAL_SECTION(a);
67
3.29k
    return_value = list_ass_item_impl((PyListObject *)a, i, v);
68
3.29k
    Py_END_CRITICAL_SECTION();
69
70
3.29k
    return return_value;
71
3.29k
}
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
76
{
88
76
    PyObject *return_value = NULL;
89
76
    Py_ssize_t index;
90
76
    PyObject *object;
91
92
76
    if (!_PyArg_CheckPositional("insert", nargs, 2, 2)) {
93
0
        goto exit;
94
0
    }
95
76
    {
96
76
        Py_ssize_t ival = -1;
97
76
        PyObject *iobj = _PyNumber_Index(args[0]);
98
76
        if (iobj != NULL) {
99
76
            ival = PyLong_AsSsize_t(iobj);
100
76
            Py_DECREF(iobj);
101
76
        }
102
76
        if (ival == -1 && PyErr_Occurred()) {
103
0
            goto exit;
104
0
        }
105
76
        index = ival;
106
76
    }
107
0
    object = args[1];
108
76
    Py_BEGIN_CRITICAL_SECTION(self);
109
76
    return_value = list_insert_impl((PyListObject *)self, index, object);
110
76
    Py_END_CRITICAL_SECTION();
111
112
76
exit:
113
76
    return return_value;
114
76
}
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
158
{
131
158
    PyObject *return_value = NULL;
132
133
158
    Py_BEGIN_CRITICAL_SECTION(self);
134
158
    return_value = py_list_clear_impl((PyListObject *)self);
135
158
    Py_END_CRITICAL_SECTION();
136
137
158
    return return_value;
138
158
}
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
97.3M
{
179
97.3M
    PyObject *return_value = NULL;
180
181
97.3M
    Py_BEGIN_CRITICAL_SECTION(self);
182
97.3M
    return_value = list_append_impl((PyListObject *)self, object);
183
97.3M
    Py_END_CRITICAL_SECTION();
184
185
97.3M
    return return_value;
186
97.3M
}
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
18.9M
{
227
18.9M
    PyObject *return_value = NULL;
228
18.9M
    Py_ssize_t index = -1;
229
230
18.9M
    if (!_PyArg_CheckPositional("pop", nargs, 0, 1)) {
231
0
        goto exit;
232
0
    }
233
18.9M
    if (nargs < 1) {
234
17.8M
        goto skip_optional;
235
17.8M
    }
236
1.14M
    {
237
1.14M
        Py_ssize_t ival = -1;
238
1.14M
        PyObject *iobj = _PyNumber_Index(args[0]);
239
1.14M
        if (iobj != NULL) {
240
1.14M
            ival = PyLong_AsSsize_t(iobj);
241
1.14M
            Py_DECREF(iobj);
242
1.14M
        }
243
1.14M
        if (ival == -1 && PyErr_Occurred()) {
244
0
            goto exit;
245
0
        }
246
1.14M
        index = ival;
247
1.14M
    }
248
18.9M
skip_optional:
249
18.9M
    Py_BEGIN_CRITICAL_SECTION(self);
250
18.9M
    return_value = list_pop_impl((PyListObject *)self, index);
251
18.9M
    Py_END_CRITICAL_SECTION();
252
253
18.9M
exit:
254
18.9M
    return return_value;
255
18.9M
}
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.27M
{
280
2.27M
    PyObject *return_value = NULL;
281
2.27M
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
282
283
2.27M
    #define NUM_KEYWORDS 2
284
2.27M
    static struct {
285
2.27M
        PyGC_Head _this_is_not_used;
286
2.27M
        PyObject_VAR_HEAD
287
2.27M
        Py_hash_t ob_hash;
288
2.27M
        PyObject *ob_item[NUM_KEYWORDS];
289
2.27M
    } _kwtuple = {
290
2.27M
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
291
2.27M
        .ob_hash = -1,
292
2.27M
        .ob_item = { &_Py_ID(key), &_Py_ID(reverse), },
293
2.27M
    };
294
2.27M
    #undef NUM_KEYWORDS
295
2.27M
    #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.27M
    static const char * const _keywords[] = {"key", "reverse", NULL};
302
2.27M
    static _PyArg_Parser _parser = {
303
2.27M
        .keywords = _keywords,
304
2.27M
        .fname = "sort",
305
2.27M
        .kwtuple = KWTUPLE,
306
2.27M
    };
307
2.27M
    #undef KWTUPLE
308
2.27M
    PyObject *argsbuf[2];
309
2.27M
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
310
2.27M
    PyObject *keyfunc = Py_None;
311
2.27M
    int reverse = 0;
312
313
2.27M
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
314
2.27M
            /*minpos*/ 0, /*maxpos*/ 0, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
315
2.27M
    if (!args) {
316
0
        goto exit;
317
0
    }
318
2.27M
    if (!noptargs) {
319
1.55M
        goto skip_optional_kwonly;
320
1.55M
    }
321
713k
    if (args[0]) {
322
713k
        keyfunc = args[0];
323
713k
        if (!--noptargs) {
324
713k
            goto skip_optional_kwonly;
325
713k
        }
326
713k
    }
327
249
    reverse = PyObject_IsTrue(args[1]);
328
249
    if (reverse < 0) {
329
0
        goto exit;
330
0
    }
331
2.27M
skip_optional_kwonly:
332
2.27M
    Py_BEGIN_CRITICAL_SECTION(self);
333
2.27M
    return_value = list_sort_impl((PyListObject *)self, keyfunc, reverse);
334
2.27M
    Py_END_CRITICAL_SECTION();
335
336
2.27M
exit:
337
2.27M
    return return_value;
338
2.27M
}
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
13.7k
{
449
13.7k
    PyObject *return_value = NULL;
450
451
13.7k
    Py_BEGIN_CRITICAL_SECTION(self);
452
13.7k
    return_value = list_remove_impl((PyListObject *)self, value);
453
13.7k
    Py_END_CRITICAL_SECTION();
454
455
13.7k
    return return_value;
456
13.7k
}
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
12.3M
{
473
12.3M
    int return_value = -1;
474
12.3M
    PyTypeObject *base_tp = &PyList_Type;
475
12.3M
    PyObject *iterable = NULL;
476
477
12.3M
    if ((Py_IS_TYPE(self, base_tp) ||
478
12.3M
         Py_TYPE(self)->tp_new == base_tp->tp_new) &&
479
12.3M
        !_PyArg_NoKeywords("list", kwargs)) {
480
0
        goto exit;
481
0
    }
482
12.3M
    if (!_PyArg_CheckPositional("list", PyTuple_GET_SIZE(args), 0, 1)) {
483
0
        goto exit;
484
0
    }
485
12.3M
    if (PyTuple_GET_SIZE(args) < 1) {
486
12.3M
        goto skip_optional;
487
12.3M
    }
488
0
    iterable = PyTuple_GET_ITEM(args, 0);
489
12.3M
skip_optional:
490
12.3M
    return_value = list___init___impl((PyListObject *)self, iterable);
491
492
12.3M
exit:
493
12.3M
    return return_value;
494
12.3M
}
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
41.7M
{
529
41.7M
    return list___reversed___impl((PyListObject *)self);
530
41.7M
}
531
/*[clinic end generated code: output=9eab9551fc4cea1c input=a9049054013a1b77]*/