Esempio n. 1
0
def test_keys_doesnt_wrap():
    space = FakeSpace()
    space.newlist = None
    strategy = KwargsDictStrategy(space)
    keys = ["a", "b", "c"]
    values = [1, 2, 3]
    storage = strategy.erase((keys, values))
    d = W_DictMultiObject(space, strategy, storage)
    w_l = d.w_keys() # does not crash
Esempio n. 2
0
    def test_getcache_and_builtins(self):
        space = FakeSpace()
        strategy = ModuleDictStrategy(space)
        storage = strategy.get_empty_storage()
        builtindict = W_ModuleDictObject(space, strategy, storage)
        builtindict.setitem_str("len", 2)
        builtindict.setitem_str("list", 19)

        class FakeModule:
            w_dict = builtindict

        space.builtin = FakeModule()
        storage = strategy.get_empty_storage()
        d = W_ModuleDictObject(space, strategy, storage)

        # just in the builtins
        c = d.get_global_cache("len")
        assert c.cell is None
        assert c.builtincache.cell == 2

        # in both dicts
        d.setitem_str("list", 23)
        c = d.get_global_cache("list")
        assert c.cell == 23
        assert c.builtincache is None

        # not in the builtins but in the normal dict
        d.setitem_str("a", 45)
        c = d.get_global_cache("a")
        assert c.cell == 45
        assert c.builtincache is None

        # not in either dict
        c = d.get_global_cache("b")
        assert c.cell is None
        assert c.builtincache is None
Esempio n. 3
0
def test_delete():
    space = FakeSpace()
    d = SharedDictImplementation(space)
    d.setitem_str("a", 1)
    d.setitem_str("b", 2)
    d.setitem_str("c", 3)
    d.delitem("b")
    assert d.r_dict_content is None
    assert d.entries == [1, 3, None]
    assert d.structure.keys == {"a": 0, "c": 1}
    assert d.getitem("a") == 1
    assert d.getitem("c") == 3
    assert d.getitem("b") is None
    py.test.raises(KeyError, d.delitem, "b")

    d.delitem("c")
    assert d.entries == [1, None, None]
    assert d.structure.keys == {"a": 0}

    d.delitem("a")
    assert d.entries == [None, None, None]
    assert d.structure.keys == {}

    d = SharedDictImplementation(space)
    d.setitem_str("a", 1)
    d.setitem_str("b", 2)
    d.setitem_str("c", 3)
    d.setitem_str("d", 4)
    d.setitem_str("e", 5)
    d.setitem_str("f", 6)
    d.setitem_str("g", 7)
    d.setitem_str("h", 8)
    d.setitem_str("i", 9)
    d.delitem("d")
    assert d.entries == [1, 2, 3, 5, 6, 7, 8, 9, None]
    assert d.structure.keys == {
        "a": 0,
        "b": 1,
        "c": 2,
        "e": 3,
        "f": 4,
        "g": 5,
        "h": 6,
        "i": 7
    }
Esempio n. 4
0
from pypy.objspace.std.test.test_dictmultiobject import FakeSpace, W_DictMultiObject
from pypy.objspace.std.mapdict import *

class Config:
    class objspace:
        class std:
            withsmalldicts = False
            withcelldict = False
            withmethodcache = False
            withidentitydict = False
            withmapdict = True

space = FakeSpace()
space.config = Config

class Class(object):
    def __init__(self, hasdict=True):
        self.hasdict = True
        if hasdict:
            self.terminator = DictTerminator(space, self)
        else:
            self.terminator = NoDictTerminator(space, self)

    def instantiate(self, sp=None):
        if sp is None:
            sp = space
        result = Object()
        result.user_setup(sp, self)
        return result

class Object(Object):
Esempio n. 5
0
import py
from pypy.objspace.std.test.test_dictmultiobject import FakeSpace, W_DictMultiObject
from pypy.objspace.std.kwargsdict import *

space = FakeSpace()
strategy = KwargsDictStrategy(space)

def test_create():
    keys = ["a", "b", "c"]
    values = [1, 2, 3]
    storage = strategy.erase((keys, values))
    d = W_DictMultiObject(space, strategy, storage)
    assert d.getitem_str("a") == 1
    assert d.getitem_str("b") == 2
    assert d.getitem_str("c") == 3
    assert d.getitem(space.wrap("a")) == 1
    assert d.getitem(space.wrap("b")) == 2
    assert d.getitem(space.wrap("c")) == 3
    assert d.w_keys() == keys
    assert d.values() == values

def test_set_existing():
    keys = ["a", "b", "c"]
    values = [1, 2, 3]
    storage = strategy.erase((keys, values))
    d = W_DictMultiObject(space, strategy, storage)
    assert d.getitem_str("a") == 1
    assert d.getitem_str("b") == 2
    assert d.getitem_str("c") == 3
    assert d.setitem_str("a", 4) is None
    assert d.getitem_str("a") == 4
Esempio n. 6
0
 def setup_method(self, method):
     self.space = FakeSpace()
