def flatten(self, obj, data): pickler = self.context if not pickler.unpicklable: return unicode(obj) cls, args = obj.__reduce__() flatten = pickler.flatten payload = util.b64encode(args[0]) args = [payload] + [flatten(i, reset=False) for i in args[1:]] data['__reduce__'] = (flatten(cls, reset=False), args) return data
def flatten(self, obj, data): pickler = self.context flatten = pickler.flatten buffer = util.b64encode(obj.tostring()) #TODO: should probably also consider including other parameters in future such as byteorder, etc. #TODO: see numpy.info(obj) and obj.__reduce__() for details. shape = flatten(obj.shape) dtype = str(obj.dtype) strides = flatten(obj.strides) args = [shape, dtype, strides, buffer] data['__reduce__'] = (flatten(np.ndarray, reset=False), args) return data
def test_bytes_unicode(self): b1 = b'foo' b2 = b'foo\xff' u1 = 'foo' # unicode strings get encoded/decoded as is encoded = self.pickler.flatten(u1) self.assertTrue(encoded == u1) self.assertTrue(isinstance(encoded, compat.ustr)) decoded = self.unpickler.restore(encoded) self.assertTrue(decoded == u1) self.assertTrue(isinstance(decoded, compat.ustr)) # bytestrings are wrapped in PY3 but in PY2 we try to decode first encoded = self.pickler.flatten(b1) if PY2: self.assertEqual(encoded, u1) self.assertTrue(isinstance(encoded, compat.ustr)) else: self.assertNotEqual(encoded, u1) encoded_ustr = util.b64encode(b'foo') self.assertEqual({tags.B64: encoded_ustr}, encoded) self.assertTrue(isinstance(encoded[tags.B64], compat.ustr)) decoded = self.unpickler.restore(encoded) self.assertTrue(decoded == b1) if PY2: self.assertTrue(isinstance(decoded, compat.ustr)) else: self.assertTrue(isinstance(decoded, bytes)) # bytestrings that we can't decode to UTF-8 will always be wrapped encoded = self.pickler.flatten(b2) self.assertNotEqual(encoded, b2) encoded_ustr = util.b64encode(b'foo\xff') self.assertEqual({tags.B64: encoded_ustr}, encoded) self.assertTrue(isinstance(encoded[tags.B64], compat.ustr)) decoded = self.unpickler.restore(encoded) self.assertEqual(decoded, b2) self.assertTrue(isinstance(decoded, bytes))