Beispiel #1
0
 def decorate(func):
     template = """
         def {name}({arglist}):
             {call_jit_merge_point}({arglist})
             return {original}({arglist})
     """
     templateargs = {'call_jit_merge_point': call_jit_merge_point.__name__}
     globaldict = {call_jit_merge_point.__name__: call_jit_merge_point}
     result = rpython_wrapper(func, template, templateargs, **globaldict)
     result._inline_jit_merge_point_ = call_jit_merge_point
     return result
Beispiel #2
0
 def decorate(func):
     template = """
         def {name}({arglist}):
             {call_jit_merge_point}({arglist})
             return {original}({arglist})
     """
     templateargs = {'call_jit_merge_point': call_jit_merge_point.__name__}
     globaldict = {call_jit_merge_point.__name__: call_jit_merge_point}
     result = rpython_wrapper(func, template, templateargs, **globaldict)
     result._inline_jit_merge_point_ = call_jit_merge_point
     return result
Beispiel #3
0
def test_rpython_wrapper():
    calls = []

    def bar(a, b):
        calls.append(('bar', a, b))
        return a + b

    template = """
        def {name}({arglist}):
            calls.append(('decorated', {arglist}))
            return {original}({arglist})
    """
    bar = rpython_wrapper(bar, template, calls=calls)
    assert bar(40, 2) == 42
    assert calls == [
        ('decorated', 40, 2),
        ('bar', 40, 2),
    ]
Beispiel #4
0
def test_rpython_wrapper():
    calls = []

    def bar(a, b):
        calls.append(('bar', a, b))
        return a+b

    template = """
        def {name}({arglist}):
            calls.append(('decorated', {arglist}))
            return {original}({arglist})
    """
    bar = rpython_wrapper(bar, template, calls=calls)
    assert bar(40, 2) == 42
    assert calls == [
        ('decorated', 40, 2),
        ('bar', 40, 2),
        ]
Beispiel #5
0
    def decorator(f):
        def get_annotation(t):
            from rpython.annotator.signature import annotation
            from rpython.annotator.model import SomeObject
            if isinstance(t, SomeObject):
                return t
            return annotation(t)

        def get_type_descr_of_argument(arg):
            # we don't want to check *all* the items in list/dict: we assume
            # they are already homogeneous, so we only check the first
            # item. The case of empty list/dict is handled inside typecheck()
            if isinstance(arg, list):
                return [get_type_descr_of_argument(arg[0])]
            elif isinstance(arg, dict):
                key, value = next(arg.iteritems())
                return {
                    get_type_descr_of_argument(key):
                    get_type_descr_of_argument(value)
                }
            else:
                return type(arg)

        def typecheck(*args):
            from rpython.annotator.model import SomeList, SomeDict, SomeChar,\
                 SomeInteger
            for i, (expected_type, arg) in enumerate(zip(types, args)):
                if expected_type is None:
                    continue
                s_expected = get_annotation(expected_type)
                # special case: if we expect a list or dict and the argument
                # is an empty list/dict, the typecheck always pass
                if isinstance(s_expected, SomeList) and arg == []:
                    continue
                if isinstance(s_expected, SomeDict) and arg == {}:
                    continue
                if isinstance(s_expected, SomeChar) and (isinstance(
                        arg, str) and len(arg) == 1):  # a char
                    continue
                if (isinstance(s_expected, SomeInteger)
                        and isinstance(arg, s_expected.knowntype)):
                    continue
                #
                s_argtype = get_annotation(get_type_descr_of_argument(arg))
                if not s_expected.contains(s_argtype):
                    msg = "%s argument %r must be of type %s" % (
                        f.func_name, srcargs[i], expected_type)
                    raise TypeError(msg)

        #
        template = """
            def {name}({arglist}):
                if not we_are_translated():
                    typecheck({arglist})    # rpython.rlib.objectmodel
                return {original}({arglist})
        """
        result = rpython_wrapper(f,
                                 template,
                                 typecheck=typecheck,
                                 we_are_translated=we_are_translated)
        #
        srcargs, srcvarargs, srckeywords, defaults = inspect.getargspec(f)
        if kwds:
            types = tuple([kwds.get(arg) for arg in srcargs])
        else:
            types = types_
        assert len(srcargs) == len(types), (
            'not enough types provided: expected %d, got %d' %
            (len(types), len(srcargs)))
        result._annenforceargs_ = types
        return result
Beispiel #6
0
 def decorator(f):
     def get_annotation(t):
         from rpython.annotator.signature import annotation
         from rpython.annotator.model import SomeObject, SomeString, SomeUnicodeString
         if isinstance(t, SomeObject):
             return t
         s_result = annotation(t)
         if (isinstance(s_result, SomeString) or
             isinstance(s_result, SomeUnicodeString)):
             return s_result.__class__(can_be_None=True)
         return s_result
     def get_type_descr_of_argument(arg):
         # we don't want to check *all* the items in list/dict: we assume
         # they are already homogeneous, so we only check the first
         # item. The case of empty list/dict is handled inside typecheck()
         if isinstance(arg, list):
             item = arg[0]
             return [get_type_descr_of_argument(item)]
         elif isinstance(arg, dict):
             key, value = next(arg.iteritems())
             return {get_type_descr_of_argument(key): get_type_descr_of_argument(value)}
         else:
             return type(arg)
     def typecheck(*args):
         from rpython.annotator.model import SomeList, SomeDict, SomeChar,\
              SomeInteger
         for i, (expected_type, arg) in enumerate(zip(types, args)):
             if expected_type is None:
                 continue
             s_expected = get_annotation(expected_type)
             # special case: if we expect a list or dict and the argument
             # is an empty list/dict, the typecheck always pass
             if isinstance(s_expected, SomeList) and arg == []:
                 continue
             if isinstance(s_expected, SomeDict) and arg == {}:
                 continue
             if isinstance(s_expected, SomeChar) and (
                     isinstance(arg, str) and len(arg) == 1):   # a char
                 continue
             if (isinstance(s_expected, SomeInteger) and
                 isinstance(arg, s_expected.knowntype)):
                 continue
             #
             s_argtype = get_annotation(get_type_descr_of_argument(arg))
             if not s_expected.contains(s_argtype):
                 msg = "%s argument %r must be of type %s" % (
                     f.func_name, srcargs[i], expected_type)
                 raise TypeError(msg)
     #
     template = """
         def {name}({arglist}):
             if not we_are_translated():
                 typecheck({arglist})    # rpython.rlib.objectmodel
             return {original}({arglist})
     """
     result = rpython_wrapper(f, template,
                              typecheck=typecheck,
                              we_are_translated=we_are_translated)
     #
     srcargs, srcvarargs, srckeywords, defaults = inspect.getargspec(f)
     if kwds:
         types = tuple([kwds.get(arg) for arg in srcargs])
     else:
         types = types_
     assert len(srcargs) == len(types), (
         'not enough types provided: expected %d, got %d' %
         (len(types), len(srcargs)))
     result._annenforceargs_ = types
     return result