def testCustomClass(self): def something_serializer(obj, serializer, stream, level): d = { "__class__": "Something", "custom": True, "name": obj.name, "value": obj.value } serializer.ser_builtins_dict(d, stream, level) serpent.register_class(Something, something_serializer) s = Something("hello", 42) d = serpent.dumps(s) x = serpent.loads(d) self.assertEqual( { "__class__": "Something", "custom": True, "name": "hello", "value": 42 }, x) serpent.unregister_class(Something) d = serpent.dumps(s) x = serpent.loads(d) self.assertEqual(("bogus", "state"), x)
def unregister_class_to_dict(cls, clazz): """Removes the to-dict conversion function registered for the given class. Objects of the class will be serialized by the default mechanism again.""" if clazz in cls.__custom_class_to_dict_registry: del cls.__custom_class_to_dict_registry[clazz] with contextlib.suppress(errors.ProtocolError): serpent.unregister_class(clazz)
def unregister_class_to_dict(cls, clazz): """Removes the to-dict conversion function registered for the given class. Objects of the class will be serialized by the default mechanism again.""" if clazz in cls.__custom_class_to_dict_registry: del cls.__custom_class_to_dict_registry[clazz] try: get_serializer_by_id(SerpentSerializer.serializer_id) import serpent serpent.unregister_class(clazz) except errors.ProtocolError: pass
def unregister_class_to_dict(cls, clazz): """Removes the to-dict conversion function registered for the given class. Objects of the class will be serialized by the default mechanism again.""" if clazz in cls.__custom_class_to_dict_registry: del cls.__custom_class_to_dict_registry[clazz] try: get_serializer_by_id(SerpentSerializer.serializer_id) import serpent serpent.unregister_class(clazz) except pyro4.errors.ProtocolError: pass
def testUUID(self): uid = uuid.uuid4() string_uid = str(uid) ser = serpent.dumps(uid) x = serpent.loads(ser) self.assertEqual(string_uid, x) def custom_uuid_translate(obj, serp, stream, level): serp._serialize("custom_uuid!", stream, level) serpent.register_class(uuid.UUID, custom_uuid_translate) try: ser = serpent.dumps(uid) x = serpent.loads(ser) self.assertEqual("custom_uuid!", x) finally: serpent.unregister_class(uuid.UUID)
def testIntercept(self): ex = ZeroDivisionError("wrong") ser = serpent.dumps(ex) data = serpent.loads(ser) # default behavior is to serialize the exception to a dict self.assertEqual({'__exception__': True, 'args': ('wrong',), '__class__': 'ZeroDivisionError', 'attributes': {}}, data) def custom_exception_translate(obj, serializer, stream, indent): serializer._serialize("custom_exception!", stream, indent) try: serpent.register_class(Exception, custom_exception_translate) ser = serpent.dumps(ex) data = serpent.loads(ser) self.assertEqual("custom_exception!", data) finally: serpent.unregister_class(Exception)
def testCustomClass(self): def something_serializer(obj, serializer, stream, level): d = { "__class__": "Something", "custom": True, "name": obj.name, "value": obj.value } serializer.ser_builtins_dict(d, stream, level) serpent.register_class(Something, something_serializer) s = Something("hello", 42) d = serpent.dumps(s) x = serpent.loads(d) self.assertEqual({"__class__": "Something", "custom": True, "name": "hello", "value": 42}, x) serpent.unregister_class(Something) d = serpent.dumps(s) x = serpent.loads(d) self.assertEqual(("bogus", "state"), x)
def testIntercept(self): ex = ZeroDivisionError("wrong") ser = serpent.dumps(ex) data = serpent.loads(ser) # default behavior is to serialize the exception to a dict self.assertEqual( { '__exception__': True, 'args': ('wrong', ), '__class__': 'ZeroDivisionError', 'attributes': {} }, data) def custom_exception_translate(obj, serializer, stream, indent): serializer._serialize("custom_exception!", stream, indent) try: serpent.register_class(Exception, custom_exception_translate) ser = serpent.dumps(ex) data = serpent.loads(ser) self.assertEqual("custom_exception!", data) finally: serpent.unregister_class(Exception)