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
10
11 PyDoc_STRVAR(dict_fromkeys__doc__,
12 "fromkeys($type, iterable, value=None, /)\n"
13 "--\n"
14 "\n"
15 "Create a new dictionary with keys from iterable and values set to value.");
16
17 #define DICT_FROMKEYS_METHODDEF \
18 {"fromkeys", _PyCFunction_CAST(dict_fromkeys), METH_FASTCALL|METH_CLASS, dict_fromkeys__doc__},
19
20 static PyObject *
21 dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value);
22
23 static PyObject *
24 dict_fromkeys(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs)
25 {
26 PyObject *return_value = NULL;
27 PyObject *iterable;
28 PyObject *value = Py_None;
29
30 if (!_PyArg_CheckPositional("fromkeys", nargs, 1, 2)) {
31 goto exit;
32 }
33 iterable = args[0];
34 if (nargs < 2) {
35 goto skip_optional;
36 }
37 value = args[1];
38 skip_optional:
39 return_value = dict_fromkeys_impl(type, iterable, value);
40
41 exit:
42 return return_value;
43 }
44
45 PyDoc_STRVAR(dict___contains____doc__,
46 "__contains__($self, key, /)\n"
47 "--\n"
48 "\n"
49 "True if the dictionary has the specified key, else False.");
50
51 #define DICT___CONTAINS___METHODDEF \
52 {"__contains__", (PyCFunction)dict___contains__, METH_O|METH_COEXIST, dict___contains____doc__},
53
54 PyDoc_STRVAR(dict_get__doc__,
55 "get($self, key, default=None, /)\n"
56 "--\n"
57 "\n"
58 "Return the value for key if key is in the dictionary, else default.");
59
60 #define DICT_GET_METHODDEF \
61 {"get", _PyCFunction_CAST(dict_get), METH_FASTCALL, dict_get__doc__},
62
63 static PyObject *
64 dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value);
65
66 static PyObject *
67 dict_get(PyDictObject *self, PyObject *const *args, Py_ssize_t nargs)
68 {
69 PyObject *return_value = NULL;
70 PyObject *key;
71 PyObject *default_value = Py_None;
72
73 if (!_PyArg_CheckPositional("get", nargs, 1, 2)) {
74 goto exit;
75 }
76 key = args[0];
77 if (nargs < 2) {
78 goto skip_optional;
79 }
80 default_value = args[1];
81 skip_optional:
82 return_value = dict_get_impl(self, key, default_value);
83
84 exit:
85 return return_value;
86 }
87
88 PyDoc_STRVAR(dict_setdefault__doc__,
89 "setdefault($self, key, default=None, /)\n"
90 "--\n"
91 "\n"
92 "Insert key with a value of default if key is not in the dictionary.\n"
93 "\n"
94 "Return the value for key if key is in the dictionary, else default.");
95
96 #define DICT_SETDEFAULT_METHODDEF \
97 {"setdefault", _PyCFunction_CAST(dict_setdefault), METH_FASTCALL, dict_setdefault__doc__},
98
99 static PyObject *
100 dict_setdefault_impl(PyDictObject *self, PyObject *key,
101 PyObject *default_value);
102
103 static PyObject *
104 dict_setdefault(PyDictObject *self, PyObject *const *args, Py_ssize_t nargs)
105 {
106 PyObject *return_value = NULL;
107 PyObject *key;
108 PyObject *default_value = Py_None;
109
110 if (!_PyArg_CheckPositional("setdefault", nargs, 1, 2)) {
111 goto exit;
112 }
113 key = args[0];
114 if (nargs < 2) {
115 goto skip_optional;
116 }
117 default_value = args[1];
118 skip_optional:
119 return_value = dict_setdefault_impl(self, key, default_value);
120
121 exit:
122 return return_value;
123 }
124
125 PyDoc_STRVAR(dict_pop__doc__,
126 "pop($self, key, default=<unrepresentable>, /)\n"
127 "--\n"
128 "\n"
129 "D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n"
130 "\n"
131 "If the key is not found, return the default if given; otherwise,\n"
132 "raise a KeyError.");
133
134 #define DICT_POP_METHODDEF \
135 {"pop", _PyCFunction_CAST(dict_pop), METH_FASTCALL, dict_pop__doc__},
136
137 static PyObject *
138 dict_pop_impl(PyDictObject *self, PyObject *key, PyObject *default_value);
139
140 static PyObject *
141 dict_pop(PyDictObject *self, PyObject *const *args, Py_ssize_t nargs)
142 {
143 PyObject *return_value = NULL;
144 PyObject *key;
145 PyObject *default_value = NULL;
146
147 if (!_PyArg_CheckPositional("pop", nargs, 1, 2)) {
148 goto exit;
149 }
150 key = args[0];
151 if (nargs < 2) {
152 goto skip_optional;
153 }
154 default_value = args[1];
155 skip_optional:
156 return_value = dict_pop_impl(self, key, default_value);
157
158 exit:
159 return return_value;
160 }
161
162 PyDoc_STRVAR(dict_popitem__doc__,
163 "popitem($self, /)\n"
164 "--\n"
165 "\n"
166 "Remove and return a (key, value) pair as a 2-tuple.\n"
167 "\n"
168 "Pairs are returned in LIFO (last-in, first-out) order.\n"
169 "Raises KeyError if the dict is empty.");
170
171 #define DICT_POPITEM_METHODDEF \
172 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS, dict_popitem__doc__},
173
174 static PyObject *
175 dict_popitem_impl(PyDictObject *self);
176
177 static PyObject *
178 dict_popitem(PyDictObject *self, PyObject *Py_UNUSED(ignored))
179 {
180 return dict_popitem_impl(self);
181 }
182
183 PyDoc_STRVAR(dict___reversed____doc__,
184 "__reversed__($self, /)\n"
185 "--\n"
186 "\n"
187 "Return a reverse iterator over the dict keys.");
188
189 #define DICT___REVERSED___METHODDEF \
190 {"__reversed__", (PyCFunction)dict___reversed__, METH_NOARGS, dict___reversed____doc__},
191
192 static PyObject *
193 dict___reversed___impl(PyDictObject *self);
194
195 static PyObject *
196 dict___reversed__(PyDictObject *self, PyObject *Py_UNUSED(ignored))
197 {
198 return dict___reversed___impl(self);
199 }
200 /*[clinic end generated code: output=c0064abbea6091c5 input=a9049054013a1b77]*/