def test_register_external_tuple_args(self):
        """
        Verify the annotation of a registered external function which takes a
        tuple argument.
        """

        def function_with_tuple_arg():
            """
            Dummy function which is declared via register_external to take a
            tuple as an argument so that register_external's behavior for
            tuple-taking functions can be verified.
            """

        register_external(function_with_tuple_arg, [(int,)], int)

        def f():
            return function_with_tuple_arg((1,))

        policy = AnnotatorPolicy()
        policy.allow_someobjects = False
        a = RPythonAnnotator(policy=policy)
        s = a.build_types(f, [])

        # Not a very good assertion, but at least it means _something_ happened.
        assert isinstance(s, annmodel.SomeInteger)
    def test_register_external_return_goes_back(self):
        """
        Check whether it works to pass the same list from one external
        fun to another
        [bookkeeper and list joining issues]
        """

        def function_with_list():
            pass

        register_external(function_with_list, [[int]], int)

        def function_returning_list():
            pass

        register_external(function_returning_list, [], [int])

        def f():
            return function_with_list(function_returning_list())

        policy = AnnotatorPolicy()
        policy.allow_someobjects = False
        a = RPythonAnnotator(policy=policy)
        s = a.build_types(f, [])
        assert isinstance(s, annmodel.SomeInteger)
    def test_callback(self):
        """
        Verify annotation when a callback function is in the arguments list.
        """

        def d(y):
            return eval("y()")

        class DTestFuncEntry(ExtFuncEntry):
            _about_ = d
            name = "d"
            signature_args = [annmodel.SomeGenericCallable(args=[], result=annmodel.SomeFloat())]
            signature_result = annmodel.SomeFloat()

        def callback():
            return 2.5

        def f():
            return d(callback)

        policy = AnnotatorPolicy()
        policy.allow_someobjects = False
        a = RPythonAnnotator(policy=policy)
        s = a.build_types(f, [])
        assert isinstance(s, annmodel.SomeFloat)
        assert a.translator._graphof(callback)
    def test_basic(self):
        """
        A ExtFuncEntry provides an annotation for a function, no need to flow
        its graph.
        """

        def b(x):
            "NOT_RPYTHON"
            return eval("x+40")

        class BTestFuncEntry(ExtFuncEntry):
            _about_ = b
            name = "b"
            signature_args = [annmodel.SomeInteger()]
            signature_result = annmodel.SomeInteger()

        def f():
            return b(2)

        policy = AnnotatorPolicy()
        policy.allow_someobjects = False
        a = RPythonAnnotator(policy=policy)
        s = a.build_types(f, [])
        assert isinstance(s, annmodel.SomeInteger)

        res = interpret(f, [])
        assert res == 42
Beispiel #5
0
    def __init__(self, function, annotations, stackless=False, view=False, html=None, is_interactive=False, root = None, run_browser = True, policy = None):
        if not use_browsertest and not _CLI_is_on_path():
            py.test.skip('Javascript CLI (js) not found')

        self.html = html
        self.is_interactive = is_interactive
        t = TranslationContext()
        
        if policy is None:
            from pypy.annotation.policy import AnnotatorPolicy
            policy = AnnotatorPolicy()
            policy.allow_someobjects = False

        ann = t.buildannotator(policy=policy)
        ann.build_types(function, annotations)
        if view or option.view:
            t.view()
        t.buildrtyper(type_system="ootype").specialize()

        if view or option.view:
            t.view()
        #self.js = JS(t, [function, callback_function], stackless)
        self.js = JS(t, function, stackless)
        self.js.write_source()
        if root is None and use_tg:
            from pypy.translator.js.demo.jsdemo.controllers import Root
            self.root = Root
        else:
            self.root = root
        self.run_browser = run_browser
        self.function_calls = []
