Debian Codegraph @

mypyc/lib-rt/dict_ops.c

raw
1 // Dict primitive operations
2 //
3 // These are registered in mypyc.primitives.dict_ops.
4
5 #include <Python.h>
6 #include "CPy.h"
7
8 #ifndef Py_TPFLAGS_MAPPING
9 #define Py_TPFLAGS_MAPPING (1 << 6)
10 #endif
11
12 // Dict subclasses like defaultdict override things in interesting
13 // ways, so we don't want to just directly use the dict methods. Not
14 // sure if it is actually worth doing all this stuff, but it saves
15 // some indirections.
16 PyObject *CPyDict_GetItem(PyObject *dict, PyObject *key) {
17 if (PyDict_CheckExact(dict)) {
18 PyObject *res;
19 int found = PyDict_GetItemRef(dict, key, &res);
20 if (found == 0) {
21 PyErr_SetObject(PyExc_KeyError, key);
22 }
23 return res;
24 } else {
25 return PyObject_GetItem(dict, key);
26 }
27 }
28
29 PyObject *CPyDict_Build(Py_ssize_t size, ...) {
30 Py_ssize_t i;
31
32 PyObject *res = _PyDict_NewPresized(size);
33 if (res == NULL) {
34 return NULL;
35 }
36
37 va_list args;
38 va_start(args, size);
39
40 for (i = 0; i < size; i++) {
41 PyObject *key = va_arg(args, PyObject *);
42 PyObject *value = va_arg(args, PyObject *);
43 if (PyDict_SetItem(res, key, value)) {
44 Py_DECREF(res);
45 return NULL;
46 }
47 }
48
49 va_end(args);
50 return res;
51 }
52
53 PyObject *CPyDict_Get(PyObject *dict, PyObject *key, PyObject *fallback) {
54 // We are dodgily assuming that get on a subclass doesn't have
55 // different behavior.
56 PyObject *res;
57 int found = PyDict_GetItemRef(dict, key, &res);
58 if (found < 0) {
59 return NULL;
60 }
61 if (found == 0) {
62 Py_INCREF(fallback);
63 return fallback;
64 }
65 return res;
66 }
67
68 PyObject *CPyDict_GetWithNone(PyObject *dict, PyObject *key) {
69 return CPyDict_Get(dict, key, Py_None);
70 }
71
72 PyObject *CPyDict_SetDefault(PyObject *dict, PyObject *key, PyObject *value) {
73 if (PyDict_CheckExact(dict)) {
74 PyObject* ret = PyDict_SetDefault(dict, key, value);
75 Py_XINCREF(ret);
76 return ret;
77 }
78 return PyObject_CallMethodObjArgs(dict, mypyc_interned_str.setdefault, key, value, NULL);
79 }
80
81 PyObject *CPyDict_SetDefaultWithNone(PyObject *dict, PyObject *key) {
82 return CPyDict_SetDefault(dict, key, Py_None);
83 }
84
85 PyObject *CPyDict_SetDefaultWithEmptyDatatype(PyObject *dict, PyObject *key,
86 int data_type) {
87 PyObject *res = CPyDict_GetItem(dict, key);
88 if (!res) {
89 // CPyDict_GetItem() would generates a PyExc_KeyError
90 // when key is not found.
91 PyErr_Clear();
92
93 PyObject *new_obj;
94 if (data_type == 1) {
95 new_obj = PyList_New(0);
96 } else if (data_type == 2) {
97 new_obj = PyDict_New();
98 } else if (data_type == 3) {
99 new_obj = PySet_New(NULL);
100 } else {
101 new_obj = NULL;
102 }
103
104 if (new_obj == NULL) {
105 res = NULL;
106 } else if (CPyDict_SetItem(dict, key, new_obj) == -1) {
107 Py_DECREF(new_obj);
108 res = NULL;
109 } else {
110 res = new_obj;
111 }
112 }
113 return res;
114 }
115
116 int CPyDict_SetItem(PyObject *dict, PyObject *key, PyObject *value) {
117 if (PyDict_CheckExact(dict)) {
118 return PyDict_SetItem(dict, key, value);
119 } else {
120 return PyObject_SetItem(dict, key, value);
121 }
122 }
123
124 static inline int CPy_ObjectToStatus(PyObject *obj) {
125 if (obj) {
126 Py_DECREF(obj);
127 return 0;
128 } else {
129 return -1;
130 }
131 }
132
133 static int CPyDict_UpdateGeneral(PyObject *dict, PyObject *stuff) {
134 PyObject *res = PyObject_CallMethodOneArg(dict, mypyc_interned_str.update, stuff);
135 return CPy_ObjectToStatus(res);
136 }
137
138 int CPyDict_UpdateInDisplay(PyObject *dict, PyObject *stuff) {
139 // from https://github.com/python/cpython/blob/55d035113dfb1bd90495c8571758f504ae8d4802/Python/ceval.c#L2710
140 int ret = PyDict_Update(dict, stuff);
141 if (ret < 0) {
142 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
143 PyErr_Format(PyExc_TypeError,
144 "'%.200s' object is not a mapping",
145 Py_TYPE(stuff)->tp_name);
146 }
147 }
148 return ret;
149 }
150
151 int CPyDict_Update(PyObject *dict, PyObject *stuff) {
152 if (PyDict_CheckExact(dict)) {
153 return PyDict_Update(dict, stuff);
154 } else {
155 return CPyDict_UpdateGeneral(dict, stuff);
156 }
157 }
158
159 int CPyDict_UpdateFromAny(PyObject *dict, PyObject *stuff) {
160 if (PyDict_CheckExact(dict)) {
161 // Argh this sucks
162 if (PyDict_Check(stuff) || PyObject_HasAttrWithError(stuff, mypyc_interned_str.keys) > 0) {
163 return PyDict_Update(dict, stuff);
164 } else {
165 return PyDict_MergeFromSeq2(dict, stuff, 1);
166 }
167 } else {
168 return CPyDict_UpdateGeneral(dict, stuff);
169 }
170 }
171
172 PyObject *CPyDict_FromAny(PyObject *obj) {
173 if (PyDict_Check(obj)) {
174 return PyDict_Copy(obj);
175 } else {
176 int res;
177 PyObject *dict = PyDict_New();
178 if (!dict) {
179 return NULL;
180 }
181 if (PyObject_HasAttrWithError(obj, mypyc_interned_str.keys) > 0) {
182 res = PyDict_Update(dict, obj);
183 } else {
184 res = PyDict_MergeFromSeq2(dict, obj, 1);
185 }
186 if (res < 0) {
187 Py_DECREF(dict);
188 return NULL;
189 }
190 return dict;
191 }
192 }
193
194 PyObject *CPyDict_KeysView(PyObject *dict) {
195 if (PyDict_CheckExact(dict)){
196 return _CPyDictView_New(dict, &PyDictKeys_Type);
197 }
198 return PyObject_CallMethodNoArgs(dict, mypyc_interned_str.keys);
199 }
200
201 PyObject *CPyDict_ValuesView(PyObject *dict) {
202 if (PyDict_CheckExact(dict)){
203 return _CPyDictView_New(dict, &PyDictValues_Type);
204 }
205 return PyObject_CallMethodNoArgs(dict, mypyc_interned_str.values);
206 }
207
208 PyObject *CPyDict_ItemsView(PyObject *dict) {
209 if (PyDict_CheckExact(dict)){
210 return _CPyDictView_New(dict, &PyDictItems_Type);
211 }
212 return PyObject_CallMethodNoArgs(dict, mypyc_interned_str.items);
213 }
214
215 PyObject *CPyDict_Keys(PyObject *dict) {
216 if (PyDict_CheckExact(dict)) {
217 return PyDict_Keys(dict);
218 }
219 // Inline generic fallback logic to also return a list.
220 PyObject *list = PyList_New(0);
221 PyObject *view = PyObject_CallMethodNoArgs(dict, mypyc_interned_str.keys);
222 if (view == NULL) {
223 return NULL;
224 }
225 int res = PyList_Extend(list, view);
226 Py_DECREF(view);
227 if (res < 0) {
228 return NULL;
229 }
230 return list;
231 }
232
233 PyObject *CPyDict_Values(PyObject *dict) {
234 if (PyDict_CheckExact(dict)) {
235 return PyDict_Values(dict);
236 }
237 // Inline generic fallback logic to also return a list.
238 PyObject *list = PyList_New(0);
239 PyObject *view = PyObject_CallMethodNoArgs(dict, mypyc_interned_str.values);
240 if (view == NULL) {
241 return NULL;
242 }
243 int res = PyList_Extend(list, view);
244 Py_DECREF(view);
245 if (res < 0) {
246 return NULL;
247 }
248 return list;
249 }
250
251 PyObject *CPyDict_Items(PyObject *dict) {
252 if (PyDict_CheckExact(dict)) {
253 return PyDict_Items(dict);
254 }
255 // Inline generic fallback logic to also return a list.
256 PyObject *list = PyList_New(0);
257 PyObject *view = PyObject_CallMethodNoArgs(dict, mypyc_interned_str.items);
258 if (view == NULL) {
259 return NULL;
260 }
261 int res = PyList_Extend(list, view);
262 Py_DECREF(view);
263 if (res < 0) {
264 return NULL;
265 }
266 return list;
267 }
268
269 char CPyDict_Clear(PyObject *dict) {
270 if (PyDict_CheckExact(dict)) {
271 PyDict_Clear(dict);
272 } else {
273 PyObject *res = PyObject_CallMethodNoArgs(dict, mypyc_interned_str.clear);
274 if (res == NULL) {
275 return 0;
276 }
277 Py_DECREF(res);
278 }
279 return 1;
280 }
281
282 PyObject *CPyDict_Copy(PyObject *dict) {
283 if (PyDict_CheckExact(dict)) {
284 return PyDict_Copy(dict);
285 }
286 return PyObject_CallMethodNoArgs(dict, mypyc_interned_str.copy);
287 }
288
289 PyObject *CPyDict_GetKeysIter(PyObject *dict) {
290 if (PyDict_CheckExact(dict)) {
291 // Return dict itself to indicate we can use fast path instead.
292 Py_INCREF(dict);
293 return dict;
294 }
295 return PyObject_GetIter(dict);
296 }
297
298 PyObject *CPyDict_GetItemsIter(PyObject *dict) {
299 if (PyDict_CheckExact(dict)) {
300 // Return dict itself to indicate we can use fast path instead.
301 Py_INCREF(dict);
302 return dict;
303 }
304 PyObject *view = PyObject_CallMethodNoArgs(dict, mypyc_interned_str.items);
305 if (view == NULL) {
306 return NULL;
307 }
308 PyObject *iter = PyObject_GetIter(view);
309 Py_DECREF(view);
310 return iter;
311 }
312
313 PyObject *CPyDict_GetValuesIter(PyObject *dict) {
314 if (PyDict_CheckExact(dict)) {
315 // Return dict itself to indicate we can use fast path instead.
316 Py_INCREF(dict);
317 return dict;
318 }
319 PyObject *view = PyObject_CallMethodNoArgs(dict, mypyc_interned_str.values);
320 if (view == NULL) {
321 return NULL;
322 }
323 PyObject *iter = PyObject_GetIter(view);
324 Py_DECREF(view);
325 return iter;
326 }
327
328 static void _CPyDict_FromNext(tuple_T3CIO *ret, PyObject *dict_iter) {
329 // Get next item from iterator and set "should continue" flag.
330 ret->f2 = PyIter_Next(dict_iter);
331 if (ret->f2 == NULL) {
332 ret->f0 = 0;
333 Py_INCREF(Py_None);
334 ret->f2 = Py_None;
335 } else {
336 ret->f0 = 1;
337 }
338 }
339
340 // Helpers for fast dictionary iteration, return a single tuple
341 // instead of writing to multiple registers, for exact dicts use
342 // the fast path, and fall back to generic iterator logic for subclasses.
343 tuple_T3CIO CPyDict_NextKey(PyObject *dict_or_iter, CPyTagged offset) {
344 tuple_T3CIO ret;
345 Py_ssize_t py_offset = CPyTagged_AsSsize_t(offset);
346 PyObject *dummy;
347
348 if (PyDict_CheckExact(dict_or_iter)) {
349 PyObject *key;
350 // PyDict_Next() returns a borrowed reference. On free-threaded builds,
351 // hold the dict lock until we have converted it to a strong reference.
352 Py_BEGIN_CRITICAL_SECTION(dict_or_iter);
353 ret.f0 = PyDict_Next(dict_or_iter, &py_offset, &key, &dummy);
354 if (ret.f0) {
355 ret.f2 = Py_NewRef(key);
356 }
357 Py_END_CRITICAL_SECTION();
358
359 if (ret.f0) {
360 ret.f1 = CPyTagged_FromSsize_t(py_offset);
361 } else {
362 // Set key to None, so mypyc can manage refcounts.
363 ret.f1 = 0;
364 ret.f2 = Py_None;
365 Py_INCREF(ret.f2);
366 }
367 } else {
368 // offset is dummy in this case, just use the old value.
369 ret.f1 = offset;
370 _CPyDict_FromNext(&ret, dict_or_iter);
371 }
372 return ret;
373 }
374
375 tuple_T3CIO CPyDict_NextValue(PyObject *dict_or_iter, CPyTagged offset) {
376 tuple_T3CIO ret;
377 Py_ssize_t py_offset = CPyTagged_AsSsize_t(offset);
378 PyObject *dummy;
379
380 if (PyDict_CheckExact(dict_or_iter)) {
381 PyObject *value;
382 // PyDict_Next() returns a borrowed reference. On free-threaded builds,
383 // hold the dict lock until we have converted it to a strong reference.
384 Py_BEGIN_CRITICAL_SECTION(dict_or_iter);
385 ret.f0 = PyDict_Next(dict_or_iter, &py_offset, &dummy, &value);
386 if (ret.f0) {
387 ret.f2 = Py_NewRef(value);
388 }
389 Py_END_CRITICAL_SECTION();
390
391 if (ret.f0) {
392 ret.f1 = CPyTagged_FromSsize_t(py_offset);
393 } else {
394 // Set value to None, so mypyc can manage refcounts.
395 ret.f1 = 0;
396 ret.f2 = Py_None;
397 Py_INCREF(ret.f2);
398 }
399 } else {
400 // offset is dummy in this case, just use the old value.
401 ret.f1 = offset;
402 _CPyDict_FromNext(&ret, dict_or_iter);
403 }
404 return ret;
405 }
406
407 tuple_T4CIOO CPyDict_NextItem(PyObject *dict_or_iter, CPyTagged offset) {
408 tuple_T4CIOO ret;
409 Py_ssize_t py_offset = CPyTagged_AsSsize_t(offset);
410
411 if (PyDict_CheckExact(dict_or_iter)) {
412 PyObject *key;
413 PyObject *value;
414 // PyDict_Next() returns borrowed references. On free-threaded builds,
415 // hold the dict lock until we have converted them to strong references.
416 Py_BEGIN_CRITICAL_SECTION(dict_or_iter);
417 ret.f0 = PyDict_Next(dict_or_iter, &py_offset, &key, &value);
418 if (ret.f0) {
419 ret.f2 = Py_NewRef(key);
420 ret.f3 = Py_NewRef(value);
421 }
422 Py_END_CRITICAL_SECTION();
423
424 if (ret.f0) {
425 ret.f1 = CPyTagged_FromSsize_t(py_offset);
426 } else {
427 // Set key and value to None, so mypyc can manage refcounts.
428 ret.f1 = 0;
429 ret.f2 = Py_None;
430 ret.f3 = Py_None;
431 Py_INCREF(ret.f2);
432 Py_INCREF(ret.f3);
433 }
434 } else {
435 ret.f1 = offset;
436 PyObject *item = PyIter_Next(dict_or_iter);
437 if (item == NULL || !PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
438 if (item != NULL) {
439 PyErr_SetString(PyExc_TypeError, "a tuple of length 2 expected");
440 Py_DECREF(item);
441 }
442 ret.f0 = 0;
443 ret.f2 = Py_None;
444 ret.f3 = Py_None;
445 Py_INCREF(ret.f2);
446 Py_INCREF(ret.f3);
447 } else {
448 ret.f0 = 1;
449 ret.f2 = PyTuple_GET_ITEM(item, 0);
450 ret.f3 = PyTuple_GET_ITEM(item, 1);
451 Py_INCREF(ret.f2);
452 Py_INCREF(ret.f3);
453 Py_DECREF(item);
454 return ret;
455 }
456 }
457 return ret;
458 }
459
460 int CPyMapping_Check(PyObject *obj) {
461 return Py_TYPE(obj)->tp_flags & Py_TPFLAGS_MAPPING;
462 }