Coverage Report

Created: 2025-07-04 06:49

/src/cpython/Objects/clinic/odictobject.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_modsupport.h"    // _PyArg_UnpackKeywords()
10
11
PyDoc_STRVAR(OrderedDict_fromkeys__doc__,
12
"fromkeys($type, /, iterable, value=None)\n"
13
"--\n"
14
"\n"
15
"Create a new ordered dictionary with keys from iterable and values set to value.");
16
17
#define ORDEREDDICT_FROMKEYS_METHODDEF    \
18
    {"fromkeys", _PyCFunction_CAST(OrderedDict_fromkeys), METH_FASTCALL|METH_KEYWORDS|METH_CLASS, OrderedDict_fromkeys__doc__},
19
20
static PyObject *
21
OrderedDict_fromkeys_impl(PyTypeObject *type, PyObject *seq, PyObject *value);
22
23
static PyObject *
24
OrderedDict_fromkeys(PyObject *type, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
25
0
{
26
0
    PyObject *return_value = NULL;
27
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
28
29
0
    #define NUM_KEYWORDS 2
30
0
    static struct {
31
0
        PyGC_Head _this_is_not_used;
32
0
        PyObject_VAR_HEAD
33
0
        Py_hash_t ob_hash;
34
0
        PyObject *ob_item[NUM_KEYWORDS];
35
0
    } _kwtuple = {
36
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
37
0
        .ob_hash = -1,
38
0
        .ob_item = { &_Py_ID(iterable), &_Py_ID(value), },
39
0
    };
40
0
    #undef NUM_KEYWORDS
41
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
42
43
    #else  // !Py_BUILD_CORE
44
    #  define KWTUPLE NULL
45
    #endif  // !Py_BUILD_CORE
46
47
0
    static const char * const _keywords[] = {"iterable", "value", NULL};
48
0
    static _PyArg_Parser _parser = {
49
0
        .keywords = _keywords,
50
0
        .fname = "fromkeys",
51
0
        .kwtuple = KWTUPLE,
52
0
    };
53
0
    #undef KWTUPLE
54
0
    PyObject *argsbuf[2];
55
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
56
0
    PyObject *seq;
57
0
    PyObject *value = Py_None;
58
59
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
60
0
            /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
61
0
    if (!args) {
62
0
        goto exit;
63
0
    }
64
0
    seq = args[0];
65
0
    if (!noptargs) {
66
0
        goto skip_optional_pos;
67
0
    }
68
0
    value = args[1];
69
0
skip_optional_pos:
70
0
    return_value = OrderedDict_fromkeys_impl((PyTypeObject *)type, seq, value);
71
72
0
exit:
73
0
    return return_value;
74
0
}
75
76
PyDoc_STRVAR(OrderedDict_setdefault__doc__,
77
"setdefault($self, /, key, default=None)\n"
78
"--\n"
79
"\n"
80
"Insert key with a value of default if key is not in the dictionary.\n"
81
"\n"
82
"Return the value for key if key is in the dictionary, else default.");
83
84
#define ORDEREDDICT_SETDEFAULT_METHODDEF    \
85
    {"setdefault", _PyCFunction_CAST(OrderedDict_setdefault), METH_FASTCALL|METH_KEYWORDS, OrderedDict_setdefault__doc__},
86
87
static PyObject *
88
OrderedDict_setdefault_impl(PyODictObject *self, PyObject *key,
89
                            PyObject *default_value);
90
91
static PyObject *
92
OrderedDict_setdefault(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
93
0
{
94
0
    PyObject *return_value = NULL;
95
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
96
97
0
    #define NUM_KEYWORDS 2
98
0
    static struct {
99
0
        PyGC_Head _this_is_not_used;
100
0
        PyObject_VAR_HEAD
101
0
        Py_hash_t ob_hash;
102
0
        PyObject *ob_item[NUM_KEYWORDS];
103
0
    } _kwtuple = {
104
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
105
0
        .ob_hash = -1,
106
0
        .ob_item = { &_Py_ID(key), &_Py_ID(default), },
107
0
    };
108
0
    #undef NUM_KEYWORDS
109
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
110
111
    #else  // !Py_BUILD_CORE
112
    #  define KWTUPLE NULL
113
    #endif  // !Py_BUILD_CORE
114
115
0
    static const char * const _keywords[] = {"key", "default", NULL};
116
0
    static _PyArg_Parser _parser = {
117
0
        .keywords = _keywords,
118
0
        .fname = "setdefault",
119
0
        .kwtuple = KWTUPLE,
120
0
    };
121
0
    #undef KWTUPLE
122
0
    PyObject *argsbuf[2];
123
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
124
0
    PyObject *key;
125
0
    PyObject *default_value = Py_None;
126
127
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
128
0
            /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
129
0
    if (!args) {
130
0
        goto exit;
131
0
    }
132
0
    key = args[0];
133
0
    if (!noptargs) {
134
0
        goto skip_optional_pos;
135
0
    }
136
0
    default_value = args[1];
137
0
skip_optional_pos:
138
0
    return_value = OrderedDict_setdefault_impl((PyODictObject *)self, key, default_value);
139
140
0
exit:
141
0
    return return_value;
142
0
}
143
144
PyDoc_STRVAR(OrderedDict_pop__doc__,
145
"pop($self, /, key, default=<unrepresentable>)\n"
146
"--\n"
147
"\n"
148
"od.pop(key[,default]) -> v, remove specified key and return the corresponding value.\n"
149
"\n"
150
"If the key is not found, return the default if given; otherwise,\n"
151
"raise a KeyError.");
152
153
#define ORDEREDDICT_POP_METHODDEF    \
154
    {"pop", _PyCFunction_CAST(OrderedDict_pop), METH_FASTCALL|METH_KEYWORDS, OrderedDict_pop__doc__},
155
156
static PyObject *
157
OrderedDict_pop_impl(PyODictObject *self, PyObject *key,
158
                     PyObject *default_value);
159
160
static PyObject *
161
OrderedDict_pop(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
162
0
{
163
0
    PyObject *return_value = NULL;
164
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
165
166
0
    #define NUM_KEYWORDS 2
167
0
    static struct {
168
0
        PyGC_Head _this_is_not_used;
169
0
        PyObject_VAR_HEAD
170
0
        Py_hash_t ob_hash;
171
0
        PyObject *ob_item[NUM_KEYWORDS];
172
0
    } _kwtuple = {
173
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
174
0
        .ob_hash = -1,
175
0
        .ob_item = { &_Py_ID(key), &_Py_ID(default), },
176
0
    };
177
0
    #undef NUM_KEYWORDS
178
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
179
180
    #else  // !Py_BUILD_CORE
181
    #  define KWTUPLE NULL
182
    #endif  // !Py_BUILD_CORE
183
184
0
    static const char * const _keywords[] = {"key", "default", NULL};
185
0
    static _PyArg_Parser _parser = {
186
0
        .keywords = _keywords,
187
0
        .fname = "pop",
188
0
        .kwtuple = KWTUPLE,
189
0
    };
190
0
    #undef KWTUPLE
191
0
    PyObject *argsbuf[2];
192
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
193
0
    PyObject *key;
194
0
    PyObject *default_value = NULL;
195
196
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
197
0
            /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
198
0
    if (!args) {
199
0
        goto exit;
200
0
    }
201
0
    key = args[0];
202
0
    if (!noptargs) {
203
0
        goto skip_optional_pos;
204
0
    }
205
0
    default_value = args[1];
206
0
skip_optional_pos:
207
0
    return_value = OrderedDict_pop_impl((PyODictObject *)self, key, default_value);
208
209
0
exit:
210
0
    return return_value;
211
0
}
212
213
PyDoc_STRVAR(OrderedDict_popitem__doc__,
214
"popitem($self, /, last=True)\n"
215
"--\n"
216
"\n"
217
"Remove and return a (key, value) pair from the dictionary.\n"
218
"\n"
219
"Pairs are returned in LIFO order if last is true or FIFO order if false.");
220
221
#define ORDEREDDICT_POPITEM_METHODDEF    \
222
    {"popitem", _PyCFunction_CAST(OrderedDict_popitem), METH_FASTCALL|METH_KEYWORDS, OrderedDict_popitem__doc__},
223
224
static PyObject *
225
OrderedDict_popitem_impl(PyODictObject *self, int last);
226
227
static PyObject *
228
OrderedDict_popitem(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
229
0
{
230
0
    PyObject *return_value = NULL;
231
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
232
233
0
    #define NUM_KEYWORDS 1
234
0
    static struct {
235
0
        PyGC_Head _this_is_not_used;
236
0
        PyObject_VAR_HEAD
237
0
        Py_hash_t ob_hash;
238
0
        PyObject *ob_item[NUM_KEYWORDS];
239
0
    } _kwtuple = {
240
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
241
0
        .ob_hash = -1,
242
0
        .ob_item = { &_Py_ID(last), },
243
0
    };
244
0
    #undef NUM_KEYWORDS
245
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
246
247
    #else  // !Py_BUILD_CORE
248
    #  define KWTUPLE NULL
249
    #endif  // !Py_BUILD_CORE
250
251
0
    static const char * const _keywords[] = {"last", NULL};
252
0
    static _PyArg_Parser _parser = {
253
0
        .keywords = _keywords,
254
0
        .fname = "popitem",
255
0
        .kwtuple = KWTUPLE,
256
0
    };
257
0
    #undef KWTUPLE
258
0
    PyObject *argsbuf[1];
259
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
260
0
    int last = 1;
261
262
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
263
0
            /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
264
0
    if (!args) {
265
0
        goto exit;
266
0
    }
267
0
    if (!noptargs) {
268
0
        goto skip_optional_pos;
269
0
    }
270
0
    last = PyObject_IsTrue(args[0]);
271
0
    if (last < 0) {
272
0
        goto exit;
273
0
    }
274
0
skip_optional_pos:
275
0
    return_value = OrderedDict_popitem_impl((PyODictObject *)self, last);
276
277
0
exit:
278
0
    return return_value;
279
0
}
280
281
PyDoc_STRVAR(OrderedDict_move_to_end__doc__,
282
"move_to_end($self, /, key, last=True)\n"
283
"--\n"
284
"\n"
285
"Move an existing element to the end (or beginning if last is false).\n"
286
"\n"
287
"Raise KeyError if the element does not exist.");
288
289
#define ORDEREDDICT_MOVE_TO_END_METHODDEF    \
290
    {"move_to_end", _PyCFunction_CAST(OrderedDict_move_to_end), METH_FASTCALL|METH_KEYWORDS, OrderedDict_move_to_end__doc__},
291
292
static PyObject *
293
OrderedDict_move_to_end_impl(PyODictObject *self, PyObject *key, int last);
294
295
static PyObject *
296
OrderedDict_move_to_end(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
297
0
{
298
0
    PyObject *return_value = NULL;
299
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
300
301
0
    #define NUM_KEYWORDS 2
302
0
    static struct {
303
0
        PyGC_Head _this_is_not_used;
304
0
        PyObject_VAR_HEAD
305
0
        Py_hash_t ob_hash;
306
0
        PyObject *ob_item[NUM_KEYWORDS];
307
0
    } _kwtuple = {
308
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
309
0
        .ob_hash = -1,
310
0
        .ob_item = { &_Py_ID(key), &_Py_ID(last), },
311
0
    };
312
0
    #undef NUM_KEYWORDS
313
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
314
315
    #else  // !Py_BUILD_CORE
316
    #  define KWTUPLE NULL
317
    #endif  // !Py_BUILD_CORE
318
319
0
    static const char * const _keywords[] = {"key", "last", NULL};
320
0
    static _PyArg_Parser _parser = {
321
0
        .keywords = _keywords,
322
0
        .fname = "move_to_end",
323
0
        .kwtuple = KWTUPLE,
324
0
    };
325
0
    #undef KWTUPLE
326
0
    PyObject *argsbuf[2];
327
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
328
0
    PyObject *key;
329
0
    int last = 1;
330
331
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
332
0
            /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
333
0
    if (!args) {
334
0
        goto exit;
335
0
    }
336
0
    key = args[0];
337
0
    if (!noptargs) {
338
0
        goto skip_optional_pos;
339
0
    }
340
0
    last = PyObject_IsTrue(args[1]);
341
0
    if (last < 0) {
342
0
        goto exit;
343
0
    }
344
0
skip_optional_pos:
345
0
    return_value = OrderedDict_move_to_end_impl((PyODictObject *)self, key, last);
346
347
0
exit:
348
0
    return return_value;
349
0
}
350
/*[clinic end generated code: output=7d8206823bb1f419 input=a9049054013a1b77]*/