Beispiel #6
0
def get_interpreter(func, values, view='auto', viewbefore='auto', policy=None,
                    someobjects=False, type_system="lltype", backendopt=False, config=None):
    key = (func,) + tuple([typeOf(x) for x in values])+ (someobjects,
                                                         backendopt)
    try: 
        (t, interp, graph) = _tcache[key]
    except KeyError:
        def annotation(x):
            T = typeOf(x)
            if T == Ptr(PyObject) and someobjects:
                return object
            elif T == Ptr(rstr.STR):
                return str
            else:
                return lltype_to_annotation(T)

        if policy is None and not someobjects:
            from pypy.annotation.policy import AnnotatorPolicy
            policy = AnnotatorPolicy()
            policy.allow_someobjects = False

        t, typer, graph = gengraph(func, [annotation(x) for x in values],
                                   viewbefore, policy, type_system=type_system,
                                   backendopt=backendopt, config=config)
        interp = LLInterpreter(typer)
        _tcache[key] = (t, interp, graph)
        # keep the cache small 
        _lastinterpreted.append(key) 
        if len(_lastinterpreted) >= 4: 
            del _tcache[_lastinterpreted.pop(0)]
    if view == 'auto':
        view = conftest.option.view
    if view:
        t.view()
    return interp, graph
Beispiel #7
0
def test_annotation_b():
    def f():
        return b(1)
    
    policy = AnnotatorPolicy()
    policy.allow_someobjects = False
    a = RPythonAnnotator(policy=policy)
    s = a.build_types(f, [])
    assert isinstance(s, annmodel.SomeInteger)
Beispiel #8
0
def test_register_external_signature():
    def f():
        return dd(3)

    policy = AnnotatorPolicy()
    policy.allow_someobjects = False
    a = RPythonAnnotator(policy=policy)
    s = a.build_types(f, [])
    assert isinstance(s, annmodel.SomeInteger)
Beispiel #9
0
def test_register_external_specialcase():
    def f():
        x = function_withspecialcase
        return x(33) + x("aaa") + x([]) + "\n"

    policy = AnnotatorPolicy()
    policy.allow_someobjects = False
    a = RPythonAnnotator(policy=policy)
    s = a.build_types(f, [])
    assert isinstance(s, annmodel.SomeString)
Beispiel #10
0
def get_interpreter(
    func,
    values,
    view="auto",
    viewbefore="auto",
    policy=None,
    someobjects=False,
    type_system="lltype",
    backendopt=False,
    config=None,
    **extraconfigopts
):
    extra_key = [(key, value) for key, value in extraconfigopts.iteritems()]
    extra_key.sort()
    extra_key = tuple(extra_key)
    key = (func,) + tuple([typeOf(x) for x in values]) + (someobjects, backendopt, extra_key)
    try:
        (t, interp, graph) = _tcache[key]
    except KeyError:

        def annotation(x):
            T = typeOf(x)
            if T == Ptr(PyObject) and someobjects:
                return object
            else:
                return lltype_to_annotation(T)

        if policy is None and not someobjects:
            from pypy.annotation.policy import AnnotatorPolicy

            policy = AnnotatorPolicy()
            policy.allow_someobjects = False

        t, typer, graph = gengraph(
            func,
            [annotation(x) for x in values],
            viewbefore,
            policy,
            type_system=type_system,
            backendopt=backendopt,
            config=config,
            **extraconfigopts
        )
        interp = LLInterpreter(typer)
        _tcache[key] = (t, interp, graph)
        # keep the cache small
        _lastinterpreted.append(key)
        if len(_lastinterpreted) >= 4:
            del _tcache[_lastinterpreted.pop(0)]
    if view == "auto":
        view = getattr(conftest.option, "view", False)
    if view:
        t.view()
    return interp, graph
Beispiel #11
0
def test_callback():
    def callback():
        return 2.5

    def f():
        return d(callback)

    policy = AnnotatorPolicy()
    policy.allow_someobjects = False
    a = RPythonAnnotator(policy=policy)
    s = a.build_types(f, [])
    assert isinstance(s, annmodel.SomeFloat)
    assert a.translator._graphof(callback)
