def testInstancesSerialization(self):
        # Because dictionaries item order is not guaranteed we cannot
        # compare directly directlly the result
        obj = common_serialization.SerializableDummy()
        name = reflect.canonical_name(common_serialization.SerializableDummy)
        data = self.serialize(obj)
        self.assertTrue(isinstance(data, list))
        self.assertEqual(data[0], name)
        self.assertTrue(isinstance(data[1], list))
        self.assertEqual(data[1][0], "dictionary")
        dict_vals = data[1][1:]
        self.assertEqual(len(dict_vals), 12)
        self.assertTrue(['none', ['None']] in dict_vals)
        self.assertTrue(['set', ['set', 1, 2, 3]] in dict_vals)
        self.assertTrue(['str', 'dummy'] in dict_vals)
        self.assertTrue(['tuple', ['tuple', 1, 2, 3]] in dict_vals)
        self.assertTrue(['int', 42] in dict_vals)
        self.assertTrue(['float', 3.1415926] in dict_vals)
        self.assertTrue(['list', ['list', 1, 2, 3]] in dict_vals)
        self.assertTrue(['long', 2**66] in dict_vals)
        self.assertTrue(['bool', ['boolean', 'true']] in dict_vals)
        self.assertTrue(['unicode', ['unicode', 'dummy']] in dict_vals)
        self.assertTrue(['dict', ['dictionary', [1, 2], [3, 4]]] in dict_vals)
        self.assertTrue(['ref', ['None']] in dict_vals)

        obj = ListSerializableDummy([1, 2, 3])
        name = reflect.canonical_name(ListSerializableDummy)
        self.assertEqual(self.serialize(obj),
                         [name, ['list', 1, 2, 3]])
Пример #2
0
    def testMethod(self):
        self.assertEqual("feat.test.test_common_reflect.Dummy.spam",
                         reflect.canonical_name(Dummy.spam))
        self.assertEqual("feat.test.test_common_reflect.Dummy.spam",
                         reflect.canonical_name(Dummy().spam))

        self.assertEqual("__builtin__.split",
                         reflect.canonical_name("test".split))
Пример #3
0
 def testClass(self):
     self.assertEqual("feat.test.test_common_reflect.Dummy",
                      reflect.canonical_name(Dummy))
     self.assertEqual("feat.test.test_common_reflect.Dummy",
                      reflect.canonical_name(Dummy()))
     self.assertEqual("__builtin__.int",
                      reflect.canonical_name(int))
     self.assertEqual("__builtin__.str",
                      reflect.canonical_name("some string"))
Пример #4
0
def _side_effect_wrapper(callable, args, kwargs, name):
    section_state = fiber.get_state()

    if section_state is not None:
        # We are in a woven section

        entry = section_state.get(JOURNAL_ENTRY_TAG, None)

        if entry is not None:
            # We are in a replayable section
            mode = section_state.get(RECMODE_TAG, None)

            if mode == JournalMode.replay:
                return entry.next_side_effect(name, *args, **kwargs)

            # Create a side-effect entry
            effect = entry.new_side_effect(name, *args, **kwargs)
            # Keep it in the replayable section state
            section_state[SIDE_EFFECT_TAG] = effect
            # Break the fiber to allow new replayable sections
            fiber.break_fiber()
            # Keep the side-effect entry to detect we are in one
            fiber.set_stack_var(SIDE_EFFECT_TAG, effect)
            try:
                result = callable(*args, **kwargs)
                result = _check_side_effet_result(result, name)
                effect.set_result(result)
                effect.commit()
                return result
            except Exception, e:
                #FIXME: handle exceptions in side effects properly
                error.handle_exception(None, e,
                                       "Exception raised by side-effect %s",
                                       reflect.canonical_name(callable))
                raise
Пример #5
0
        def check_records(_, records):
            # Filter out the fiber related fields
            records = [r[:2] + r[4:] for r in records]
            # instance_id should be the same
            iid = records[0][0]

            spam_id = "feat.test.test_common_journal.BasicRecordingDummy.spam"
            bacon_id = "bacon"

            break_call = ((reflect.canonical_name(common.break_chain),
                           None, None), None)

            expected = [[iid, spam_id, ("beans", ), None,
                         [], "spam and beans"],

                        [iid, spam_id, ("beans", ), {"extra": "spam"},
                         [], "spam and beans with spam"],

                        [iid, bacon_id, ("beans", ), None,
                         [], (TriggerType.succeed,
                                "spam and beans",
                                [break_call])],
                        [iid, bacon_id, ("beans", ), {"extra": "spam"},
                         [], (TriggerType.succeed,
                                "spam and beans with spam",
                                [break_call])]]

            self.assertEqual(expected, records)
