Exemplo n.º 1
0
    def test_basic_property_cells(self):
        strategy = ModuleDictStrategy(space)
        storage = strategy.get_empty_storage()
        d = W_ModuleDictObject(space, strategy, storage)

        v1 = strategy.version
        key = "a"
        w_key = self.FakeString(key)
        d.setitem(w_key, 1)
        v2 = strategy.version
        assert v1 is not v2
        assert d.getitem(w_key) == 1
        assert d.get_strategy().getdictvalue_no_unwrapping(d, key) == 1

        d.setitem(w_key, 2)
        v3 = strategy.version
        assert v2 is not v3
        assert d.getitem(w_key) == 2
        assert d.get_strategy().getdictvalue_no_unwrapping(d, key).w_value == 2

        d.setitem(w_key, 3)
        v4 = strategy.version
        assert v3 is v4
        assert d.getitem(w_key) == 3
        assert d.get_strategy().getdictvalue_no_unwrapping(d, key).w_value == 3

        d.delitem(w_key)
        v5 = strategy.version
        assert v5 is not v4
        assert d.getitem(w_key) is None
        assert d.get_strategy().getdictvalue_no_unwrapping(d, key) is None
Exemplo n.º 2
0
    def test_basic_property(self):
        strategy = ModuleDictStrategy(space)
        storage = strategy.get_empty_storage()
        d = W_DictMultiObject(space, strategy, storage)

        # replace getcell with getcell from strategy
        def f(key, makenew):
            return strategy.getcell(d, key, makenew)
        d.getcell = f

        d.setitem("a", 1)
        assert d.getcell("a", False) is d.getcell("a", False)
        acell = d.getcell("a", False)
        d.setitem("b", 2)
        assert d.getcell("b", False) is d.getcell("b", False)
        assert d.getcell("c", True) is d.getcell("c", True)

        assert d.getitem("a") == 1
        assert d.getitem("b") == 2

        d.delitem("a")
        py.test.raises(KeyError, d.delitem, "a")
        assert d.getitem("a") is None
        assert d.getcell("a", False) is acell
        assert d.length() == 1

        d.clear()
        assert d.getitem("a") is None
        assert d.getcell("a", False) is acell
        assert d.length() == 0
Exemplo n.º 3
0
    def test_basic_property_cells(self):
        strategy = ModuleDictStrategy(space)
        storage = strategy.get_empty_storage()
        d = W_ModuleDictObject(space, strategy, storage)

        v1 = strategy.version
        key = "a"
        w_key = self.FakeString(key)
        d.setitem(w_key, 1)
        v2 = strategy.version
        assert v1 is not v2
        assert d.getitem(w_key) == 1
        assert d.get_strategy().getdictvalue_no_unwrapping(d, key) == 1

        d.setitem(w_key, 2)
        v3 = strategy.version
        assert v2 is not v3
        assert d.getitem(w_key) == 2
        assert d.get_strategy().getdictvalue_no_unwrapping(d, key).w_value == 2

        d.setitem(w_key, 3)
        v4 = strategy.version
        assert v3 is v4
        assert d.getitem(w_key) == 3
        assert d.get_strategy().getdictvalue_no_unwrapping(d, key).w_value == 3

        d.delitem(w_key)
        v5 = strategy.version
        assert v5 is not v4
        assert d.getitem(w_key) is None
        assert d.get_strategy().getdictvalue_no_unwrapping(d, key) is None
Exemplo n.º 4
0
    def allocate_and_init_instance(space, w_type=None, module=False,
                                   instance=False, strdict=False,
                                   kwargs=False):
        if space.config.objspace.std.withcelldict and module:
            from pypy.objspace.std.celldict import ModuleDictStrategy
            assert w_type is None
            # every module needs its own strategy, because the strategy stores
            # the version tag
            strategy = ModuleDictStrategy(space)
        elif space.config.objspace.std.withmapdict and instance:
            from pypy.objspace.std.mapdict import MapDictStrategy
            strategy = space.fromcache(MapDictStrategy)
        elif instance or strdict or module:
            assert w_type is None
            strategy = space.fromcache(StringDictStrategy)
        elif kwargs:
            assert w_type is None
            from pypy.objspace.std.kwargsdict import EmptyKwargsDictStrategy
            strategy = space.fromcache(EmptyKwargsDictStrategy)
        else:
            strategy = space.fromcache(EmptyDictStrategy)
        if w_type is None:
            w_type = space.w_dict

        storage = strategy.get_empty_storage()
        w_self = space.allocate_instance(W_DictMultiObject, w_type)
        W_DictMultiObject.__init__(w_self, space, strategy, storage)
        return w_self
Exemplo n.º 5
0
    def test_basic_property_cells(self):
        strategy = ModuleDictStrategy(space)
        storage = strategy.get_empty_storage()
        d = W_DictMultiObject(space, strategy, storage)

        v1 = strategy.version
        d.setitem("a", 1)
        v2 = strategy.version
        assert v1 is not v2
        assert d.getitem("a") == 1
        assert d.strategy.getdictvalue_no_unwrapping(d, "a") == 1

        d.setitem("a", 2)
        v3 = strategy.version
        assert v2 is not v3
        assert d.getitem("a") == 2
        assert d.strategy.getdictvalue_no_unwrapping(d, "a").w_value == 2

        d.setitem("a", 3)
        v4 = strategy.version
        assert v3 is v4
        assert d.getitem("a") == 3
        assert d.strategy.getdictvalue_no_unwrapping(d, "a").w_value == 3

        d.delitem("a")
        v5 = strategy.version
        assert v5 is not v4
        assert d.getitem("a") is None
        assert d.strategy.getdictvalue_no_unwrapping(d, "a") is None
