Coverage Report

Created: 2025-11-02 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Objects/clinic/odictobject.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_critical_section.h"// Py_BEGIN_CRITICAL_SECTION()
10
#include "pycore_modsupport.h"    // _PyArg_UnpackKeywords()
11
12
PyDoc_STRVAR(OrderedDict_fromkeys__doc__,
13
"fromkeys($type, /, iterable, value=None)\n"
14
"--\n"
15
"\n"
16
"Create a new ordered dictionary with keys from iterable and values set to value.");
17
18
#define ORDEREDDICT_FROMKEYS_METHODDEF    \
19
    {"fromkeys", _PyCFunction_CAST(OrderedDict_fromkeys), METH_FASTCALL|METH_KEYWORDS|METH_CLASS, OrderedDict_fromkeys__doc__},
20
21
static PyObject *
22
OrderedDict_fromkeys_impl(PyTypeObject *type, PyObject *seq, PyObject *value);
23
24
static PyObject *
25
OrderedDict_fromkeys(PyObject *type, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
26
0
{
27
0
    PyObject *return_value = NULL;
28
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
29
30
0
    #define NUM_KEYWORDS 2
31
0
    static struct {
32
0
        PyGC_Head _this_is_not_used;
33
0
        PyObject_VAR_HEAD
34
0
        Py_hash_t ob_hash;
35
0
        PyObject *ob_item[NUM_KEYWORDS];
36
0
    } _kwtuple = {
37
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
38
0
        .ob_hash = -1,
39
0
        .ob_item = { &_Py_ID(iterable), &_Py_ID(value), },
40
0
    };
41
0
    #undef NUM_KEYWORDS
42
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
43
44
    #else  // !Py_BUILD_CORE
45
    #  define KWTUPLE NULL
46
    #endif  // !Py_BUILD_CORE
47
48
0
    static const char * const _keywords[] = {"iterable", "value", NULL};
49
0
    static _PyArg_Parser _parser = {
50
0
        .keywords = _keywords,
51
0
        .fname = "fromkeys",
52
0
        .kwtuple = KWTUPLE,
53
0
    };
54
0
    #undef KWTUPLE
55
0
    PyObject *argsbuf[2];
56
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
57
0
    PyObject *seq;
58
0
    PyObject *value = Py_None;
59
60
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
61
0
            /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
62
0
    if (!args) {
63
0
        goto exit;
64
0
    }
65
0
    seq = args[0];
66
0
    if (!noptargs) {
67
0
        goto skip_optional_pos;
68
0
    }
69
0
    value = args[1];
70
0
skip_optional_pos:
71
0
    return_value = OrderedDict_fromkeys_impl((PyTypeObject *)type, seq, value);
72
73
0
exit:
74
0
    return return_value;
75
0
}
76
77
PyDoc_STRVAR(OrderedDict___sizeof____doc__,
78
"__sizeof__($self, /)\n"
79
"--\n"
80
"\n");
81
82
#define ORDEREDDICT___SIZEOF___METHODDEF    \
83
    {"__sizeof__", (PyCFunction)OrderedDict___sizeof__, METH_NOARGS, OrderedDict___sizeof____doc__},
84
85
static Py_ssize_t
86
OrderedDict___sizeof___impl(PyODictObject *self);
87
88
static PyObject *
89
OrderedDict___sizeof__(PyObject *self, PyObject *Py_UNUSED(ignored))
90
0
{
91
0
    PyObject *return_value = NULL;
92
0
    Py_ssize_t _return_value;
93
94
0
    Py_BEGIN_CRITICAL_SECTION(self);
95
0
    _return_value = OrderedDict___sizeof___impl((PyODictObject *)self);
96
0
    Py_END_CRITICAL_SECTION();
97
0
    if ((_return_value == -1) && PyErr_Occurred()) {
98
0
        goto exit;
99
0
    }
100
0
    return_value = PyLong_FromSsize_t(_return_value);
101
102
0
exit:
103
0
    return return_value;
104
0
}
105
106
PyDoc_STRVAR(OrderedDict___reduce____doc__,
107
"__reduce__($self, /)\n"
108
"--\n"
109
"\n"
110
"Return state information for pickling");
111
112
#define ORDEREDDICT___REDUCE___METHODDEF    \
113
    {"__reduce__", (PyCFunction)OrderedDict___reduce__, METH_NOARGS, OrderedDict___reduce____doc__},
114
115
static PyObject *
116
OrderedDict___reduce___impl(PyODictObject *od);
117
118
static PyObject *
119
OrderedDict___reduce__(PyObject *od, PyObject *Py_UNUSED(ignored))
120
0
{
121
0
    return OrderedDict___reduce___impl((PyODictObject *)od);
122
0
}
123
124
PyDoc_STRVAR(OrderedDict_setdefault__doc__,
125
"setdefault($self, /, key, default=None)\n"
126
"--\n"
127
"\n"
128
"Insert key with a value of default if key is not in the dictionary.\n"
129
"\n"
130
"Return the value for key if key is in the dictionary, else default.");
131
132
#define ORDEREDDICT_SETDEFAULT_METHODDEF    \
133
    {"setdefault", _PyCFunction_CAST(OrderedDict_setdefault), METH_FASTCALL|METH_KEYWORDS, OrderedDict_setdefault__doc__},
134
135
static PyObject *
136
OrderedDict_setdefault_impl(PyODictObject *self, PyObject *key,
137
                            PyObject *default_value);
138
139
static PyObject *
140
OrderedDict_setdefault(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
141
0
{
142
0
    PyObject *return_value = NULL;
143
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
144
145
0
    #define NUM_KEYWORDS 2
146
0
    static struct {
147
0
        PyGC_Head _this_is_not_used;
148
0
        PyObject_VAR_HEAD
149
0
        Py_hash_t ob_hash;
150
0
        PyObject *ob_item[NUM_KEYWORDS];
151
0
    } _kwtuple = {
152
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
153
0
        .ob_hash = -1,
154
0
        .ob_item = { &_Py_ID(key), &_Py_ID(default), },
155
0
    };
156
0
    #undef NUM_KEYWORDS
157
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
158
159
    #else  // !Py_BUILD_CORE
160
    #  define KWTUPLE NULL
161
    #endif  // !Py_BUILD_CORE
162
163
0
    static const char * const _keywords[] = {"key", "default", NULL};
164
0
    static _PyArg_Parser _parser = {
165
0
        .keywords = _keywords,
166
0
        .fname = "setdefault",
167
0
        .kwtuple = KWTUPLE,
168
0
    };
169
0
    #undef KWTUPLE
170
0
    PyObject *argsbuf[2];
171
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
172
0
    PyObject *key;
173
0
    PyObject *default_value = Py_None;
174
175
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
176
0
            /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
177
0
    if (!args) {
178
0
        goto exit;
179
0
    }
180
0
    key = args[0];
181
0
    if (!noptargs) {
182
0
        goto skip_optional_pos;
183
0
    }
184
0
    default_value = args[1];
185
0
skip_optional_pos:
186
0
    Py_BEGIN_CRITICAL_SECTION(self);
187
0
    return_value = OrderedDict_setdefault_impl((PyODictObject *)self, key, default_value);
188
0
    Py_END_CRITICAL_SECTION();
189
190
0
exit:
191
0
    return return_value;
192
0
}
193
194
PyDoc_STRVAR(OrderedDict_pop__doc__,
195
"pop($self, /, key, default=<unrepresentable>)\n"
196
"--\n"
197
"\n"
198
"od.pop(key[,default]) -> v, remove specified key and return the corresponding value.\n"
199
"\n"
200
"If the key is not found, return the default if given; otherwise,\n"
201
"raise a KeyError.");
202
203
#define ORDEREDDICT_POP_METHODDEF    \
204
    {"pop", _PyCFunction_CAST(OrderedDict_pop), METH_FASTCALL|METH_KEYWORDS, OrderedDict_pop__doc__},
205
206
static PyObject *
207
OrderedDict_pop_impl(PyODictObject *self, PyObject *key,
208
                     PyObject *default_value);
209
210
static PyObject *
211
OrderedDict_pop(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
212
0
{
213
0
    PyObject *return_value = NULL;
214
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
215
216
0
    #define NUM_KEYWORDS 2
217
0
    static struct {
218
0
        PyGC_Head _this_is_not_used;
219
0
        PyObject_VAR_HEAD
220
0
        Py_hash_t ob_hash;
221
0
        PyObject *ob_item[NUM_KEYWORDS];
222
0
    } _kwtuple = {
223
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
224
0
        .ob_hash = -1,
225
0
        .ob_item = { &_Py_ID(key), &_Py_ID(default), },
226
0
    };
227
0
    #undef NUM_KEYWORDS
228
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
229
230
    #else  // !Py_BUILD_CORE
231
    #  define KWTUPLE NULL
232
    #endif  // !Py_BUILD_CORE
233
234
0
    static const char * const _keywords[] = {"key", "default", NULL};
235
0
    static _PyArg_Parser _parser = {
236
0
        .keywords = _keywords,
237
0
        .fname = "pop",
238
0
        .kwtuple = KWTUPLE,
239
0
    };
240
0
    #undef KWTUPLE
241
0
    PyObject *argsbuf[2];
242
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
243
0
    PyObject *key;
244
0
    PyObject *default_value = NULL;
245
246
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
247
0
            /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
248
0
    if (!args) {
249
0
        goto exit;
250
0
    }
251
0
    key = args[0];
252
0
    if (!noptargs) {
253
0
        goto skip_optional_pos;
254
0
    }
255
0
    default_value = args[1];
256
0
skip_optional_pos:
257
0
    Py_BEGIN_CRITICAL_SECTION(self);
258
0
    return_value = OrderedDict_pop_impl((PyODictObject *)self, key, default_value);
259
0
    Py_END_CRITICAL_SECTION();
260
261
0
exit:
262
0
    return return_value;
263
0
}
264
265
PyDoc_STRVAR(OrderedDict_popitem__doc__,
266
"popitem($self, /, last=True)\n"
267
"--\n"
268
"\n"
269
"Remove and return a (key, value) pair from the dictionary.\n"
270
"\n"
271
"Pairs are returned in LIFO order if last is true or FIFO order if false.");
272
273
#define ORDEREDDICT_POPITEM_METHODDEF    \
274
    {"popitem", _PyCFunction_CAST(OrderedDict_popitem), METH_FASTCALL|METH_KEYWORDS, OrderedDict_popitem__doc__},
275
276
static PyObject *
277
OrderedDict_popitem_impl(PyODictObject *self, int last);
278
279
static PyObject *
280
OrderedDict_popitem(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
281
0
{
282
0
    PyObject *return_value = NULL;
283
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
284
285
0
    #define NUM_KEYWORDS 1
286
0
    static struct {
287
0
        PyGC_Head _this_is_not_used;
288
0
        PyObject_VAR_HEAD
289
0
        Py_hash_t ob_hash;
290
0
        PyObject *ob_item[NUM_KEYWORDS];
291
0
    } _kwtuple = {
292
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
293
0
        .ob_hash = -1,
294
0
        .ob_item = { &_Py_ID(last), },
295
0
    };
296
0
    #undef NUM_KEYWORDS
297
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
298
299
    #else  // !Py_BUILD_CORE
300
    #  define KWTUPLE NULL
301
    #endif  // !Py_BUILD_CORE
302
303
0
    static const char * const _keywords[] = {"last", NULL};
304
0
    static _PyArg_Parser _parser = {
305
0
        .keywords = _keywords,
306
0
        .fname = "popitem",
307
0
        .kwtuple = KWTUPLE,
308
0
    };
309
0
    #undef KWTUPLE
310
0
    PyObject *argsbuf[1];
311
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
312
0
    int last = 1;
313
314
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
315
0
            /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
316
0
    if (!args) {
317
0
        goto exit;
318
0
    }
319
0
    if (!noptargs) {
320
0
        goto skip_optional_pos;
321
0
    }
322
0
    last = PyObject_IsTrue(args[0]);
323
0
    if (last < 0) {
324
0
        goto exit;
325
0
    }
326
0
skip_optional_pos:
327
0
    Py_BEGIN_CRITICAL_SECTION(self);
328
0
    return_value = OrderedDict_popitem_impl((PyODictObject *)self, last);
329
0
    Py_END_CRITICAL_SECTION();
330
331
0
exit:
332
0
    return return_value;
333
0
}
334
335
PyDoc_STRVAR(OrderedDict_clear__doc__,
336
"clear($self, /)\n"
337
"--\n"
338
"\n"
339
"Remove all items from ordered dict.");
340
341
#define ORDEREDDICT_CLEAR_METHODDEF    \
342
    {"clear", (PyCFunction)OrderedDict_clear, METH_NOARGS, OrderedDict_clear__doc__},
343
344
static PyObject *
345
OrderedDict_clear_impl(PyODictObject *self);
346
347
static PyObject *
348
OrderedDict_clear(PyObject *self, PyObject *Py_UNUSED(ignored))
349
0
{
350
0
    PyObject *return_value = NULL;
351
352
0
    Py_BEGIN_CRITICAL_SECTION(self);
353
0
    return_value = OrderedDict_clear_impl((PyODictObject *)self);
354
0
    Py_END_CRITICAL_SECTION();
355
356
0
    return return_value;
357
0
}
358
359
PyDoc_STRVAR(OrderedDict_copy__doc__,
360
"copy($self, /)\n"
361
"--\n"
362
"\n"
363
"A shallow copy of ordered dict.");
364
365
#define ORDEREDDICT_COPY_METHODDEF    \
366
    {"copy", (PyCFunction)OrderedDict_copy, METH_NOARGS, OrderedDict_copy__doc__},
367
368
static PyObject *
369
OrderedDict_copy_impl(PyObject *od);
370
371
static PyObject *
372
OrderedDict_copy(PyObject *od, PyObject *Py_UNUSED(ignored))
373
0
{
374
0
    PyObject *return_value = NULL;
375
376
0
    Py_BEGIN_CRITICAL_SECTION(od);
377
0
    return_value = OrderedDict_copy_impl(od);
378
0
    Py_END_CRITICAL_SECTION();
379
380
0
    return return_value;
381
0
}
382
383
PyDoc_STRVAR(OrderedDict_move_to_end__doc__,
384
"move_to_end($self, /, key, last=True)\n"
385
"--\n"
386
"\n"
387
"Move an existing element to the end (or beginning if last is false).\n"
388
"\n"
389
"Raise KeyError if the element does not exist.");
390
391
#define ORDEREDDICT_MOVE_TO_END_METHODDEF    \
392
    {"move_to_end", _PyCFunction_CAST(OrderedDict_move_to_end), METH_FASTCALL|METH_KEYWORDS, OrderedDict_move_to_end__doc__},
393
394
static PyObject *
395
OrderedDict_move_to_end_impl(PyODictObject *self, PyObject *key, int last);
396
397
static PyObject *
398
OrderedDict_move_to_end(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
399
0
{
400
0
    PyObject *return_value = NULL;
401
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
402
403
0
    #define NUM_KEYWORDS 2
404
0
    static struct {
405
0
        PyGC_Head _this_is_not_used;
406
0
        PyObject_VAR_HEAD
407
0
        Py_hash_t ob_hash;
408
0
        PyObject *ob_item[NUM_KEYWORDS];
409
0
    } _kwtuple = {
410
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
411
0
        .ob_hash = -1,
412
0
        .ob_item = { &_Py_ID(key), &_Py_ID(last), },
413
0
    };
414
0
    #undef NUM_KEYWORDS
415
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
416
417
    #else  // !Py_BUILD_CORE
418
    #  define KWTUPLE NULL
419
    #endif  // !Py_BUILD_CORE
420
421
0
    static const char * const _keywords[] = {"key", "last", NULL};
422
0
    static _PyArg_Parser _parser = {
423
0
        .keywords = _keywords,
424
0
        .fname = "move_to_end",
425
0
        .kwtuple = KWTUPLE,
426
0
    };
427
0
    #undef KWTUPLE
428
0
    PyObject *argsbuf[2];
429
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
430
0
    PyObject *key;
431
0
    int last = 1;
432
433
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
434
0
            /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
435
0
    if (!args) {
436
0
        goto exit;
437
0
    }
438
0
    key = args[0];
439
0
    if (!noptargs) {
440
0
        goto skip_optional_pos;
441
0
    }
442
0
    last = PyObject_IsTrue(args[1]);
443
0
    if (last < 0) {
444
0
        goto exit;
445
0
    }
446
0
skip_optional_pos:
447
0
    Py_BEGIN_CRITICAL_SECTION(self);
448
0
    return_value = OrderedDict_move_to_end_impl((PyODictObject *)self, key, last);
449
0
    Py_END_CRITICAL_SECTION();
450
451
0
exit:
452
0
    return return_value;
453
0
}
454
/*[clinic end generated code: output=7bc997ca7900f06f input=a9049054013a1b77]*/