Пример #6
0
Файл: base.py Проект: f3at/feat
 def _lookup_restorator(self, type_name):
     # Lookup the registry for a IRestorator
     restorator = self._registry.lookup(type_name)
     if restorator is None:
         raise TypeError("Type %s not supported by unserializer %s"
                         % (type_name, reflect.canonical_name(self)))
     return restorator
Пример #7
0
    def tearDown(self):
        # First get the current exception before anything else
        exc_type, _, _ = sys.exc_info()

        yield self.driver.freeze_all()

        if self.save_stats:
            f = file(self.save_stats, "a")
            print >> f, ""
            print >> f, "%s.%s:" % (reflect.canonical_name(self),
                                    self._testMethodName, )
            t = text_helper.Table(fields=('name', 'value'),
                                  lengths=(40, 40))
            print >> f, t.render(self.driver.get_stats().iteritems())
            f.close()

        try:
            if exc_type is None or exc_type is StopIteration:
                yield self._check_replayability()
        finally:
            OverrideConfigMixin.tearDown(self)
            # remove leaking memory during the tests
            yield self.driver.destroy()
            for k, v in self.__dict__.items():
                if str(k)[0] == "_":
                    continue
                delattr(self, k)
            yield common.TestCase.tearDown(self)
Пример #8
0
 def wrapper(*args, **kwargs):
     try:
         return function(*args, **kwargs)
     except BaseException as e:
         handle_exception(None, e, "Exception in function %s",
                          reflect.canonical_name(function))
         raise
Пример #9
0
 def flatten_key(self, key, caps, freezing):
     if not isinstance(key, str):
         raise TypeError("Serializer %s is not capable of serializing "
                         "non-string dictionary keys: %r"
                         % (reflect.canonical_name(self), key))
     # Flatten it as unicode by using the selected encoding
     return self.pack_unicode, key.decode(DEFAULT_ENCODING)
Пример #10
0
 def wrapper(*args, **kwargs):
     try:
         return function(*args, **kwargs)
     except BaseException as e:
         print ("Exception raise calling %s: %s"
                % (reflect.canonical_name(function),
                   get_exception_message(e)))
         raise
Пример #11
0
Файл: base.py Проект: f3at/feat
    def flatten_unknown_value(self, value, caps, freezing):
        # Flatten enums
        if isinstance(value, enum.Enum):
            return self.flatten_enum_value(value, caps, freezing)

        # Flatten types and interfaces
        if isinstance(value, (type, InterfaceClass)):
            return self.flatten_type_value(value, caps, freezing)

        if self._externalizer is not None:
            extid = self._externalizer.identify(value)
            if extid is not None:
                return self.flatten_external(extid, caps, freezing)

        # Checks if value support the current required protocol
        # Could be ISnapshotable or ISerializable
        if freezing:

            try:
                snapshotable = ISnapshotable(value)
            except TypeError:
                raise TypeError("Freezing of type %s values "
                                "not supported by %s. Value = %r."
                                % (type(value).__name__,
                                   reflect.canonical_name(self), value)), \
                      None, sys.exc_info()[2]

            return self.flatten_instance(snapshotable, caps, freezing)

        else:

            try:
                serializable = ISerializable(value)
            except TypeError:
                raise TypeError("Serialization of type %s values "
                                "not supported by %s. Value = %r."
                                % (reflect.canonical_name(value),
                                   reflect.canonical_name(self), value)), \
                      None, sys.exc_info()[2]

            return self.flatten_instance(serializable, caps, freezing)
Пример #12
0
    def flatten_unknown_key(self, value, caps, freezing):
        # Flatten enums
        if isinstance(value, enum.Enum):
            return self.flatten_enum_key(value, caps, freezing)

        # Flatten types and interfaces
        if isinstance(value, (type, InterfaceClass)):
            return self.flatten_type_key(value, caps, freezing)

        # Instances are not supported in keys
        raise TypeError(
            "Type %s keys not supported by serializer %s" % (type(value).__name__, reflect.canonical_name(self))
        )
    def testNotReferenceable(self):
        Klass = common_serialization.NotReferenceableDummy
        Inst = pytree.Instance
        name = reflect.canonical_name(Klass)

        obj = Klass()
        data = self.serializer.convert([obj, obj])

        self.assertEqual(data, [Inst(name, {"value": 42}),
                                Inst(name, {"value": 42})])

        data = self.serializer.freeze([obj, obj])

        self.assertEqual(data, [{"value": 42}, {"value": 42}])
    def testNotReferenceable(self):
        Klass = common_serialization.NotReferenceableDummy
        name = reflect.canonical_name(Klass)

        obj = Klass()
        data = self.serializer.convert([obj, obj])

        self.assertEqual(data, ["list",
                                [name, ["dictionary", ["value", 42]]],
                                [name, ["dictionary", ["value", 42]]]])

        data = self.serializer.freeze([obj, obj])

        self.assertEqual(data, ["list",
                                ["dictionary", ["value", 42]],
                                ["dictionary", ["value", 42]]])