Exemplo n.º 6
0
 def moduledict_and_key(self):
     strategy = ModuleDictStrategy(space)
     storage = strategy.get_empty_storage()
     d = W_ModuleDictObject(space, strategy, storage)
     key = "a"
     w_key = self.FakeString(key)
     return d, key, w_key
Exemplo n.º 7
0
    def allocate_and_init_instance(space, w_type=None, module=False,
                                   instance=False, strdict=False, kwargs=False):

        if space.config.objspace.std.withcelldict and module:
            from pypy.objspace.std.celldict import ModuleDictStrategy
            assert w_type is None
            # every module needs its own strategy, because the strategy stores
            # the version tag
            strategy = ModuleDictStrategy(space)

        elif instance or strdict or module:
            assert w_type is None
            strategy = space.fromcache(StringDictStrategy)

        elif kwargs:
            assert w_type is None
            from pypy.objspace.std.kwargsdict import KwargsDictStrategy
            strategy = space.fromcache(KwargsDictStrategy)
        else:
            strategy = space.fromcache(EmptyDictStrategy)
        if w_type is None:
            w_type = space.w_dict

        storage = strategy.get_empty_storage()
        w_self = space.allocate_instance(W_DictMultiObject, w_type)
        W_DictMultiObject.__init__(w_self, space, strategy, storage)
        return w_self
Exemplo n.º 8
0
    def test_basic_property_cells(self):
        strategy = ModuleDictStrategy(space)
        storage = strategy.get_empty_storage()
        d = W_DictMultiObject(space, strategy, storage)

        v1 = strategy.version
        d.setitem("a", 1)
        v2 = strategy.version
        assert v1 is not v2
        assert d.getitem("a") == 1
        assert d.strategy.getdictvalue_no_unwrapping(d, "a") == 1

        d.setitem("a", 2)
        v3 = strategy.version
        assert v2 is not v3
        assert d.getitem("a") == 2
        assert d.strategy.getdictvalue_no_unwrapping(d, "a").w_value == 2

        d.setitem("a", 3)
        v4 = strategy.version
        assert v3 is v4
        assert d.getitem("a") == 3
        assert d.strategy.getdictvalue_no_unwrapping(d, "a").w_value == 3

        d.delitem("a")
        v5 = strategy.version
        assert v5 is not v4
        assert d.getitem("a") is None
        assert d.strategy.getdictvalue_no_unwrapping(d, "a") is None
Exemplo n.º 9
0
    def test_same_key_set_twice(self):
        strategy = ModuleDictStrategy(space)
        storage = strategy.get_empty_storage()
        d = W_ModuleDictObject(space, strategy, storage)

        v1 = strategy.version
        x = object()
        d.setitem("a", x)
        v2 = strategy.version
        assert v1 is not v2
        d.setitem("a", x)
        v3 = strategy.version
        assert v2 is v3
Exemplo n.º 10
0
    def test_same_key_set_twice(self):
        strategy = ModuleDictStrategy(space)
        storage = strategy.get_empty_storage()
        d = W_DictMultiObject(space, strategy, storage)

        v1 = strategy.version
        x = object()
        d.setitem("a", x)
        v2 = strategy.version
        assert v1 is not v2
        d.setitem("a", x)
        v3 = strategy.version
        assert v2 is v3
Exemplo n.º 11
0
    def test_devolve(self):
        strategy = ModuleDictStrategy(space)
        storage = strategy.get_empty_storage()
        d = W_ModuleDictObject(space, strategy, storage)

        key = "a"
        w_key = self.FakeString(key)
        d.setitem(w_key, 1)
        c = d.get_global_cache(key)
        assert c.getvalue(space) == 1
        assert c.valid

        d.setitem(5, 1)
        assert not c.valid
Exemplo n.º 12
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
Exemplo n.º 13
0
 def setup_class(cls):
     if option.runappdirect:
         py.test.skip("__repr__ doesn't work on appdirect")
     strategy = ModuleDictStrategy(cls.space)
     storage = strategy.get_empty_storage()
     cls.w_d = W_DictMultiObject(cls.space, strategy, storage)
Exemplo n.º 14
0
 def setup_class(cls):
     if cls.runappdirect:
         py.test.skip("__repr__ doesn't work on appdirect")
     strategy = ModuleDictStrategy(cls.space)
     storage = strategy.get_empty_storage()
     cls.w_d = W_ModuleDictObject(cls.space, strategy, storage)
Exemplo n.º 15
0
 def setup_method(self, method):
     space = self.space
     strategy = ModuleDictStrategy(space)
     storage = strategy.get_empty_storage()
     self.w_d = W_ModuleDictObject(space, strategy, storage)
Exemplo n.º 16
0
 def setup_method(self, method):
     space = self.space
     strategy = ModuleDictStrategy(space)
     storage = strategy.get_empty_storage()
     self.w_d = W_DictMultiObject(space, strategy, storage)