File: test_unsimplify.py

package info (click to toggle)
pypy 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 107,216 kB
  • sloc: python: 1,201,787; ansic: 62,419; asm: 5,169; cpp: 3,017; sh: 2,534; makefile: 545; xml: 243; lisp: 45; awk: 4
file content (108 lines) | stat: -rw-r--r-- 3,413 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import os
from rpython.translator.translator import TranslationContext, graphof
from rpython.translator.unsimplify import split_block, call_final_function
from rpython.translator.unsimplify import call_initial_function
from rpython.rtyper.llinterp import LLInterpreter
from rpython.flowspace.model import checkgraph
from rpython.rlib.objectmodel import we_are_translated
from rpython.tool.udir import udir

def translate(func, argtypes):
    t = TranslationContext()
    t.buildannotator().build_types(func, argtypes)
    t.entry_point_graph = graphof(t, func)
    t.buildrtyper().specialize()
    return graphof(t, func), t

def test_split_blocks_simple():
    for i in range(4):
        def f(x, y):
            z = x + y
            w = x * y
            return z + w
        graph, t = translate(f, [int, int])
        split_block(graph.startblock, i)
        checkgraph(graph)
        interp = LLInterpreter(t.rtyper)
        result = interp.eval_graph(graph, [1, 2])
        assert result == 5

def test_split_blocks_conditional():
    for i in range(3):
        def f(x, y):
            if x + 12:
                return y + 1
            else:
                return y + 2
        graph, t = translate(f, [int, int])
        split_block(graph.startblock, i)
        checkgraph(graph)
        interp = LLInterpreter(t.rtyper)
        result = interp.eval_graph(graph, [-12, 2])
        assert result == 4
        result = interp.eval_graph(graph, [0, 2])
        assert result == 3

def test_split_block_exceptions():
    for i in range(2):
        def raises(x):
            if x == 1:
                raise ValueError
            elif x == 2:
                raise KeyError
            return x
        def catches(x):
            try:
                y = x + 1
                raises(y)
            except ValueError:
                return 0
            except KeyError:
                return 1
            return x
        graph, t = translate(catches, [int])
        split_block(graph.startblock, i)
        checkgraph(graph)
        interp = LLInterpreter(t.rtyper)
        result = interp.eval_graph(graph, [0])
        assert result == 0
        result = interp.eval_graph(graph, [1])
        assert result == 1
        result = interp.eval_graph(graph, [2])
        assert result == 2

def test_call_initial_function():
    tmpfile = str(udir.join('test_call_initial_function'))
    def f(x):
        return x * 6
    def hello_world():
        if we_are_translated():
            fd = os.open(tmpfile, os.O_WRONLY | os.O_CREAT, 0644)
            os.close(fd)
    graph, t = translate(f, [int])
    call_initial_function(t, hello_world)
    #
    if os.path.exists(tmpfile):
        os.unlink(tmpfile)
    interp = LLInterpreter(t.rtyper)
    result = interp.eval_graph(graph, [7])
    assert result == 42
    assert os.path.isfile(tmpfile)

def test_call_final_function():
    tmpfile = str(udir.join('test_call_final_function'))
    def f(x):
        return x * 6
    def goodbye_world():
        if we_are_translated():
            fd = os.open(tmpfile, os.O_WRONLY | os.O_CREAT, 0644)
            os.close(fd)
    graph, t = translate(f, [int])
    call_final_function(t, goodbye_world)
    #
    if os.path.exists(tmpfile):
        os.unlink(tmpfile)
    interp = LLInterpreter(t.rtyper)
    result = interp.eval_graph(graph, [7])
    assert result == 42
    assert os.path.isfile(tmpfile)