Debian Codegraph @

Modules/_testlimitedcapi/dict.c

raw
1 #include "parts.h"
2 #include "util.h"
3
4
5 static PyObject *
6 dict_check(PyObject *self, PyObject *obj)
7 {
8 NULLABLE(obj);
9 return PyLong_FromLong(PyDict_Check(obj));
10 }
11
12 static PyObject *
13 dict_checkexact(PyObject *self, PyObject *obj)
14 {
15 NULLABLE(obj);
16 return PyLong_FromLong(PyDict_CheckExact(obj));
17 }
18
19 static PyObject *
20 dict_new(PyObject *self, PyObject *Py_UNUSED(ignored))
21 {
22 return PyDict_New();
23 }
24
25 static PyObject *
26 dictproxy_new(PyObject *self, PyObject *obj)
27 {
28 NULLABLE(obj);
29 return PyDictProxy_New(obj);
30 }
31
32 static PyObject *
33 dict_clear(PyObject *self, PyObject *obj)
34 {
35 PyDict_Clear(obj);
36 Py_RETURN_NONE;
37 }
38
39 static PyObject *
40 dict_copy(PyObject *self, PyObject *obj)
41 {
42 NULLABLE(obj);
43 return PyDict_Copy(obj);
44 }
45
46 static PyObject *
47 dict_contains(PyObject *self, PyObject *args)
48 {
49 PyObject *obj, *key;
50 if (!PyArg_ParseTuple(args, "OO", &obj, &key)) {
51 return NULL;
52 }
53 NULLABLE(obj);
54 NULLABLE(key);
55 RETURN_INT(PyDict_Contains(obj, key));
56 }
57
58 static PyObject *
59 dict_size(PyObject *self, PyObject *obj)
60 {
61 NULLABLE(obj);
62 RETURN_SIZE(PyDict_Size(obj));
63 }
64
65 static PyObject *
66 dict_getitem(PyObject *self, PyObject *args)
67 {
68 PyObject *mapping, *key;
69 if (!PyArg_ParseTuple(args, "OO", &mapping, &key)) {
70 return NULL;
71 }
72 NULLABLE(mapping);
73 NULLABLE(key);
74 PyObject *value = PyDict_GetItem(mapping, key);
75 if (value == NULL) {
76 if (PyErr_Occurred()) {
77 return NULL;
78 }
79 return Py_NewRef(PyExc_KeyError);
80 }
81 return Py_NewRef(value);
82 }
83
84 static PyObject *
85 dict_getitemstring(PyObject *self, PyObject *args)
86 {
87 PyObject *mapping;
88 const char *key;
89 Py_ssize_t size;
90 if (!PyArg_ParseTuple(args, "Oz#", &mapping, &key, &size)) {
91 return NULL;
92 }
93 NULLABLE(mapping);
94 PyObject *value = PyDict_GetItemString(mapping, key);
95 if (value == NULL) {
96 if (PyErr_Occurred()) {
97 return NULL;
98 }
99 return Py_NewRef(PyExc_KeyError);
100 }
101 return Py_NewRef(value);
102 }
103
104 static PyObject *
105 dict_getitemwitherror(PyObject *self, PyObject *args)
106 {
107 PyObject *mapping, *key;
108 if (!PyArg_ParseTuple(args, "OO", &mapping, &key)) {
109 return NULL;
110 }
111 NULLABLE(mapping);
112 NULLABLE(key);
113 PyObject *value = PyDict_GetItemWithError(mapping, key);
114 if (value == NULL) {
115 if (PyErr_Occurred()) {
116 return NULL;
117 }
118 return Py_NewRef(PyExc_KeyError);
119 }
120 return Py_NewRef(value);
121 }
122
123
124 static PyObject *
125 dict_setitem(PyObject *self, PyObject *args)
126 {
127 PyObject *mapping, *key, *value;
128 if (!PyArg_ParseTuple(args, "OOO", &mapping, &key, &value)) {
129 return NULL;
130 }
131 NULLABLE(mapping);
132 NULLABLE(key);
133 NULLABLE(value);
134 RETURN_INT(PyDict_SetItem(mapping, key, value));
135 }
136
137 static PyObject *
138 dict_setitemstring(PyObject *self, PyObject *args)
139 {
140 PyObject *mapping, *value;
141 const char *key;
142 Py_ssize_t size;
143 if (!PyArg_ParseTuple(args, "Oz#O", &mapping, &key, &size, &value)) {
144 return NULL;
145 }
146 NULLABLE(mapping);
147 NULLABLE(value);
148 RETURN_INT(PyDict_SetItemString(mapping, key, value));
149 }
150
151 static PyObject *
152 dict_delitem(PyObject *self, PyObject *args)
153 {
154 PyObject *mapping, *key;
155 if (!PyArg_ParseTuple(args, "OO", &mapping, &key)) {
156 return NULL;
157 }
158 NULLABLE(mapping);
159 NULLABLE(key);
160 RETURN_INT(PyDict_DelItem(mapping, key));
161 }
162
163 static PyObject *
164 dict_delitemstring(PyObject *self, PyObject *args)
165 {
166 PyObject *mapping;
167 const char *key;
168 Py_ssize_t size;
169 if (!PyArg_ParseTuple(args, "Oz#", &mapping, &key, &size)) {
170 return NULL;
171 }
172 NULLABLE(mapping);
173 RETURN_INT(PyDict_DelItemString(mapping, key));
174 }
175
176 static PyObject *
177 dict_keys(PyObject *self, PyObject *obj)
178 {
179 NULLABLE(obj);
180 return PyDict_Keys(obj);
181 }
182
183 static PyObject *
184 dict_values(PyObject *self, PyObject *obj)
185 {
186 NULLABLE(obj);
187 return PyDict_Values(obj);
188 }
189
190 static PyObject *
191 dict_items(PyObject *self, PyObject *obj)
192 {
193 NULLABLE(obj);
194 return PyDict_Items(obj);
195 }
196
197 static PyObject *
198 dict_next(PyObject *self, PyObject *args)
199 {
200 PyObject *mapping, *key = UNINITIALIZED_PTR, *value = UNINITIALIZED_PTR;
201 Py_ssize_t pos;
202 if (!PyArg_ParseTuple(args, "On", &mapping, &pos)) {
203 return NULL;
204 }
205 NULLABLE(mapping);
206 int rc = PyDict_Next(mapping, &pos, &key, &value);
207 if (rc != 0) {
208 return Py_BuildValue("inOO", rc, pos, key, value);
209 }
210 assert(key == UNINITIALIZED_PTR);
211 assert(value == UNINITIALIZED_PTR);
212 if (PyErr_Occurred()) {
213 return NULL;
214 }
215 Py_RETURN_NONE;
216 }
217
218 static PyObject *
219 dict_merge(PyObject *self, PyObject *args)
220 {
221 PyObject *mapping, *mapping2;
222 int override;
223 if (!PyArg_ParseTuple(args, "OOi", &mapping, &mapping2, &override)) {
224 return NULL;
225 }
226 NULLABLE(mapping);
227 NULLABLE(mapping2);
228 RETURN_INT(PyDict_Merge(mapping, mapping2, override));
229 }
230
231 static PyObject *
232 dict_update(PyObject *self, PyObject *args)
233 {
234 PyObject *mapping, *mapping2;
235 if (!PyArg_ParseTuple(args, "OO", &mapping, &mapping2)) {
236 return NULL;
237 }
238 NULLABLE(mapping);
239 NULLABLE(mapping2);
240 RETURN_INT(PyDict_Update(mapping, mapping2));
241 }
242
243 static PyObject *
244 dict_mergefromseq2(PyObject *self, PyObject *args)
245 {
246 PyObject *mapping, *seq;
247 int override;
248 if (!PyArg_ParseTuple(args, "OOi", &mapping, &seq, &override)) {
249 return NULL;
250 }
251 NULLABLE(mapping);
252 NULLABLE(seq);
253 RETURN_INT(PyDict_MergeFromSeq2(mapping, seq, override));
254 }
255
256
257 static PyMethodDef test_methods[] = {
258 {"dict_check", dict_check, METH_O},
259 {"dict_checkexact", dict_checkexact, METH_O},
260 {"dict_new", dict_new, METH_NOARGS},
261 {"dictproxy_new", dictproxy_new, METH_O},
262 {"dict_clear", dict_clear, METH_O},
263 {"dict_copy", dict_copy, METH_O},
264 {"dict_size", dict_size, METH_O},
265 {"dict_getitem", dict_getitem, METH_VARARGS},
266 {"dict_getitemwitherror", dict_getitemwitherror, METH_VARARGS},
267 {"dict_getitemstring", dict_getitemstring, METH_VARARGS},
268 {"dict_contains", dict_contains, METH_VARARGS},
269 {"dict_setitem", dict_setitem, METH_VARARGS},
270 {"dict_setitemstring", dict_setitemstring, METH_VARARGS},
271 {"dict_delitem", dict_delitem, METH_VARARGS},
272 {"dict_delitemstring", dict_delitemstring, METH_VARARGS},
273 {"dict_keys", dict_keys, METH_O},
274 {"dict_values", dict_values, METH_O},
275 {"dict_items", dict_items, METH_O},
276 {"dict_next", dict_next, METH_VARARGS},
277 {"dict_merge", dict_merge, METH_VARARGS},
278 {"dict_update", dict_update, METH_VARARGS},
279 {"dict_mergefromseq2", dict_mergefromseq2, METH_VARARGS},
280 {NULL},
281 };
282
283 int
284 _PyTestLimitedCAPI_Init_Dict(PyObject *m)
285 {
286 if (PyModule_AddFunctions(m, test_methods) < 0) {
287 return -1;
288 }
289
290 return 0;
291 }