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