Пример #15
0
    def defer_to_thread(self, func, *args, **kw):
        if self.joined:
            return defer.fail(ThreadPoolError("Pool already stopped"))

        job_explanation = kw.pop('job_explanation', None) or \
                          reflect.canonical_name(func)
        job_id = self._next_job_id(job_explanation)
        cb = JobCallback(self, job_id)
        if self._stats:
            self._stats.new_item(job_id, job_explanation)
        ctx = context.theContextTracker.currentContext().contexts[-1]

        self.q.put((ctx, func, args, kw, cb, job_id))

        if self.started:
            self._start_some_workers()

        return cb.deferred
Пример #16
0
    def defer_to_thread(self, func, *args, **kw):
        if self.joined:
            return defer.fail(ThreadPoolError("Pool already stopped"))

        job_explanation = kw.pop('job_explanation', None) or \
                          reflect.canonical_name(func)
        job_id = self._next_job_id(job_explanation)
        cb = JobCallback(self, job_id)
        if self._stats:
            self._stats.new_item(job_id, job_explanation)
        ctx = context.theContextTracker.currentContext().contexts[-1]

        self.q.put((ctx, func, args, kw, cb, job_id))

        if self.started:
            self._start_some_workers()

        return cb.deferred
Пример #17
0
Файл: base.py Проект: f3at/feat
    def _unpack_data(self, data, refid, refdata):
        # Just return pass-through types,
        # support sub-classed base types and metaclasses
        if set(type(data).__mro__) & self.pass_through_types:
            return data

        analysis = self.analyse_data(data)

        if analysis is not None:

            constructor, unpacker = analysis

            if constructor is None:
                # Immutable types
                return unpacker(self, data)

            if callable(constructor):
                # Unpack the mutable containers that provides constructor
                container = constructor()
                if container is not None:
                    if refid is not None:
                        self._references[refid] = (id(refdata), container)
                    return self.delayed_unpacking(container, unpacker,
                                                  self, container, data)

            else:
                # Instance type name
                prepared = self.prepare_instance(constructor)
                if prepared is None:
                    # Immutable instance
                    return unpacker(self, data, None, None, None)

                restorator, instance = prepared

                if refid is not None:
                    self._references[refid] = (id(refdata), instance)
                return self.delayed_unpacking(instance, unpacker, self, data,
                                              refid, restorator, instance)

        raise TypeError("Type %s not supported by unserializer %s"
                        % (type(data).__name__,
                           reflect.canonical_name(self)))
