Example #1
0
    def test_in(self):
        from __pypy__ import identity_dict
        d = identity_dict()
        d[None] = 1

        assert None in d
        assert [] not in d
Example #2
0
    def test_in(self):
        from __pypy__ import identity_dict
        d = identity_dict()
        d[None] = 1

        assert None in d
        assert [] not in d
Example #3
0
    def test_keys(self):
        from __pypy__ import identity_dict
        d = identity_dict()
        d[[]] = 1
        d[[]] = 2
        d[[]] = 3

        assert d.keys() == [[], [], []]
        assert sorted(d.values()) == [1, 2, 3]
Example #4
0
    def test_get(self):
        from __pypy__ import identity_dict
        d = identity_dict()
        d[None] = 1

        assert d.get(None, 42) == 1
        assert d.get(None) == 1
        assert d.get(1) is None
        assert d.get(1, 42) == 42
Example #5
0
    def test_keys(self):
        from __pypy__ import identity_dict
        d = identity_dict()
        d[[]] = 1
        d[[]] = 2
        d[[]] = 3

        assert d.keys() == [[], [], []]
        assert sorted(d.values()) == [1, 2, 3]
Example #6
0
    def test_get(self):
        from __pypy__ import identity_dict
        d = identity_dict()
        d[None] = 1

        assert d.get(None, 42) == 1
        assert d.get(None) == 1
        assert d.get(1) is None
        assert d.get(1, 42) == 42
Example #7
0
    def test_numbers(self):
        from __pypy__ import identity_dict
        d = identity_dict()
        d[0] = 1
        d[0.0] = 2

        assert d
        assert len(d) == 2
        del d[0]
        d.clear()
        assert not d
Example #8
0
    def test_unhashable(self):
        from __pypy__ import identity_dict

        d = identity_dict()
        d[[]] = 1
        d[[]] = 2
        a = []
        d[a] = 3
        assert len(d) == 3
        d[a] = 4
        assert len(d) == 3
        assert d[a] == 4

        raises(KeyError, d.__getitem__, [])
Example #9
0
    def test_unhashable(self):
        from __pypy__ import identity_dict

        d = identity_dict()
        d[[]] = 1
        d[[]] = 2
        a = []
        d[a] = 3
        assert len(d) == 3
        d[a] = 4
        assert len(d) == 3
        assert d[a] == 4

        raises(KeyError, d.__getitem__, [])
Example #10
0
    py_item = lib.PyObject_GetItem(py_elem, py_zero)
    py_locals = ffi.gc(lib.PyDict_New(), lib.Py_DECREF)
    py_globals = ffi.gc(lib.PyDict_New(), lib.Py_DECREF)
    py_bltns = lib.PyEval_GetBuiltins()
    lib.PyDict_SetItemString(py_globals, '__builtins__', py_bltns)
    py_res = lib.PyEval_EvalCode(py_item, py_globals, py_locals)
    return MetabiosisWrapper(py_res, noconvert=noconvert)

ApplevelWrapped = applevel('''
class ApplevelWrapped(object):
    pass
return ApplevelWrapped
''', noconvert=True)

_applevel_by_obj = {}
_applevel_by_unhashable_obj = identity_dict()
_obj_by_applevel = {}

def convert_unknown(obj):
    try:
        aw = _applevel_by_obj.get(obj)
    except TypeError:
        aw = _applevel_by_unhashable_obj.get(obj)
    if aw is None:
        aw = ApplevelWrapped().obj
        try:
            _applevel_by_obj[obj] = aw
        except TypeError:
            _applevel_by_unhashable_obj[obj] = aw
        _obj_by_applevel[aw] = obj
    lib.Py_INCREF(aw)
Example #11
0
 def test_iterate(self):
     from __pypy__ import identity_dict
     d = identity_dict()
     d[None] = 1
     raises(TypeError, iter, d)
     raises(TypeError, list, d)
Example #12
0
    py_globals = ffi.gc(lib.PyDict_New(), lib.Py_DECREF)
    py_bltns = lib.PyEval_GetBuiltins()
    lib.PyDict_SetItemString(py_globals, '__builtins__', py_bltns)
    py_res = lib.PyEval_EvalCode(py_item, py_globals, py_locals)
    return MetabiosisWrapper(py_res, noconvert=noconvert)


ApplevelWrapped = applevel('''
class ApplevelWrapped(object):
    pass
return ApplevelWrapped
''',
                           noconvert=True)

_applevel_by_obj = {}
_applevel_by_unhashable_obj = identity_dict()
_obj_by_applevel = {}


def convert_unknown(obj):
    try:
        aw = _applevel_by_obj.get(obj)
    except TypeError:
        aw = _applevel_by_unhashable_obj.get(obj)
    if aw is None:
        aw = ApplevelWrapped()._cpyobj
        try:
            _applevel_by_obj[obj] = aw
        except TypeError:
            _applevel_by_unhashable_obj[obj] = aw
        _obj_by_applevel[aw] = obj