def test_serialization(self): values = [5, 'foo', u'bar', (), (1,), (1,2), [], [1], [1,2], set(), set('abc'), set((1,2,3)), set(((3,), (), '')), {}, {1:2}, {'a':1}, {'1': 1}, {4.3: ''}, 4.3, {'4.3':5}, {u'234':54}, {1:u'342'}, {1:'abc'}, {1:2,3:4,5:6}, {'foo': set((1,2.3,'3', (), (1,2)))}, frozenset('123'),None, {None:None}, {True:False},u'__str__foo', {u'__tuple__':u'foo'}, {u'__tuple__':'foo'}, {'__tuple__':u'foo'}, {'__tuple__':u'foo'}, ['__tuple__'],'__True__',u'__None__', ] for value in values + [values]: dataout = pyloads(pydumps(value)) self.assertEquals(value, dataout) self.assertEqual(type(value), type(dataout))
def unpack(cls, data): if len(data) < 4: # profile strings less than 4 bytes long should be treated as # strings version = 0 else: (version,) = unpack("!I", data[:4]) if version not in (1, 2, 3): # assume it was really old and either v0 or had no version # (used for converting from NetData to SerializableNetData) log.info("unpacking version %d of %s", version, cls.__name__) data = pack("I", 1) + data version = 1 if version == 1: log.info("unpacking version %d of %s", version, cls.__name__) data = data[4:] try: data = data.decode("z") except Exception: pass if version == 2: log.info("unpacking version %d of %s", version, cls.__name__) data = data[4:] data = data.decode("z") if version != 3: d = NetData.unpack(data) if isinstance(d, (SerializedDict, SerializedSet)): return unserialize(d) elif isinstance(d, cls.basetype): return d else: return cls.upgrade(d) if version == 3: log.info("unpacking version %d of %s", version, cls.__name__) from util.json import pyloads return pyloads(data[4:].decode("z"))
def unpack(cls, data): if len(data) < 4: # profile strings less than 4 bytes long should be treated as # strings version = 0 else: (version, ) = unpack("!I", data[:4]) if version not in (1, 2, 3): #assume it was really old and either v0 or had no version #(used for converting from NetData to SerializableNetData) log.info("unpacking version %d of %s", version, cls.__name__) data = pack("I", 1) + data version = 1 if version == 1: log.info("unpacking version %d of %s", version, cls.__name__) data = data[4:] try: data = data.decode('z') except Exception: pass if version == 2: log.info("unpacking version %d of %s", version, cls.__name__) data = data[4:] data = data.decode('z') if version != 3: d = NetData.unpack(data) if isinstance(d, (SerializedDict, SerializedSet)): return unserialize(d) elif isinstance(d, cls.basetype): return d else: return cls.upgrade(d) if version == 3: log.info("unpacking version %d of %s", version, cls.__name__) from util.json import pyloads return pyloads(data[4:].decode('z'))
def deserialize_default(self, key, data, crypt_key=None): return pyloads(self.decrypt(data, crypt_key))
def test_serialization(self): values = [ 5, 'foo', u'bar', (), (1, ), (1, 2), [], [1], [1, 2], set(), set('abc'), set((1, 2, 3)), set(((3, ), (), '')), {}, { 1: 2 }, { 'a': 1 }, { '1': 1 }, { 4.3: '' }, 4.3, { '4.3': 5 }, { u'234': 54 }, { 1: u'342' }, { 1: 'abc' }, { 1: 2, 3: 4, 5: 6 }, { 'foo': set((1, 2.3, '3', (), (1, 2))) }, frozenset('123'), None, { None: None }, { True: False }, u'__str__foo', { u'__tuple__': u'foo' }, { u'__tuple__': 'foo' }, { '__tuple__': u'foo' }, { '__tuple__': u'foo' }, ['__tuple__'], '__True__', u'__None__', ] for value in values + [values]: dataout = pyloads(pydumps(value)) self.assertEquals(value, dataout) self.assertEqual(type(value), type(dataout))
def deserialize_default(self, key, data, crypt_key = None): return pyloads(self.decrypt(data, crypt_key))