File: test_dictobject.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (487 lines) | stat: -rw-r--r-- 17,425 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import py
from pytest import raises
from rpython.rtyper.lltypesystem import rffi, lltype
from pypy.module.cpyext.test.test_api import BaseApiTest, raises_w
from pypy.module.cpyext.api import Py_ssize_tP, PyObjectP, PyTypeObjectPtr
from pypy.module.cpyext.pyobject import make_ref, from_ref
from pypy.interpreter.error import OperationError
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
from pypy.module.cpyext.dictproxyobject import *
from pypy.module.cpyext.dictobject import *
from pypy.module.cpyext.pyobject import decref

class TestDictObject(BaseApiTest):
    def test_dict(self, space):
        d = PyDict_New(space)
        assert space.eq_w(d, space.newdict())

        assert space.eq_w(PyDict_GetItem(space, space.wrap({"a": 72}),
                                             space.wrap("a")),
                          space.wrap(72))

        PyDict_SetItem(space, d, space.wrap("c"), space.wrap(42))
        assert space.eq_w(space.getitem(d, space.wrap("c")),
                          space.wrap(42))

        space.setitem(d, space.wrap("name"), space.wrap(3))
        assert space.eq_w(PyDict_GetItem(space, d, space.wrap("name")),
                          space.wrap(3))

        space.delitem(d, space.wrap("name"))
        assert not PyDict_GetItem(space, d, space.wrap("name"))

        buf = rffi.str2charp("name")
        assert not PyDict_GetItemString(space, d, buf)
        rffi.free_charp(buf)

        assert PyDict_Contains(space, d, space.wrap("c"))
        assert not PyDict_Contains(space, d, space.wrap("z"))

        PyDict_DelItem(space, d, space.wrap("c"))
        with raises_w(space, KeyError):
            PyDict_DelItem(space, d, space.wrap("name"))
        assert PyDict_Size(space, d) == 0

        space.setitem(d, space.wrap("some_key"), space.wrap(3))
        buf = rffi.str2charp("some_key")
        PyDict_DelItemString(space, d, buf)
        assert PyDict_Size(space, d) == 0
        with raises_w(space, KeyError):
            PyDict_DelItemString(space, d, buf)
        rffi.free_charp(buf)

        d = space.wrap({'a': 'b'})
        PyDict_Clear(space, d)
        assert PyDict_Size(space, d) == 0

    def test_check(self, space):
        d = PyDict_New(space, )
        assert PyDict_Check(space, d)
        assert PyDict_CheckExact(space, d)
        sub = space.appexec([], """():
            class D(dict):
                pass
            return D""")
        d = space.call_function(sub)
        assert PyDict_Check(space, d)
        assert not PyDict_CheckExact(space, d)
        i = space.wrap(2)
        assert not PyDict_Check(space, i)
        assert not PyDict_CheckExact(space, i)

    def test_keys(self, space):
        w_d = space.newdict()
        space.setitem(w_d, space.wrap("a"), space.wrap("b"))

        assert space.eq_w(PyDict_Keys(space, w_d), space.wrap(["a"]))
        assert space.eq_w(PyDict_Values(space, w_d), space.wrap(["b"]))
        assert space.eq_w(PyDict_Items(space, w_d), space.wrap([("a", "b")]))

    def test_merge(self, space):
        w_d = space.newdict()
        space.setitem(w_d, space.wrap("a"), space.wrap("b"))

        w_d2 = space.newdict()
        space.setitem(w_d2, space.wrap("a"), space.wrap("c"))
        space.setitem(w_d2, space.wrap("c"), space.wrap("d"))
        space.setitem(w_d2, space.wrap("e"), space.wrap("f"))

        PyDict_Merge(space, w_d, w_d2, 0)
        assert space.unwrap(w_d) == dict(a='b', c='d', e='f')
        PyDict_Merge(space, w_d, w_d2, 1)
        assert space.unwrap(w_d) == dict(a='c', c='d', e='f')

    def test_update(self, space):
        w_d = space.newdict()
        space.setitem(w_d, space.wrap("a"), space.wrap("b"))

        w_d2 = PyDict_Copy(space, w_d)
        assert not space.is_w(w_d2, w_d)
        space.setitem(w_d, space.wrap("c"), space.wrap("d"))
        space.setitem(w_d2, space.wrap("e"), space.wrap("f"))

        PyDict_Update(space, w_d, w_d2)
        assert space.unwrap(w_d) == dict(a='b', c='d', e='f')

    def test_update_doesnt_accept_list_of_tuples(self, space):
        w_d = space.newdict()
        space.setitem(w_d, space.wrap("a"), space.wrap("b"))

        w_d2 = space.wrap([("c", "d"), ("e", "f")])

        with raises_w(space, AttributeError):
            PyDict_Update(space, w_d, w_d2)
        assert space.unwrap(w_d) == dict(a='b') # unchanged

    def test_dictproxy(self, space):
        w_dict = space.appexec([], """(): return {1: 2, 3: 4}""")
        w_proxy = PyDictProxy_New(space, w_dict)
        assert space.contains_w(w_proxy, space.newint(1))
        raises(OperationError, space.setitem,
               w_proxy, space.newint(1), space.w_None)
        raises(OperationError, space.delitem,
               w_proxy, space.newint(1))
        raises(OperationError, space.call_method, w_proxy, 'clear')
        assert PyDictProxy_Check(space, w_proxy)

    def test_typedict1(self, space):
        py_type = make_ref(space, space.w_int)
        py_dict = rffi.cast(PyTypeObjectPtr, py_type).c_tp_dict
        ppos = lltype.malloc(Py_ssize_tP.TO, 1, flavor='raw')

        ppos[0] = 0
        pkey = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
        pvalue = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
        try:
            w_copy = space.newdict()
            while PyDict_Next(space, py_dict, ppos, pkey, pvalue):
                w_key = from_ref(space, pkey[0])
                w_value = from_ref(space, pvalue[0])
                space.setitem(w_copy, w_key, w_value)
        finally:
            lltype.free(ppos, flavor='raw')
            lltype.free(pkey, flavor='raw')
            lltype.free(pvalue, flavor='raw')
        decref(space, py_type) # release borrowed references
        # do something with w_copy ?