Esempio n. 7
0
class TestMixin(object):
    Mixin = make_inlinedict_mixin(StrDictImplementation, "content")

    class FakeObject(Mixin):
        def user_setup_slots(self, nslots):
            pass

    fakespace = FakeSpace()

    def make_obj(self):
        obj = self.FakeObject()
        obj.user_setup(self.fakespace, FakeSubtype)
        obj.setdictvalue(self.fakespace, "hello", 1)
        obj.setdictvalue(self.fakespace, "world", 2)
        assert obj._inlined_dict_valid()
        assert obj.w__dict__ is None
        return obj

    def test_setgetdel_dictvalue(self):
        obj = self.make_obj()
        assert obj.getdictvalue(self.fakespace, "hello") == 1
        assert obj.getdictvalue(self.fakespace, "world") == 2
        assert obj.getdictvalue(self.fakespace, "bla") is None
        assert not obj.deldictvalue(self.fakespace, "bla")
        obj.deldictvalue(self.fakespace, "world")
        assert obj.getdictvalue(self.fakespace, "world") is None
        obj.deldictvalue(self.fakespace, "hello")
        assert obj.getdictvalue(self.fakespace, "hello") is None

    def test_getdict(self):
        obj = self.make_obj()
        w_dict = obj.getdict()
        assert obj.getdict() is w_dict  # always get the same dict
        assert obj.w__dict__ is w_dict

        assert w_dict.getitem("hello") == 1
        assert w_dict.getitem("world") == 2
        w_dict.setitem("hello", 4)
        w_dict.setitem("world", 5)
        assert obj.getdictvalue(self.fakespace, "hello") == 4
        assert obj.getdictvalue(self.fakespace, "world") == 5

    def test_setdict(self):
        obj1 = self.make_obj()
        w_dict1 = obj1.getdict()
        obj2 = self.make_obj()
        w_dict2 = obj2.getdict()
        obj2.setdict(self.space, w_dict1)
        assert obj2.getdictvalue(self.fakespace, "hello") == 1
        assert obj2.getdictvalue(self.fakespace, "world") == 2
        obj1.setdictvalue(self.fakespace, "hello", 4)
        obj1.setdictvalue(self.fakespace, "world", 5)
        assert obj2.getdictvalue(self.fakespace, "hello") == 4
        assert obj2.getdictvalue(self.fakespace, "world") == 5
        assert w_dict2.getitem("hello") == 1
        assert w_dict2.getitem("world") == 2

    def test_setdict_keeps_previous_dict_working(self):
        obj1 = self.make_obj()
        w_dict1 = obj1.getdict()
        obj2 = self.make_obj()
        w_dict2 = obj2.getdict()
        w_dict2.setitem(4, 1)  # devolve dict
        w_dict2.setitem(5, 2)
        obj2.setdict(self.space, w_dict1)
        assert obj2.getdictvalue(self.fakespace, "hello") == 1
        assert obj2.getdictvalue(self.fakespace, "world") == 2
        obj1.setdictvalue(self.fakespace, "hello", 4)
        obj1.setdictvalue(self.fakespace, "world", 5)
        assert obj2.getdictvalue(self.fakespace, "hello") == 4
        assert obj2.getdictvalue(self.fakespace, "world") == 5

    def test_setdict_devolves_existing_dict(self):
        obj1 = self.make_obj()
        w_dict1 = obj1.getdict()
        obj2 = self.make_obj()
        obj2.setdictvalue(self.fakespace, "hello", 6)
        obj2.setdictvalue(self.fakespace, "world", 7)
        w_dict2 = obj2.getdict()
        obj2.setdict(self.space, w_dict1)
        assert w_dict2.getitem("hello") == 6
        assert w_dict2.getitem("world") == 7
        assert obj2.getdictvalue(self.fakespace, "hello") == 1
        assert obj2.getdictvalue(self.fakespace, "world") == 2
        obj1.setdictvalue(self.fakespace, "hello", 4)
        obj1.setdictvalue(self.fakespace, "world", 5)
        assert obj2.getdictvalue(self.fakespace, "hello") == 4
        assert obj2.getdictvalue(self.fakespace, "world") == 5

    def test_dict_devolves_via_dict(self):
        obj = self.make_obj()
        w_dict = obj.getdict()
        w_dict.setitem(4, 1)
        w_dict.setitem(5, 2)
        assert dict(w_dict.r_dict_content) == {
            4: 1,
            5: 2,
            "hello": 1,
            "world": 2
        }
        assert obj.getdictvalue(self.fakespace, "hello") == 1
        assert obj.getdictvalue(self.fakespace, "world") == 2
        assert obj.getdictvalue(self.fakespace, 4) == 1
        assert obj.getdictvalue(self.fakespace, 5) == 2
        obj.deldictvalue(self.fakespace, "world")
        assert obj.getdictvalue(self.fakespace, "world") is None
        obj.deldictvalue(self.fakespace, "hello")
        assert obj.getdictvalue(self.fakespace, "hello") is None
Esempio n. 8
0
from pypy.objspace.std.test.test_dictmultiobject import FakeSpace, W_DictObject
from pypy.objspace.std.mapdict import *


class Config:
    class objspace:
        class std:
            methodcachesizeexp = 11
            withmethodcachecounter = False


space = FakeSpace()
space.config = Config


class Class(object):
    def __init__(self, hasdict=True):
        self.hasdict = hasdict
        if hasdict:
            self.terminator = DictTerminator(space, self)
        else:
            self.terminator = NoDictTerminator(space, self)

    def instantiate(self, sp=None):
        if sp is None:
            sp = space
        if self.hasdict:
            result = Object()
        else:
            result = ObjectWithoutDict()
        result.user_setup(sp, self)
Esempio n. 9
0
 def BList(other=[]):
     return BListImplementation(FakeSpace(), other)