示例#1
0
    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)
示例#2
0
 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)
示例#3
0
文件: util.py 项目: dean-shaff/Pyro4
 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
示例#4
0
 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
示例#5
0
    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)
示例#6
0
    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)
示例#7
0
    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)
示例#8
0
    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)
示例#9
0
    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)