def maybe_dill_dumps(o): """Pickle using cPickle or the Dill pickler as a fallback.""" # We need to use the dill pickler for objects of certain custom classes, # including, for example, ones that contain lambdas. try: return pickle.dumps(o, pickle.HIGHEST_PROTOCOL) except Exception: # pylint: disable=broad-except return dill.dumps(o)
def test_generated_class_pickle(self): schema = schema_pb2.Schema( id="some-uuid", fields=[ schema_pb2.Field( name='name', type=schema_pb2.FieldType(atomic_type=schema_pb2.STRING), ) ]) user_type = named_tuple_from_schema(schema) instance = user_type(name="test") self.assertEqual(instance, pickle.loads(pickle.dumps(instance)))
def encode(self, value): return base64.b64encode(pickle.dumps(value, pickle.HIGHEST_PROTOCOL))