Beispiel #12
0
def get_interpreter(func,
                    values,
                    view='auto',
                    viewbefore='auto',
                    policy=None,
                    someobjects=False,
                    type_system="lltype",
                    backendopt=False,
                    config=None,
                    malloc_check=True,
                    **extraconfigopts):
    extra_key = [(key, value) for key, value in extraconfigopts.iteritems()]
    extra_key.sort()
    extra_key = tuple(extra_key)
    key = ((func, ) + tuple([typeOf(x) for x in values]) +
           (someobjects, backendopt, extra_key))
    try:
        (t, interp, graph) = _tcache[key]
    except KeyError:

        def annotation(x):
            T = typeOf(x)
            if T == Ptr(PyObject) and someobjects:
                return object
            else:
                return lltype_to_annotation(T)

        if policy is None and not someobjects:
            from pypy.annotation.policy import AnnotatorPolicy
            policy = AnnotatorPolicy()
            policy.allow_someobjects = False

        t, typer, graph = gengraph(func, [annotation(x) for x in values],
                                   viewbefore,
                                   policy,
                                   type_system=type_system,
                                   backendopt=backendopt,
                                   config=config,
                                   **extraconfigopts)
        interp = LLInterpreter(typer, malloc_check=malloc_check)
        _tcache[key] = (t, interp, graph)
        # keep the cache small
        _lastinterpreted.append(key)
        if len(_lastinterpreted) >= 4:
            del _tcache[_lastinterpreted.pop(0)]
    if view == 'auto':
        view = conftest.option.view
    if view:
        t.view()
    return interp, graph
Beispiel #13
0
def test_register_external_tuple_args():
    """
    Verify the annotation of a registered external function which takes a tuple
    argument.
    """
    def f():
        return function_with_tuple_arg((1,))

    policy = AnnotatorPolicy()
    policy.allow_someobjects = False
    a = RPythonAnnotator(policy=policy)
    s = a.build_types(f, [])

    # Not a very good assertion, but at least it means _something_ happened.
    assert isinstance(s, annmodel.SomeInteger)
Beispiel #14
0
def test_register_external_tuple_args():
    """
    Verify the annotation of a registered external function which takes a tuple
    argument.
    """
    def f():
        return function_with_tuple_arg((1, ))

    policy = AnnotatorPolicy()
    policy.allow_someobjects = False
    a = RPythonAnnotator(policy=policy)
    s = a.build_types(f, [])

    # Not a very good assertion, but at least it means _something_ happened.
    assert isinstance(s, annmodel.SomeInteger)
Beispiel #15
0
    def test_register_external_signature(self):
        """
        Test the standard interface for external functions.
        """
        def dd():
            pass
        register_external(dd, [int], int)

        def f():
            return dd(3)

        policy = AnnotatorPolicy()
        policy.allow_someobjects = False
        a = RPythonAnnotator(policy=policy)
        s = a.build_types(f, [])
        assert isinstance(s, annmodel.SomeInteger)
Beispiel #16
0
def test_prebuilt_lock():
    import thread
    import pypy.module.thread.rpython.exttable   # for declare()/declaretype()
    lock0 = thread.allocate_lock()
    lock1 = thread.allocate_lock()
    lock1.acquire()
    def fn(i):
        lock = [lock0, lock1][i]
        ok = lock.acquire(False)
        if ok: lock.release()
        return ok
    policy = AnnotatorPolicy()
    policy.allow_someobjects = False
    res = interpret(fn, [0], policy=policy)
    assert res is True
    res = interpret(fn, [1], policy=policy)
    assert res is False
Beispiel #17
0
    def test_register_external_specialcase(self):
        """
        When args=None, the external function accepts any arguments unmodified.
        """
        def function_withspecialcase(arg):
            return repr(arg)
        register_external(function_withspecialcase, args=None, result=str)

        def f():
            x = function_withspecialcase
            return x(33) + x("aaa") + x([]) + "\n"

        policy = AnnotatorPolicy()
        policy.allow_someobjects = False
        a = RPythonAnnotator(policy=policy)
        s = a.build_types(f, [])
        assert isinstance(s, annmodel.SomeString)
Beispiel #18
0
def test_lock_or_None():
    import thread
    import pypy.module.thread.rpython.exttable   # for declare()/declaretype()
    def makelock(i):
        if i > 0:
            return thread.allocate_lock()
        else:
            return None
    def fn(i):
        lock = makelock(i)
        return lock is not None and lock.acquire(False)
    policy = AnnotatorPolicy()
    policy.allow_someobjects = False
    res = interpret(fn, [0], policy=policy)
    assert res is False
    res = interpret(fn, [1], policy=policy)
    assert res is True
Beispiel #19
0
    def test_register_external_signature(self):
        """
        Test the standard interface for external functions.
        """
        def dd():
            pass

        register_external(dd, [int], int)

        def f():
            return dd(3)

        policy = AnnotatorPolicy()
        policy.allow_someobjects = False
        a = RPythonAnnotator(policy=policy)
        s = a.build_types(f, [])
        assert isinstance(s, annmodel.SomeInteger)