Пример #18
0
    def run(self, result):
        backupdir = os.path.abspath(os.path.curdir)
        try:
            canonical_name = '.'.join([reflect.canonical_name(self),
                                       self._testMethodName])
            testcase_dir = os.path.join(os.path.curdir, canonical_name)
            os.mkdir(testcase_dir)
            os.chdir(testcase_dir)

            logfile = os.path.join(os.path.curdir, 'test.log')
            log.FluLogKeeper.init(logfile)
            log.FluLogKeeper.redirect_to(None, logfile)
            log.FluLogKeeper.set_debug('5')

            self.browser = TestDriver(self, suffix='screenshot',
                                      work_in_thread=self.threaded_selenium)
            unittest.TestCase.run(self, result)

            b = self.browser
            # when the test is finished reactor might not be running,
            # we need to force switching browser to synchronous mode
            b._work_in_thread = False
            for handle in b.window_handles:
                b.switch_to_window(handle)
                self.info(
                    "Grabbing screenshot before closing the window "
                    "title: %s", b.title)
                b.do_screenshot()
            b.quit()
            del(self.browser)
        except Exception:
            # if b.do_screenshot() raised a WebDriverException, we never quit
            # the browser, so do it here
            if self.browser:
                self.browser.quit()
            result.addError(self, failure.Failure())
        finally:
            os.chdir(backupdir)

        return result
    def convertion_table(self, capabilities, freezing):
        ### Basic immutable types ###

        yield str, [""], str, [""], False
        yield str, ["dummy"], str, ["dummy"], False
        yield unicode, [u""], unicode, [u""], False
        yield unicode, [u"dummy"], unicode, [u"dummy"], False
        yield unicode, [u"áéí"], unicode, [u"áéí"], False
        yield int, [0], int, [0], False
        yield int, [42], int, [42], False
        yield int, [-42], int, [-42], False
        yield long, [0L], long, [0L], False
        yield long, [2**66], long, [2**66], False
        yield long, [-2**66], long, [-2**66], False
        yield float, [0.0], float, [0.0], False
        yield float, [3.1415926], float, [3.1415926], False
        yield float, [1e24], float, [1e24], False
        yield float, [1e-24], float, [1e-24], False
        yield bool, [True], bool, [True], False
        yield bool, [False], bool, [False], False
        yield type(None), [None], type(None), [None], False

        ### Types ###
        from datetime import datetime
        yield type, [int], type, [int], False
        yield type, [datetime], type, [datetime], False
        yield (type, [common_serialization.SerializableDummy],
               type, [common_serialization.SerializableDummy], False)
        yield (InterfaceClass, [DummyInterface],
               InterfaceClass, [DummyInterface], False)

        ### Enums ###

        DummyEnum = common_serialization.DummyEnum

        yield DummyEnum, [DummyEnum.a], DummyEnum, [DummyEnum.a], False
        yield DummyEnum, [DummyEnum.c], DummyEnum, [DummyEnum.c], False

        ### External References ###

        if freezing:
            identifier = (self.ext_val.type_name, id(self.ext_val))
            yield (type(self.ext_val), [self.ext_val],
                   tuple, [identifier], False)
            yield (type(self.ext_snap_val), [self.ext_snap_val],
                   type(id(self.ext_snap_val)), [id(self.ext_snap_val)], False)
        else:
            identifier = (self.ext_val.type_name, id(self.ext_val))
            yield (common_serialization.SerializableDummy, [self.ext_val],
                   pytree.External, [pytree.External(identifier)], False)

        ### Freezing-Only Types ###

        if freezing:
            mod_name = "feat.test.test_common_serialization_pytree"
            fun_name = mod_name + ".dummy_function"
            meth_name = mod_name + ".DummyClass.dummy_method"

            yield types.FunctionType, [dummy_function], str, [fun_name], True

            yield (types.FunctionType, [DummyClass.dummy_method],
                   str, [meth_name], True)

            o = DummyClass()
            yield types.FunctionType, [o.dummy_method], str, [meth_name], True

        #### Basic mutable types plus tuples ###

        # Exception for empty tuple singleton
        yield tuple, [()], tuple, [()], False
        yield tuple, [(1, 2, 3)], tuple, [(1, 2, 3)], True
        yield list, [[]], list, [[]], True
        yield list, [[1, 2, 3]], list, [[1, 2, 3]], True
        yield set, [set([])], set, [set([])], True
        yield set, [set([1, 3])], set, [set([1, 3])], True
        yield dict, [{}], dict, [{}], True
        yield dict, [{1: 2, 3: 4}], dict, [{1: 2, 3: 4}], True

        # Container with different types
        yield (tuple, [(0.1, 2**45, "a", u"z", False, None,
                        (1, ), [2], set([3]), {4: 5})],
               tuple, [(0.1, 2**45, "a", u"z", False, None,
                        (1, ), [2], set([3]), {4: 5})], True)
        yield (list, [[0.1, 2**45, "a", u"z", False, None,
                       (1, ), [2], set([3]), {4: 5}]],
               list, [[0.1, 2**45, "a", u"z", False, None,
                       (1, ), [2], set([3]), {4: 5}]], True)
        yield (set, [set([0.1, 2**45, "a", u"z", False, None, (1)])],
               set, [set([0.1, 2**45, "a", u"z", False, None, (1)])], True)
        yield (dict, [{0.2: 0.1, 2**42: 2**45, "x": "a", u"y": u"z",
                       True: False, None: None, (-1, ): (1, ),
                       8: [2], 9: set([3]), 10: {4: 5}}],
               dict, [{0.2: 0.1, 2**42: 2**45, "x": "a", u"y": u"z",
                       True: False, None: None, (-1, ): (1, ),
                       8: [2], 9: set([3]), 10: {4: 5}}], True)

        ### References and Dereferences ###

        Ref = pytree.Reference
        Deref = pytree.Dereference

        # Simple reference in list
        a = []
        b = [a, a]
        yield list, [b], list, [[Ref(1, []), Deref(1)]], True

        # Simple reference in tuple
        a = ()
        b = (a, a)
        yield tuple, [b], tuple, [(Ref(1, ()), Deref(1))], True

        # Simple dereference in dict value.
        a = ()
        b = [a, {1: a}]
        yield list, [b], list, [[Ref(1, ()), {1: Deref(1)}]], True

        # Simple reference in dict value.
        a = ()
        b = [{1: a}, a]
        yield list, [b], list, [[{1: Ref(1, ())}, Deref(1)]], True

        # Simple dereference in dict keys.
        a = ()
        b = [a, {a: 1}]
        yield list, [b], list, [[Ref(1, ()), {Deref(1): 1}]], True

        # Simple reference in dict keys.
        a = ()
        b = [{a: 1}, a]
        yield list, [b], list, [[{Ref(1, ()): 1}, Deref(1)]], True

        # Multiple reference in dictionary values, because dictionary order
        # is not predictable all possibilities have to be tested
        a = {}
        b = {1: a, 2: a, 3: a}
        yield (dict, [b], dict,
               [{1: Ref(1, {}), 2: Deref(1), 3: Deref(1)},
                {1: Deref(1), 2: Ref(1, {}), 3: Deref(1)},
                {1: Deref(1), 2: Deref(1), 3: Ref(1, {})}],
               True)

        # Multiple reference in dictionary keys, because dictionary order
        # is not predictable all possibilities have to be tested
        a = (1, )
        b = {(1, a): 1, (2, a): 2, (3, a): 3}
        yield (dict, [b], dict,
               [{(1, Ref(1, (1, ))): 1, (2, Deref(1)): 2, (3, Deref(1)): 3},
                {(1, Deref(1)): 1, (2, Ref(1, (1, ))): 2, (3, Deref(1)): 3},
                {(1, Deref(1)): 1, (2, Deref(1)): 2, (3, Ref(1, (1, ))): 3}],
               True)

        # Simple dereference in set.
        a = ()
        b = [a, set([a])]
        yield list, [b], list, [[Ref(1, ()), set([Deref(1)])]], True

        # Simple reference in set.
        a = ()
        b = [set([a]), a]
        yield list, [b], list, [[set([Ref(1, ())]), Deref(1)]], True

        # Multiple reference in set, because set values order
        # is not predictable all possibilities have to be tested
        a = (1, )
        b = set([(1, a), (2, a), (3, a)])
        yield (set, [b], set,
               [set([(1, Ref(1, (1, ))), (2, Deref(1)), (3, Deref(1))]),
                set([(1, Deref(1)), (2, Ref(1, (1, ))), (3, Deref(1))]),
                set([(1, Deref(1)), (2, Deref(1)), (3, Ref(1, (1, )))])],
               True)

        # List self-reference
        a = []
        a.append(a)
        yield list, [a], Ref, [Ref(1, [Deref(1)])], True

        # Dict self-reference
        a = {}
        a[1] = a
        yield dict, [a], Ref, [Ref(1, {1: Deref(1)})], True

        # Multiple references
        a = []
        b = [a]
        c = [a, b]
        d = [a, b, c]
        yield (list, [d], list, [[Ref(1, []), Ref(2, [Deref(1)]),
                                 [Deref(1), Deref(2)]]], True)

        # Complex structure without dict or set
        a = ()
        b = (a, )
        b2 = set(b)
        c = (a, b)
        c2 = [c]
        d = (a, b, c)
        d2 = [a, b2, c2]
        e = (b, c, d)
        e2 = [b2, c2, e]
        g = (b, b2, c, c2, d, d2, e, e2)

        yield (tuple, [g], tuple, [(Ref(2, (Ref(1, ()), )),
                                    Ref(4, set([Deref(1)])),
                                    Ref(3, (Deref(1), Deref(2))),
                                    Ref(5, [Deref(3)]),
                                    Ref(6, (Deref(1), Deref(2), Deref(3))),
                                    [Deref(1), Deref(4), Deref(5)],
                                    Ref(7, (Deref(2), Deref(3), Deref(6))),
                                    [Deref(4), Deref(5), Deref(7)])], True)

        Klass = common_serialization.SerializableDummy
        name = reflect.canonical_name(Klass)

        if freezing:
            Inst = lambda v: v
            InstType = dict
        else:
            Inst = lambda v: pytree.Instance(name, v)
            InstType = pytree.Instance

        # Default instance
        o = Klass()
        yield (Klass, [o], InstType,
               [Inst({"str": "dummy",
                      "unicode": u"dummy",
                      "int": 42,
                      "long": 2**66,
                      "float": 3.1415926,
                      "bool": True,
                      "none": None,
                      "list": [1, 2, 3],
                      "tuple": (1, 2, 3),
                      "set": set([1, 2, 3]),
                      "dict": {1: 2, 3: 4},
                      "ref": None})], True)

        Klass = DummyClass
        name = reflect.canonical_name(Klass)

        if freezing:
            Inst = lambda v: v
            InstType = dict
        else:
            Inst = lambda v: pytree.Instance(name, v)
            InstType = pytree.Instance

        a = Klass()
        b = Klass()
        c = Klass()

        a.ref = b
        b.ref = a
        c.ref = c

        yield (Klass, [a], Ref,
               [Ref(1, Inst({"ref":
                    Inst({"ref": Deref(1)})}))], True)

        yield (Klass, [b], Ref,
               [Ref(1, Inst({"ref":
                    Inst({"ref": Deref(1)})}))], True)

        yield (Klass, [c], Ref,
               [Ref(1, Inst({"ref": Deref(1)}))], True)

        yield (list, [[a, b]], list,
               [[Ref(1, Inst({"ref":
                    Ref(2, Inst({"ref": Deref(1)}))})), Deref(2)]], True)

        yield (list, [[a, c]], list,
               [[Ref(1, Inst({"ref":
                    Inst({"ref": Deref(1)})})),
                    Ref(2, Inst({"ref": Deref(2)}))]], True)

        yield (list, [[a, [a, [a, [a]]]]], list,
               [[Ref(1, Inst({'ref': Inst({'ref': Deref(1)})})),
                 [Deref(1), [Deref(1), [Deref(1)]]]]], True)

        yield (tuple, [(a, (a, (a, (a, ))))], tuple,
               [(Ref(1, Inst({'ref': Inst({'ref': Deref(1)})})),
                 (Deref(1), (Deref(1), (Deref(1), ))))], True)
