コード例 #1
0
ファイル: test_serpent.py プロジェクト: achernet/Serpent
    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
ファイル: serializers.py プロジェクト: tishyk/Pyro5
 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
ファイル: util.py プロジェクト: KenV99/script.module.ipc
 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
ファイル: test_serpent.py プロジェクト: achernet/Serpent
    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
ファイル: test_serpent.py プロジェクト: pombredanne/Serpent
    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
ファイル: test_serpent.py プロジェクト: pombredanne/Serpent
    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
ファイル: test_serpent.py プロジェクト: pombredanne/Serpent
    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
ファイル: test_serpent.py プロジェクト: achernet/Serpent
    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)