Beispiel #20
0
    def test_register_external_specialcase(self):
        """
        When args=None, the external function accepts any arguments unmodified.
        """
        def function_withspecialcase(arg):
            return repr(arg)

        register_external(function_withspecialcase, args=None, result=str)

        def f():
            x = function_withspecialcase
            return x(33) + x("aaa") + x([]) + "\n"

        policy = AnnotatorPolicy()
        policy.allow_someobjects = False
        a = RPythonAnnotator(policy=policy)
        s = a.build_types(f, [])
        assert isinstance(s, annmodel.SomeString)
Beispiel #21
0
def annotate(func, values, inline=None, backendoptimize=True,
             type_system="lltype"):
    # build the normal ll graphs for ll_function
    t = TranslationContext()
    annpolicy = AnnotatorPolicy()
    annpolicy.allow_someobjects = False
    a = t.buildannotator(policy=annpolicy)
    argtypes = getargtypes(a, values)
    a.build_types(func, argtypes, main_entry_point=True)
    rtyper = t.buildrtyper(type_system = type_system)
    rtyper.specialize()
    #if inline:
    #    auto_inlining(t, threshold=inline)
    if backendoptimize:
        from pypy.translator.backendopt.all import backend_optimizations
        backend_optimizations(t, inline_threshold=inline or 0,
                remove_asserts=True, really_remove_asserts=True)

    return rtyper
Beispiel #22
0
def annotate(func, values, inline=None, backendoptimize=True,
             type_system="lltype"):
    # build the normal ll graphs for ll_function
    t = TranslationContext()
    annpolicy = AnnotatorPolicy()
    annpolicy.allow_someobjects = False
    a = t.buildannotator(policy=annpolicy)
    argtypes = getargtypes(a, values)
    a.build_types(func, argtypes)
    rtyper = t.buildrtyper(type_system = type_system)
    rtyper.specialize()
    if inline:
        auto_inlining(t, threshold=inline)
    if backendoptimize:
        from pypy.translator.backendopt.all import backend_optimizations
        backend_optimizations(t, inline_threshold=inline or 0,
                remove_asserts=True, really_remove_asserts=True)

    #if conftest.option.view:
    #    t.view()
    return rtyper
Beispiel #23
0
def rcompile(rgenop, entrypoint, argtypes, random_seed=0, type_system='lltype'):
    from pypy.translator.translator import TranslationContext
    from pypy.annotation.policy import AnnotatorPolicy
    from pypy import conftest
    t = TranslationContext()
    policy = AnnotatorPolicy()
    policy.allow_someobjects = False
    t.buildannotator(policy=policy).build_types(entrypoint, argtypes)
    t.buildrtyper(type_system = type_system).specialize()

    # note that backend optimizations will constant-fold simple operations,
    # which is required by some backends that don't accept calls like
    # genop1("add", constant, constant).
    from pypy.translator.backendopt.all import backend_optimizations
    backend_optimizations(t)

    if conftest.option.view:
        t.view()

    entrygraph = t._graphof(entrypoint)
    return compile_graph(rgenop, entrygraph, random_seed=random_seed)
Beispiel #24
0
    def __init__(self,
                 function,
                 annotations,
                 stackless=False,
                 view=False,
                 html=None,
                 is_interactive=False,
                 root=None,
                 run_browser=True,
                 policy=None):
        if not use_browsertest and not _CLI_is_on_path():
            py.test.skip('Javascript CLI (js) not found')

        self.html = html
        self.is_interactive = is_interactive
        t = TranslationContext()

        if policy is None:
            from pypy.annotation.policy import AnnotatorPolicy
            policy = AnnotatorPolicy()
            policy.allow_someobjects = False

        ann = t.buildannotator(policy=policy)
        ann.build_types(function, annotations)
        if view or option.view:
            t.view()
        t.buildrtyper(type_system="ootype").specialize()

        if view or option.view:
            t.view()
        #self.js = JS(t, [function, callback_function], stackless)
        self.js = JS(t, function, stackless)
        self.js.write_source()
        if root is None and use_tg:
            from pypy.translator.js.demo.jsdemo.controllers import Root
            self.root = Root
        else:
            self.root = root
        self.run_browser = run_browser
        self.function_calls = []