class AppTestDictObject(AppTestCpythonExtensionBase):
    def test_dictproxytype(self):
        module = self.import_extension('foo', [
            ("dict_proxy", "METH_VARARGS",
             """
                 PyObject * dict;
                 PyObject * proxydict;
                 int i;
                 if (!PyArg_ParseTuple(args, "O", &dict))
                     return NULL;
                 proxydict = PyDictProxy_New(dict);
#ifdef PYPY_VERSION  // PyDictProxy_Check[Exact] are PyPy-specific.
                 if (!PyDictProxy_Check(proxydict)) {
                    Py_DECREF(proxydict);
                    PyErr_SetNone(PyExc_ValueError);
                    return NULL;
                 }
                 if (!PyDictProxy_CheckExact(proxydict)) {
                    Py_DECREF(proxydict);
                    PyErr_SetNone(PyExc_ValueError);
                    return NULL;
                 }
#endif  // PYPY_VERSION
                 i = PyObject_Size(proxydict);
                 Py_DECREF(proxydict);
                 return PyLong_FromLong(i);
             """),
            ])
        assert module.dict_proxy({'a': 1, 'b': 2}) == 2

    def test_getitemwitherror(self):
        module = self.import_extension('foo', [
            ("dict_getitem", "METH_VARARGS",
             """
             PyObject *d, *key, *result;
             if (!PyArg_ParseTuple(args, "OO", &d, &key)) {
                return NULL;
             }
             result = PyDict_GetItemWithError(d, key);
             if (result == NULL && !PyErr_Occurred())
                Py_RETURN_NONE;
             Py_XINCREF(result);
             return result;
             """),
            ("dict_getitem_string", "METH_VARARGS",
             """
             PyObject *d, *result;
             char * key;
             if (!PyArg_ParseTuple(args, "Os", &d, &key)) {
                return NULL;
             }
             result = _PyDict_GetItemStringWithError(d, key);
             if (result == NULL && !PyErr_Occurred())
                Py_RETURN_NONE;
             Py_XINCREF(result);
             return result;
             """)])
        d = {'foo': 'bar'}
        assert module.dict_getitem(d, 'foo') == 'bar'
        assert module.dict_getitem_string(d, 'foo') == 'bar'
        assert module.dict_getitem(d, 'missing') is None
        assert module.dict_getitem_string(d, 'missing') is None
        with raises(TypeError):
            module.dict_getitem(d, [])
        with raises(TypeError):
            module.dict_getitem_string(d, [])

    def test_setdefault(self):
        module = self.import_extension('foo', [
            ("setdefault", "METH_VARARGS",
             '''
             PyObject *d, *key, *defaultobj, *val;
             if (!PyArg_ParseTuple(args, "OOO", &d, &key, &defaultobj))
                 return NULL;
             val = PyDict_SetDefault(d, key, defaultobj);
             Py_XINCREF(val);
             return val;
             ''')])

        class Dict(dict):
            def setdefault(self, key, default):
                return 42

        d = Dict()
        assert module.setdefault(d, 'x', 1) == 1
        assert d['x'] == 1

    def test_update(self):
        module = self.import_extension('foo', [
            ("update", "METH_VARARGS",
             '''
             if (PyDict_Update(PyTuple_GetItem(args, 0), PyTuple_GetItem(args, 1)))
                return NULL;
             Py_RETURN_NONE;
             ''')])
        d = {"a": 1}
        module.update(d, {"c": 2})
        assert d == dict(a=1, c=2)
        d = {"a": 1}
        raises(AttributeError, module.update, d, [("c", 2)])

    def test_iter(self):
        module = self.import_extension('foo', [
            ("copy", "METH_O",
             '''
             Py_ssize_t pos = 0;
             PyObject *key, *value;
             PyObject* copy = PyDict_New();
             while (PyDict_Next(args, &pos, &key, &value))
             {
                if (PyDict_SetItem(copy, key, value) < 0)
                {
                    Py_DecRef(copy);
                    return NULL;
                }
             }
             return copy;
             ''')])
        d = {1: 'xyz', 3: 'abcd'}
        copy = module.copy(d)
        assert len(copy) == len(d)
        assert copy == d

    def test_iterkeys(self):
        module = self.import_extension('foo', [
            ("keys_and_values", "METH_O",
             '''
             Py_ssize_t pos = 0;
             PyObject *key, *value, *values;
             PyObject* keys = PyList_New(0);
             while (PyDict_Next(args, &pos, &key, NULL))
             {
                if (PyList_Append(keys, key) < 0)
                {
                    Py_DecRef(keys);
                    return NULL;
                }
             }
             pos = 0;
             values = PyList_New(0);
             while (PyDict_Next(args, &pos, NULL, &value))
             {
                if (PyList_Append(values, value) < 0)
                {
                    Py_DecRef(keys);
                    Py_DecRef(values);
                    return NULL;
                }
             }
             return Py_BuildValue("(NN)", keys, values);
             ''')])
        d = {1: 'xyz', 3: 'abcd'}
        assert module.keys_and_values(d) == (list(d.keys()), list(d.values()))

    def test_typedict2(self):
        module = self.import_extension('foo', [
            ("get_type_dict", "METH_O",
             '''
                PyObject* value = args->ob_type->tp_dict;
                if (value == NULL) value = Py_None;
                Py_INCREF(value);
                return value;
             '''),
            ])
        d = module.get_type_dict(1)
        assert d['real'].__get__(1, 1) == 1

    def test_advanced(self):
        module = self.import_extension('foo', [
            ("dict_len", "METH_O",
            '''
                int ret = args->ob_type->tp_as_mapping->mp_length(args);
                return PyLong_FromLong(ret);
            '''),
            ("dict_setitem", "METH_VARARGS",
            '''
                int ret;
                PyObject * dict = PyTuple_GetItem(args, 0);
                if (PyTuple_Size(args) < 3 || !dict ||
                        !dict->ob_type->tp_as_mapping ||
                        !dict->ob_type->tp_as_mapping->mp_ass_subscript)
                    return PyLong_FromLong(-1);
                ret = dict->ob_type->tp_as_mapping->mp_ass_subscript(
                        dict, PyTuple_GetItem(args, 1),
                        PyTuple_GetItem(args, 2));
                return PyLong_FromLong(ret);
            '''),
            ("dict_delitem", "METH_VARARGS",
            '''
                int ret;
                PyObject * dict = PyTuple_GetItem(args, 0);
                if (PyTuple_Size(args) < 2 || !dict ||
                        !dict->ob_type->tp_as_mapping ||
                        !dict->ob_type->tp_as_mapping->mp_ass_subscript)
                    return PyLong_FromLong(-1);
                ret = dict->ob_type->tp_as_mapping->mp_ass_subscript(
                        dict, PyTuple_GetItem(args, 1), NULL);
                return PyLong_FromLong(ret);
            '''),
            ("dict_next", "METH_VARARGS",
            '''
                PyObject *key, *value;
                PyObject *arg = NULL;
                Py_ssize_t pos = 0;
                int ret = 0;
                if ((PyArg_ParseTuple(args, "|O", &arg))) {
                    if (arg && PyDict_Check(arg)) {
                        while (PyDict_Next(arg, &pos, &key, &value))
                            ret ++;
                        /* test no crash if pos is not reset to 0*/
                        while (PyDict_Next(arg, &pos, &key, &value))
                            ret ++;
                    }
                }
                return PyLong_FromLong(ret);
            '''),
            ("exhaust_pos", "METH_O",
             """
                PyObject *key, *val;
                Py_ssize_t pos = 0;
                while (PyDict_Next(args, &pos, &key, &val)) {
                }
                return PyLong_FromLong(pos);
             """),
            ])
        d = {'a': 1, 'b':2}
        assert module.dict_len(d) == 2
        assert module.dict_setitem(d, 'a', 'c') == 0
        assert d['a'] == 'c'
        assert module.dict_delitem(d, 'a') == 0
        r = module.dict_next({'a': 1, 'b': 2})
        assert r == 2
        assert module.exhaust_pos({'a': 1}) == 1

    def test_subclassing(self):
        module = self.import_extension('foo', [
            ("dict_setitem", "METH_VARARGS",
             """
             PyObject *d, *key, *value;
             if (!PyArg_ParseTuple(args, "OOO", &d, &key, &value)) {
                return NULL;
             }
             if (PyDict_SetItem(d, key, value) < 0) {
                return NULL;
             }
             Py_RETURN_NONE;
             """),
            ("dict_delitem", "METH_VARARGS",
             """
             PyObject *d, *key;
             if (!PyArg_ParseTuple(args, "OO", &d, &key)) {
                return NULL;
             }
             if (PyDict_DelItem(d, key) < 0) {
                return NULL;
             }
             Py_RETURN_NONE;
             """),
            ("dict_getitem", "METH_VARARGS",
             """
             PyObject *d, *key, *result;
             if (!PyArg_ParseTuple(args, "OO", &d, &key)) {
                return NULL;
             }
             result = PyDict_GetItem(d, key);
             Py_XINCREF(result);
             return result;
             """),
        ])

        class mydict(dict):
            def __setitem__(self, key, value):
                dict.__setitem__(self, key, 42)

            def __delitem__(self, key):
                dict.__setitem__(self, key, None)
        d = {}
        module.dict_setitem(d, 1, 2)
        assert d[1] == 2
        d = mydict()
        d[1] = 2
        assert d[1] == 42
        module.dict_setitem(d, 2, 3)
        assert d[2] == 3
        del d[2]
        assert d[2] is None
        module.dict_delitem(d, 2)
        assert 2 not in d

        class mydict2(dict):
            def __getitem__(self, key):
                return 42

        d = mydict2()
        d[1] = 2
        assert d[1] == 42
        assert module.dict_getitem(d, 1) == 2

    def test_getitem_error(self):
        module = self.import_extension('foo', [
            ("dict_getitem", "METH_VARARGS",
             """
             PyObject *d, *key, *result;
             if (!PyArg_ParseTuple(args, "OO", &d, &key)) {
                return NULL;
             }
             result = PyDict_GetItem(d, key);
             if (!result) Py_RETURN_NONE;
             Py_XINCREF(result);
             return result;
             """),
        ])
        assert module.dict_getitem(42, 43) is None
        assert module.dict_getitem({}, []) is None

    def test_contains(self):
        module = self.import_extension('foo', [
            ("contains", "METH_VARARGS",
             """
             PyObject *d, *key;
             if (!PyArg_ParseTuple(args, "OO", &d, &key)) {
                return NULL;
             }
             int ret = PyDict_Contains(d, key);
             if (ret < 0) return NULL;
             return PyLong_FromLong(ret);
             """),
        ])

        class Unhashable():
            __hash__ = None

        raises(TypeError, module.contains, {}, Unhashable())

        class C(dict):
            def __contains__(self, key):
                return dict.__contains__(self, key)

        ret = module.contains(C(), 1)
        assert ret == 0