Пример #20
0
 def pack_type(self, data):
     return [TYPE_ATOM, reflect.canonical_name(data)]
Пример #21
0
 def __init__(cls, name, bases, dct):
     cls.type_name = reflect.canonical_name(cls)
     cls.application.register_restorator(cls)
     super(MetaContractor, cls).__init__(name, bases, dct)
Пример #22
0
 def pack_enum(self, value):
     return [ENUM_ATOM, reflect.canonical_name(value), int(value)]
    def convertion_table(self, capabilities, freezing):
        ### Basic immutable types ###

        yield str, [""], str, ['[".enc", "UTF8", ""]',
                               '[".bytes", ""]'], False
        yield str, ["dummy"], str, ['[".enc", "UTF8", "dummy"]',
                                    '[".bytes", "ZHVtbXk="]'], False
        yield str, ["\xFF"], str, ['[".bytes", "/w=="]'], False
        yield unicode, [u""], str, ['""'], False
        yield unicode, [u"dummy"], str, ['"dummy"'], False
        yield unicode, [u"áéí"], str, ['"\\u00e1\\u00e9\\u00ed"'], False
        yield [int, long], [0], str, ["0"], False
        yield [int, long], [42], str, ["42"], False
        yield [int, long], [-42], str, ["-42"], False
        yield [int, long], [0L], str, ["0"], False
        yield long, [2**72], str, ["4722366482869645213696"], False
        yield long, [-2**72], str, ["-4722366482869645213696"], False
        yield float, [0.0], str, ["0.0"], False
        yield float, [3.141], str, ["3.141"], False
        yield float, [-3.141], str, ["-3.141"], False
        yield float, [1e20], str, ["1e+20"], False
        yield float, [1e-22], str, ["1e-22"], False
        yield bool, [True], str, ["true"], False
        yield bool, [False], str, ["false"], False
        yield type(None), [None], str, ["null"], False

        ### Types ###
        from datetime import datetime
        yield type, [int], str, ['[".type", "__builtin__.int"]'], False
        yield (type, [datetime],
               str, ['[".type", "datetime.datetime"]'], False)
        yield (type, [common_serialization.SerializableDummy],
               str, ['[".type", "feat.test.common_serialization.'
                     'SerializableDummy"]'], False)
        yield (InterfaceClass, [DummyInterface],
               str, ['[".type", "feat.test.test_common_serialization_json.'
                     'DummyInterface"]'], False)

        ### Enums ###

        DummyEnum = common_serialization.DummyEnum

        yield (DummyEnum, [DummyEnum.a],
               str, ['[".enum", "feat.test.common_serialization.'
                     'DummyEnum.a"]'], False)
        yield (DummyEnum, [DummyEnum.c],
               str, ['[".enum", "feat.test.common_serialization.'
                     'DummyEnum.c"]'], False)

        ### External References ###

        if freezing:
            name = '[".enc", "UTF8", "%s"]' % self.ext_val.type_name
            identifier = '[".tuple", %s, %d]' % (name, id(self.ext_val))
            yield (type(self.ext_val), [self.ext_val],
                   str, [identifier], False)
            yield (type(self.ext_snap_val), [self.ext_snap_val],
                   str, [str(id(self.ext_snap_val))], False)
        else:
            name = '[".enc", "UTF8", "%s"]' % self.ext_val.type_name
            identifier = '[".tuple", %s, %d]' % (name, id(self.ext_val))
            yield (common_serialization.SerializableDummy, [self.ext_val],
                   str, ['[".ext", %s]' % identifier], False)

        ### Freezing-Only Types ###

        if freezing:
            mod_name = "feat.test.test_common_serialization_json"
            fun_name = '"%s.dummy_function"' % mod_name
            meth_name = '"%s.DummyClass.dummy_method"' % mod_name

            yield types.FunctionType, [dummy_function], str, [fun_name], True

            yield (types.FunctionType, [DummyClass.dummy_method],
                   str, [meth_name], True)

            o = DummyClass()
            yield types.FunctionType, [o.dummy_method], str, [meth_name], True

        #### Basic mutable types plus tuples ###

        # Exception for empty tuple singleton
        yield tuple, [()], str, ['[".tuple"]'], False
        yield tuple, [(1, 2, 3)], str, ['[".tuple", 1, 2, 3]'], True
        yield list, [[]], str, ['[]'], True
        yield list, [[1, 2, 3]], str, ['[1, 2, 3]'], True
        yield set, [set([])], str, ['[".set"]'], True
        yield set, [set([1, 3])], str, ['[".set", 1, 3]'], True
        yield dict, [{}], str, ['{}'], True
        yield dict, [{"1": 2, "3": 4}], str, ['{"1": 2, "3": 4}'], True

        # Container with different types
        yield (tuple, [(0.11, "a", u"z", False, None,
                        (1, ), [2], set([3]), {"4": 5})],
               str, ['[".tuple", 0.11, [".enc", "UTF8", "a"], "z", false, '
                     'null, [".tuple", 1], [2], [".set", 3], {"4": 5}]'], True)
        yield (list, [[0.11, "a", u"z", False, None,
                       (1, ), [2], set([3]), {"4": 5}]],
               str, ['[0.11, [".enc", "UTF8", "a"], "z", false, null, '
                     '[".tuple", 1], [2], [".set", 3], {"4": 5}]'], True)

        ### References and Dereferences ###

        # Simple reference in list
        a = []
        b = [a, a]
        yield list, [b], str, ['[[".ref", 1, []], [".deref", 1]]'], True

        # Simple reference in tuple
        a = ()
        b = (a, a)
        yield tuple, [b], str, ['[".tuple", [".ref", 1, [".tuple"]], '
                                '[".deref", 1]]'], True

        # Simple dereference in dict value.
        a = []
        b = [a, {"1": a}]
        yield list, [b], str, ['[[".ref", 1, []], {"1": [".deref", 1]}]'], True

        # Simple reference in dict value.
        a = []
        b = [{"1": a}, a]
        yield list, [b], str, ['[{"1": [".ref", 1, []]}, [".deref", 1]]'], True

        # Multiple reference in dictionary values, because dictionary order
        # is not predictable all possibilities have to be tested
        a = {}
        b = {"1": a, "2": a, "3": a}
        yield (dict, [b], str,
            ['{"1": [".ref", 1, {}], "2": [".deref", 1], "3": [".deref", 1]}',
             '{"1": [".ref", 1, {}], "3": [".deref", 1], "2": [".deref", 1]}',
             '{"2": [".ref", 1, {}], "1": [".deref", 1], "3": [".deref", 1]}',
             '{"2": [".ref", 1, {}], "3": [".deref", 1], "1": [".deref", 1]}',
             '{"3": [".ref", 1, {}], "1": [".deref", 1], "2": [".deref", 1]}',
             '{"3": [".ref", 1, {}], "2": [".deref", 1], "1": [".deref", 1]}'],
               True)

        # Simple dereference in set.
        a = ()
        b = [a, set([a])]
        yield list, [b], str, ['[[".ref", 1, [".tuple"]], '
                               '[".set", [".deref", 1]]]'], True

        # Simple reference in set.
        a = ()
        b = [set([a]), a]
        yield list, [b], str, ['[[".set", [".ref", 1, [".tuple"]]], '
                               '[".deref", 1]]'], True

        # Multiple reference in set, because set values order
        # is not predictable all possibilities have to be tested
        a = ()
        b = set([(1, a), (2, a)])
        yield (set, [b], str,
               ['[".set", [".tuple", 1, [".ref", 1, [".tuple"]]], '
                '[".tuple", 2, [".deref", 1]]]',
                '[".set", [".tuple", 2, [".ref", 1, [".tuple"]]], '
                '[".tuple", 1, [".deref", 1]]]'], True)

        # List self-reference
        a = []
        a.append(a)
        yield list, [a], str, ['[".ref", 1, [[".deref", 1]]]'], True

        # Dict self-reference
        a = {}
        a["1"] = a
        yield dict, [a], str, ['[".ref", 1, {"1": [".deref", 1]}]'], True

        # Multiple references
        a = []
        b = [a]
        c = [a, b]
        d = [a, b, c]
        yield list, [d], str, ['[[".ref", 1, []], '
                               '[".ref", 2, [[".deref", 1]]], '
                               '[[".deref", 1], [".deref", 2]]]'], True

        # Default instance
        o = DummyClass()
        o.value = 42

        if freezing:
            yield (DummyClass, [o], str, ['{"value": 42}'], True)
        else:
            name = reflect.canonical_name(o)
            yield (DummyClass, [o], str,
                   ['{".type": "%s", "value": 42}' % name,
                    '{"value": 42, ".type": "%s"}' % name], True)

        Klass = DummyClass
        name = reflect.canonical_name(Klass)

        a = Klass()
        b = Klass()
        c = Klass()

        a.ref = b
        b.ref = a
        c.ref = c

        if freezing:
            yield (Klass, [a], str,
                   ['[".ref", 1, {"ref": {"ref": [".deref", 1]}}]'], True)
            yield (Klass, [b], str,
                   ['[".ref", 1, {"ref": {"ref": [".deref", 1]}}]'], True)
            yield (Klass, [c], str,
                   ['[".ref", 1, {"ref": [".deref", 1]}]'], True)

        else:
            yield (Klass, [a], str,
                   [('[".ref", 1, {".type": "%s", "ref": {".type": "%s", '
                     '"ref": [".deref", 1]}}]') % (name, name)], True)
            yield (Klass, [b], str,
                   [('[".ref", 1, {".type": "%s", "ref": {".type": "%s", '
                     '"ref": [".deref", 1]}}]') % (name, name)], True)
            yield (Klass, [c], str, [('[".ref", 1, {".type": "%s", "ref": '
                                      '[".deref", 1]}]') % (name, )], True)