Beispiel #25
0
    def test_register_external_return_goes_back(self):
        """
        Check whether it works to pass the same list from one external
        fun to another
        [bookkeeper and list joining issues]
        """
        def function_with_list():
            pass

        register_external(function_with_list, [[int]], int)

        def function_returning_list():
            pass

        register_external(function_returning_list, [], [int])

        def f():
            return function_with_list(function_returning_list())

        policy = AnnotatorPolicy()
        policy.allow_someobjects = False
        a = RPythonAnnotator(policy=policy)
        s = a.build_types(f, [])
        assert isinstance(s, annmodel.SomeInteger)
    def test_list_of_str0(self):
        str0 = annmodel.SomeString(no_nul=True)

        def os_execve(l):
            pass

        register_external(os_execve, [[str0]], None)

        def f(l):
            return os_execve(l)

        policy = AnnotatorPolicy()
        policy.allow_someobjects = False
        a = RPythonAnnotator(policy=policy)
        a.build_types(f, [[str]])  # Does not raise
        assert a.translator.config.translation.check_str_without_nul == False
        # Now enable the str0 check, and try again with a similar function
        a.translator.config.translation.check_str_without_nul = True

        def g(l):
            return os_execve(l)

        raises(Exception, a.build_types, g, [[str]])
        a.build_types(g, [[str0]])  # Does not raise
Beispiel #27
0
    def test_register_external_tuple_args(self):
        """
        Verify the annotation of a registered external function which takes a
        tuple argument.
        """
        def function_with_tuple_arg():
            """
            Dummy function which is declared via register_external to take a
            tuple as an argument so that register_external's behavior for
            tuple-taking functions can be verified.
            """

        register_external(function_with_tuple_arg, [(int, )], int)

        def f():
            return function_with_tuple_arg((1, ))

        policy = AnnotatorPolicy()
        policy.allow_someobjects = False
        a = RPythonAnnotator(policy=policy)
        s = a.build_types(f, [])

        # Not a very good assertion, but at least it means _something_ happened.
        assert isinstance(s, annmodel.SomeInteger)
Beispiel #28
0
    def test_list_of_str0(self):
        str0 = annmodel.SomeString(no_nul=True)

        def os_execve(l):
            pass

        register_external(os_execve, [[str0]], None)

        def f(l):
            return os_execve(l)

        policy = AnnotatorPolicy()
        policy.allow_someobjects = False
        a = RPythonAnnotator(policy=policy)
        a.build_types(f, [[str]])  # Does not raise
        assert a.translator.config.translation.check_str_without_nul == False
        # Now enable the str0 check, and try again with a similar function
        a.translator.config.translation.check_str_without_nul = True

        def g(l):
            return os_execve(l)

        raises(Exception, a.build_types, g, [[str]])
        a.build_types(g, [[str0]])  # Does not raise
Beispiel #29
0
def test_compile_timer():
    policy = AnnotatorPolicy()
    policy.allow_someobjects = False
    f_compiled = compile(timer_user, [], annotatorpolicy=policy)
    f_compiled(expected_extra_mallocs=2)
Beispiel #30
0
def test_compile_timer():
    policy = AnnotatorPolicy()
    policy.allow_someobjects = False
    f_compiled = compile(timer_user, [], annotatorpolicy=policy)
    f_compiled(expected_extra_mallocs=2)
Beispiel #31
0
        assert res == 101

    def test_recursive_structure(self):
        P1 = RPointer(None)
        S1 = RStruct('S1', [('next', P1)])
        P1.setpointertype(S1)
        def func():
            s1 = S1.allocate()
            s2 = S1.allocate()
            s2.ref_next().set_contents(s1)
            return s2.ref_next().get_contents().sameaddr(s1)
        res = self.do(func)
        assert res == True

POLICY = AnnotatorPolicy()
POLICY.allow_someobjects = False

class TestLLInterpreted(TestBasic):

    def do(self, func):
        return interpret(func, [], policy=POLICY, backendopt=True)

    def test_simple_struct(self):
        S0 = RStruct('S0', [('x', rc_int)])
        def func():
            s = S0.allocate()
            s.ref_x().set_value(12)
            return s.ref_x().get_value()

        interp, graph = get_interpreter(func, [], policy=POLICY,
                                        backendopt=True)