Exemplo n.º 1
0
 def test_no_multiple_transform(self):
     def f(x):
         return x + 1
     t = TranslationContext()
     t.buildannotator().build_types(f, [int])
     t.buildrtyper().specialize()
     g = graphof(t, f)
     etrafo = exceptiontransform.ExceptionTransformer(t)
     etrafo.create_exception_handling(g)
     etrafo2 = exceptiontransform.ExceptionTransformer(t)
     py.test.raises(AssertionError, etrafo2.create_exception_handling, g)
Exemplo n.º 2
0
def interpret(func, values):
    interp, graph = get_interpreter(func, values)
    t = interp.typer.annotator.translator
    if t not in _already_transformed:
        etrafo = exceptiontransform.ExceptionTransformer(t)
        etrafo.transform_completely()
        _already_transformed[t] = True
    return interp.eval_graph(graph, values)
Exemplo n.º 3
0
def test_half_exceptiontransformed_graphs():
    from rpython.translator import exceptiontransform

    def f1(x):
        if x < 0:
            raise ValueError
        return 754

    def g1(x):
        try:
            return f1(x)
        except ValueError:
            return 5

    def f2(x):
        if x < 0:
            raise ValueError
        return 21

    def g2(x):
        try:
            return f2(x)
        except ValueError:
            return 6

    f3 = lltype.functionptr(lltype.FuncType([lltype.Signed], lltype.Signed),
                            'f3',
                            _callable=f1)

    def g3(x):
        try:
            return f3(x)
        except ValueError:
            return 7

    def f(flag, x):
        if flag == 1:
            return g1(x)
        elif flag == 2:
            return g2(x)
        else:
            return g3(x)

    t = TranslationContext()
    t.buildannotator().build_types(f, [int, int])
    t.buildrtyper().specialize()
    etrafo = exceptiontransform.ExceptionTransformer(t)
    etrafo.create_exception_handling(graphof(t, f1))
    etrafo.create_exception_handling(graphof(t, g2))
    etrafo.create_exception_handling(graphof(t, g3))
    graph = graphof(t, f)
    interp = LLInterpreter(t.rtyper)
    res = interp.eval_graph(graph, [1, -64])
    assert res == 5
    res = interp.eval_graph(graph, [2, -897])
    assert res == 6
    res = interp.eval_graph(graph, [3, -9831])
    assert res == 7
Exemplo n.º 4
0
 def test_preserve_can_raise(self):
     def f(x):
         raise ValueError
     t = TranslationContext()
     t.buildannotator().build_types(f, [int])
     t.buildrtyper().specialize()
     g = graphof(t, f)
     etrafo = exceptiontransform.ExceptionTransformer(t)
     etrafo.create_exception_handling(g)
     assert etrafo.raise_analyzer.analyze_direct_call(g)
Exemplo n.º 5
0
 def transform_func(self, fn, inputtypes, backendopt=False):
     t = TranslationContext()
     t.buildannotator().build_types(fn, inputtypes)
     t.buildrtyper().specialize()
     if option.view:
         t.view()
     if backendopt:
         backend_optimizations(t)
     g = graphof(t, fn)
     etrafo = exceptiontransform.ExceptionTransformer(t)
     etrafo.create_exception_handling(g)
     join_blocks(g)
     if option.view:
         t.view()
     return t, g
Exemplo n.º 6
0
 def test_inserting_zeroing_op(self):
     from rpython.rtyper.lltypesystem import lltype
     S = lltype.GcStruct("S", ('x', lltype.Signed))
     def f(x):
         s = lltype.malloc(S)
         s.x = 0
         return s.x
     t = TranslationContext()
     t.buildannotator().build_types(f, [int])
     t.buildrtyper().specialize()
     g = graphof(t, f)
     etrafo = exceptiontransform.ExceptionTransformer(t)
     etrafo.create_exception_handling(g)
     ops = dict.fromkeys([o.opname for b, o in g.iterblockops()])
     assert 'zero_gc_pointers_inside' in ops
Exemplo n.º 7
0
def test_exceptiontransformed_add_ovf():
    from rpython.translator import exceptiontransform

    def f(x, y):
        try:
            return ovfcheck(x + y)
        except OverflowError:
            return -42

    t = TranslationContext()
    t.buildannotator().build_types(f, [int, int])
    t.buildrtyper().specialize()
    etrafo = exceptiontransform.ExceptionTransformer(t)
    graph = graphof(t, f)
    etrafo.create_exception_handling(graph)
    interp = LLInterpreter(t.rtyper)
    res = interp.eval_graph(graph, [1, -64])
    assert res == -63
    res = interp.eval_graph(graph, [1, sys.maxint])
    assert res == -42