def test_set_strategy(self): from __pypy__ import strategy s = set() assert strategy(s) == "EmptySetStrategy" s = set([2, 3, 4]) assert strategy(s) == "IntegerSetStrategy"
def test_dict_strategy(self): from __pypy__ import strategy d = {} assert strategy(d) == "EmptyDictStrategy" d = {1: None, 5: None} assert strategy(d) == "IntDictStrategy"
def test_check_strategy(self): import __pypy__ import _pypyjson d = _pypyjson.loads('{"a": 1}') assert __pypy__.strategy(d) == "JsonDictStrategy" d = _pypyjson.loads('{}') assert __pypy__.strategy(d) == "EmptyDictStrategy"
def test_update_bug_strategy(self): from __pypy__ import strategy s = set([1, 2, 3]) assert strategy(s) == "IntegerSetStrategy" s.update(set()) assert strategy(s) == "IntegerSetStrategy" # s = set([1, 2, 3]) s |= set() assert strategy(s) == "IntegerSetStrategy" # s = set([1, 2, 3]).difference(set()) assert strategy(s) == "IntegerSetStrategy" # s = set([1, 2, 3]) s.difference_update(set()) assert strategy(s) == "IntegerSetStrategy" # s = set([1, 2, 3]).symmetric_difference(set()) assert strategy(s) == "IntegerSetStrategy" # s = set([1, 2, 3]) s.symmetric_difference_update(set()) assert strategy(s) == "IntegerSetStrategy" # s = set([1, 2, 3]).intersection(set()) assert strategy(s) == "EmptySetStrategy" # s = set([1, 2, 3]) s.intersection_update(set()) assert strategy(s) == "EmptySetStrategy"
def test_huge_map(self): import _pypyjson import __pypy__ s = '{' + ",".join('"%s": %s' % (i, i) for i in range(200)) + '}' res = _pypyjson.loads(s) assert len(res) == 200 assert __pypy__.strategy(res) == "UnicodeDictStrategy"
def test_dict_order_retained_when_switching_strategies(self): import _pypyjson import __pypy__ d = _pypyjson.loads('{"a": 1, "b": "x"}') assert list(d) == [u"a", u"b"] # devolve assert not 1 in d assert __pypy__.strategy(d) == "UnicodeDictStrategy" assert list(d) == [u"a", u"b"]
def test_delitem(self): import __pypy__ import _pypyjson d = _pypyjson.loads('{"a": 1, "b": "x"}') del d[u"a"] assert __pypy__.strategy(d) == "UnicodeDictStrategy" assert len(d) == 1 assert d == {u"b": "x"}
def test_setdefault(self): import __pypy__ import _pypyjson d = _pypyjson.loads('{"a": 1, "b": "x"}') assert d.setdefault(u"a", "blub") == 1 d.setdefault(u"x", 23) assert __pypy__.strategy(d) == "UnicodeDictStrategy" assert len(d) == 3 assert d == {u"a": 1, u"b": "x", u"x": 23}
def test_simple(self): import __pypy__ import _pypyjson d = _pypyjson.loads('{"a": 1, "b": "x"}') assert len(d) == 2 assert d[u"a"] == 1 assert d[u"b"] == u"x" assert u"c" not in d d[u"a"] = 5 assert d[u"a"] == 5 assert __pypy__.strategy(d) == "JsonDictStrategy" # devolve it assert not 1 in d assert __pypy__.strategy(d) == "UnicodeDictStrategy" assert len(d) == 2 assert d[u"a"] == 5 assert d[u"b"] == u"x" assert u"c" not in d
def test_popitem(self): import __pypy__ import _pypyjson d = _pypyjson.loads('{"a": 1, "b": "x"}') k, v = d.popitem() assert __pypy__.strategy(d) == "UnicodeDictStrategy" if k == u"a": assert v == 1 assert len(d) == 1 assert d == {u"b": "x"} else: assert v == u"x" assert len(d) == 1 assert d == {u"a": 1}
def test_instance_strategy(self): import sys from __pypy__ import strategy if sys.maxsize < 2**32: skip('not for 32-bit python') class A(object): pass a = A() a.x = 1 a.y = 2 assert strategy(a).startswith( "<UnboxedPlainAttribute y DICT 0 1 <UnboxedPlainAttribute x DICT 0 0 <DictTerminator w_cls=<W_TypeObject 'A'" )
def _pack(self, obj, *args): if isinstance(obj, list): strategy = __pypy__.strategy(obj) if strategy == 'int': # super-fast conversion from list-of-ints to a raw # buffer, only in the pypy fast_cffi_list_init branch for # now buf = ffi.buffer(ffi.new("long[]", obj)) # this extra copy should not be needed :-( return self.pack_ext_type(INT_LIST, buf[:]) elif strategy == 'float': # same as above buf = ffi.buffer(ffi.new("double[]", obj)) return self.pack_ext_type(FLOAT_LIST, buf[:]) return msgpack.Packer._pack(self, obj, *args)
def _normalize_dict(_dict): # Credit: Lin Cheng # return the original dictionary if not running on PyPy try: from __pypy__ import strategy, newdict except Exception: return _dict # return the original dictionary if already using ModuleDictStrategy if strategy(_dict) == "ModuleDictStrategy": return _dict # create a new module dict new_dict = newdict("module") # copy over entries for key, value in _dict.items(): new_dict[key] = value return new_dict
def test_list_strategy(self): from __pypy__ import strategy l = [1, 2, 3] assert strategy(l) == "IntegerListStrategy" l = ["a", "b", "c"] assert strategy(l) == "BytesListStrategy" l = [u"a", u"b", u"c"] assert strategy(l) == "UnicodeListStrategy" l = [1.1, 2.2, 3.3] assert strategy(l) == "FloatListStrategy" l = range(3) assert strategy(l) == "SimpleRangeListStrategy" l = range(1, 2) assert strategy(l) == "RangeListStrategy" l = [1, "b", 3] assert strategy(l) == "ObjectListStrategy" l = [] assert strategy(l) == "EmptyListStrategy" o = 5 raises(TypeError, strategy, 5)
def test_list_strategy(self): from __pypy__ import strategy l = [1, 2, 3] assert strategy(l) == "IntegerListStrategy" l = ["a", "b", "c"] assert strategy(l) == "BytesListStrategy" l = [u"a", u"b", u"c"] assert strategy(l) == "AsciiListStrategy" l = [1.1, 2.2, 3.3] assert strategy(l) == "FloatListStrategy" l = range(3) assert strategy(l) == "SimpleRangeListStrategy" l = range(1, 2) assert strategy(l) == "RangeListStrategy" l = [1, "b", 3] assert strategy(l) == "ObjectListStrategy" l = [] assert strategy(l) == "EmptyListStrategy" o = 5 raises(TypeError, strategy, 5)
def test_list_strategy(self): from __pypy__ import strategy l = [1, 2, 3] assert strategy(l) == "IntegerListStrategy" l = [b"a", b"b", b"c"] assert strategy(l) == "BytesListStrategy" l = [u"a", u"b", u"c"] assert strategy(l) == "UnicodeListStrategy" l = [1.1, 2.2, 3.3] assert strategy(l) == "FloatListStrategy" l = [1, "b", 3] assert strategy(l) == "ObjectListStrategy" l = [] assert strategy(l) == "EmptyListStrategy" o = 5 raises(TypeError, strategy, 5)
def test_dict_structseqfield_immutable(): import __pypy__ assert __pypy__.strategy(foo.f5).count("immutable") == 5
def test_dict_mapdict(): import __pypy__ t = foo((1, 2, 3, 4), dict(f6=12)) assert __pypy__.strategy(t.__dict__) == 'MapDictStrategy'