Debian Codegraph @

Objects/clinic/odictobject.c.h

raw
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 {
27 PyObject *return_value = NULL;
28 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
29
30 #define NUM_KEYWORDS 2
31 static struct {
32 PyGC_Head _this_is_not_used;
33 PyObject_VAR_HEAD
34 Py_hash_t ob_hash;
35 PyObject *ob_item[NUM_KEYWORDS];
36 } _kwtuple = {
37 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
38 .ob_hash = -1,
39 .ob_item = { &_Py_ID(iterable), &_Py_ID(value), },
40 };
41 #undef NUM_KEYWORDS
42 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
43
44 #else // !Py_BUILD_CORE
45 # define KWTUPLE NULL
46 #endif // !Py_BUILD_CORE
47
48 static const char * const _keywords[] = {"iterable", "value", NULL};
49 static _PyArg_Parser _parser = {
50 .keywords = _keywords,
51 .fname = "fromkeys",
52 .kwtuple = KWTUPLE,
53 };
54 #undef KWTUPLE
55 PyObject *argsbuf[2];
56 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
57 PyObject *seq;
58 PyObject *value = Py_None;
59
60 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
61 /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
62 if (!args) {
63 goto exit;
64 }
65 seq = args[0];
66 if (!noptargs) {
67 goto skip_optional_pos;
68 }
69 value = args[1];
70 skip_optional_pos:
71 return_value = OrderedDict_fromkeys_impl((PyTypeObject *)type, seq, value);
72
73 exit:
74 return return_value;
75 }
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 {
91 PyObject *return_value = NULL;
92 Py_ssize_t _return_value;
93
94 Py_BEGIN_CRITICAL_SECTION(self);
95 _return_value = OrderedDict___sizeof___impl((PyODictObject *)self);
96 Py_END_CRITICAL_SECTION();
97 if ((_return_value == -1) && PyErr_Occurred()) {
98 goto exit;
99 }
100 return_value = PyLong_FromSsize_t(_return_value);
101
102 exit:
103 return return_value;
104 }
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 {
121 return OrderedDict___reduce___impl((PyODictObject *)od);
122 }
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 {
142 PyObject *return_value = NULL;
143 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
144
145 #define NUM_KEYWORDS 2
146 static struct {
147 PyGC_Head _this_is_not_used;
148 PyObject_VAR_HEAD
149 Py_hash_t ob_hash;
150 PyObject *ob_item[NUM_KEYWORDS];
151 } _kwtuple = {
152 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
153 .ob_hash = -1,
154 .ob_item = { &_Py_ID(key), &_Py_ID(default), },
155 };
156 #undef NUM_KEYWORDS
157 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
158
159 #else // !Py_BUILD_CORE
160 # define KWTUPLE NULL
161 #endif // !Py_BUILD_CORE
162
163 static const char * const _keywords[] = {"key", "default", NULL};
164 static _PyArg_Parser _parser = {
165 .keywords = _keywords,
166 .fname = "setdefault",
167 .kwtuple = KWTUPLE,
168 };
169 #undef KWTUPLE
170 PyObject *argsbuf[2];
171 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
172 PyObject *key;
173 PyObject *default_value = Py_None;
174
175 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
176 /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
177 if (!args) {
178 goto exit;
179 }
180 key = args[0];
181 if (!noptargs) {
182 goto skip_optional_pos;
183 }
184 default_value = args[1];
185 skip_optional_pos:
186 Py_BEGIN_CRITICAL_SECTION(self);
187 return_value = OrderedDict_setdefault_impl((PyODictObject *)self, key, default_value);
188 Py_END_CRITICAL_SECTION();
189
190 exit:
191 return return_value;
192 }
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 {
213 PyObject *return_value = NULL;
214 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
215
216 #define NUM_KEYWORDS 2
217 static struct {
218 PyGC_Head _this_is_not_used;
219 PyObject_VAR_HEAD
220 Py_hash_t ob_hash;
221 PyObject *ob_item[NUM_KEYWORDS];
222 } _kwtuple = {
223 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
224 .ob_hash = -1,
225 .ob_item = { &_Py_ID(key), &_Py_ID(default), },
226 };
227 #undef NUM_KEYWORDS
228 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
229
230 #else // !Py_BUILD_CORE
231 # define KWTUPLE NULL
232 #endif // !Py_BUILD_CORE
233
234 static const char * const _keywords[] = {"key", "default", NULL};
235 static _PyArg_Parser _parser = {
236 .keywords = _keywords,
237 .fname = "pop",
238 .kwtuple = KWTUPLE,
239 };
240 #undef KWTUPLE
241 PyObject *argsbuf[2];
242 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
243 PyObject *key;
244 PyObject *default_value = NULL;
245
246 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
247 /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
248 if (!args) {
249 goto exit;
250 }
251 key = args[0];
252 if (!noptargs) {
253 goto skip_optional_pos;
254 }
255 default_value = args[1];
256 skip_optional_pos:
257 Py_BEGIN_CRITICAL_SECTION(self);
258 return_value = OrderedDict_pop_impl((PyODictObject *)self, key, default_value);
259 Py_END_CRITICAL_SECTION();
260
261 exit:
262 return return_value;
263 }
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 {
282 PyObject *return_value = NULL;
283 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
284
285 #define NUM_KEYWORDS 1
286 static struct {
287 PyGC_Head _this_is_not_used;
288 PyObject_VAR_HEAD
289 Py_hash_t ob_hash;
290 PyObject *ob_item[NUM_KEYWORDS];
291 } _kwtuple = {
292 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
293 .ob_hash = -1,
294 .ob_item = { &_Py_ID(last), },
295 };
296 #undef NUM_KEYWORDS
297 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
298
299 #else // !Py_BUILD_CORE
300 # define KWTUPLE NULL
301 #endif // !Py_BUILD_CORE
302
303 static const char * const _keywords[] = {"last", NULL};
304 static _PyArg_Parser _parser = {
305 .keywords = _keywords,
306 .fname = "popitem",
307 .kwtuple = KWTUPLE,
308 };
309 #undef KWTUPLE
310 PyObject *argsbuf[1];
311 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
312 int last = 1;
313
314 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
315 /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
316 if (!args) {
317 goto exit;
318 }
319 if (!noptargs) {
320 goto skip_optional_pos;
321 }
322 last = PyObject_IsTrue(args[0]);
323 if (last < 0) {
324 goto exit;
325 }
326 skip_optional_pos:
327 Py_BEGIN_CRITICAL_SECTION(self);
328 return_value = OrderedDict_popitem_impl((PyODictObject *)self, last);
329 Py_END_CRITICAL_SECTION();
330
331 exit:
332 return return_value;
333 }
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 {
350 PyObject *return_value = NULL;
351
352 Py_BEGIN_CRITICAL_SECTION(self);
353 return_value = OrderedDict_clear_impl((PyODictObject *)self);
354 Py_END_CRITICAL_SECTION();
355
356 return return_value;
357 }
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 {
374 PyObject *return_value = NULL;
375
376 Py_BEGIN_CRITICAL_SECTION(od);
377 return_value = OrderedDict_copy_impl(od);
378 Py_END_CRITICAL_SECTION();
379
380 return return_value;
381 }
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 {
400 PyObject *return_value = NULL;
401 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
402
403 #define NUM_KEYWORDS 2
404 static struct {
405 PyGC_Head _this_is_not_used;
406 PyObject_VAR_HEAD
407 Py_hash_t ob_hash;
408 PyObject *ob_item[NUM_KEYWORDS];
409 } _kwtuple = {
410 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
411 .ob_hash = -1,
412 .ob_item = { &_Py_ID(key), &_Py_ID(last), },
413 };
414 #undef NUM_KEYWORDS
415 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
416
417 #else // !Py_BUILD_CORE
418 # define KWTUPLE NULL
419 #endif // !Py_BUILD_CORE
420
421 static const char * const _keywords[] = {"key", "last", NULL};
422 static _PyArg_Parser _parser = {
423 .keywords = _keywords,
424 .fname = "move_to_end",
425 .kwtuple = KWTUPLE,
426 };
427 #undef KWTUPLE
428 PyObject *argsbuf[2];
429 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
430 PyObject *key;
431 int last = 1;
432
433 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
434 /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
435 if (!args) {
436 goto exit;
437 }
438 key = args[0];
439 if (!noptargs) {
440 goto skip_optional_pos;
441 }
442 last = PyObject_IsTrue(args[1]);
443 if (last < 0) {
444 goto exit;
445 }
446 skip_optional_pos:
447 Py_BEGIN_CRITICAL_SECTION(self);
448 return_value = OrderedDict_move_to_end_impl((PyODictObject *)self, key, last);
449 Py_END_CRITICAL_SECTION();
450
451 exit:
452 return return_value;
453 }
454 /*[clinic end generated code: output=7bc997ca7900f06f input=a9049054013a1b77]*/