Пример #24
0
 def __init__(cls, name, bases, dct):
     cls.type_name = reflect.canonical_name(cls)
     serialization.register(cls)
     super(MetaReplier, cls).__init__(name, bases, dct)
Пример #25
0
 def _extract_fun_id(self, something):
     if something is None:
         return something
     if isinstance(something, (types.FunctionType, types.MethodType)):
         return reflect.canonical_name(something)
     return something
Пример #26
0
 def check_capabilities(self, cap, value, caps, freezing):
     if cap not in caps:
         kind = "Freezer" if freezing else "Serializer"
         raise ValueError("%s %s do not support %s: %r" % (kind, reflect.canonical_name(self), cap.name, value))
Пример #27
0
    def pack_enum(self, data):

        return [ENUM_ATOM, reflect.canonical_name(data) + "." + data.name]
Пример #28
0
 def blocking_call(self, method, args=tuple(), kwargs=dict()):
     return self.wait_for_defer(self.call_async(method, args, kwargs),
                                reason=reflect.canonical_name(method))
Пример #29
0
 def pack_type(self, value):
     return [CLASS_ATOM, reflect.canonical_name(value)]
Пример #30
0
 def pack_frozen_function(self, value):
     return reflect.canonical_name(value)
Пример #31
0
def format_call(callback, *args, **kwargs):
    return "%s(%s)" % (reflect.canonical_name(callback),
                       format_args(*args, **kwargs))
Пример #32
0
 def pack_frozen_function(self, data):
     return reflect.canonical_name(data)