| 1 |
import os |
| 2 |
from rpython.translator.translator import TranslationContext, graphof |
| 3 |
from rpython.translator.unsimplify import split_block, call_final_function |
| 4 |
from rpython.translator.unsimplify import call_initial_function |
| 5 |
from rpython.rtyper.llinterp import LLInterpreter |
| 6 |
from rpython.flowspace.model import checkgraph |
| 7 |
from rpython.rlib.objectmodel import we_are_translated |
| 8 |
from rpython.tool.udir import udir |
| 9 |
|
| 10 |
def translate(func, argtypes): |
| 11 |
t = TranslationContext() |
| 12 |
t.buildannotator().build_types(func, argtypes) |
| 13 |
t.entry_point_graph = graphof(t, func) |
| 14 |
t.buildrtyper().specialize() |
| 15 |
return graphof(t, func), t |
| 16 |
|
| 17 |
def test_split_blocks_simple(): |
| 18 |
for i in range(4): |
| 19 |
def f(x, y): |
| 20 |
z = x + y |
| 21 |
w = x * y |
| 22 |
return z + w |
| 23 |
graph, t = translate(f, [int, int]) |
| 24 |
split_block(graph.startblock, i) |
| 25 |
checkgraph(graph) |
| 26 |
interp = LLInterpreter(t.rtyper) |
| 27 |
result = interp.eval_graph(graph, [1, 2]) |
| 28 |
assert result == 5 |
| 29 |
|
| 30 |
def test_split_blocks_conditional(): |
| 31 |
for i in range(3): |
| 32 |
def f(x, y): |
| 33 |
if x + 12: |
| 34 |
return y + 1 |
| 35 |
else: |
| 36 |
return y + 2 |
| 37 |
graph, t = translate(f, [int, int]) |
| 38 |
split_block(graph.startblock, i) |
| 39 |
checkgraph(graph) |
| 40 |
interp = LLInterpreter(t.rtyper) |
| 41 |
result = interp.eval_graph(graph, [-12, 2]) |
| 42 |
assert result == 4 |
| 43 |
result = interp.eval_graph(graph, [0, 2]) |
| 44 |
assert result == 3 |
| 45 |
|
| 46 |
def test_split_block_exceptions(): |
| 47 |
for i in range(2): |
| 48 |
def raises(x): |
| 49 |
if x == 1: |
| 50 |
raise ValueError |
| 51 |
elif x == 2: |
| 52 |
raise KeyError |
| 53 |
return x |
| 54 |
def catches(x): |
| 55 |
try: |
| 56 |
y = x + 1 |
| 57 |
raises(y) |
| 58 |
except ValueError: |
| 59 |
return 0 |
| 60 |
except KeyError: |
| 61 |
return 1 |
| 62 |
return x |
| 63 |
graph, t = translate(catches, [int]) |
| 64 |
split_block(graph.startblock, i) |
| 65 |
checkgraph(graph) |
| 66 |
interp = LLInterpreter(t.rtyper) |
| 67 |
result = interp.eval_graph(graph, [0]) |
| 68 |
assert result == 0 |
| 69 |
result = interp.eval_graph(graph, [1]) |
| 70 |
assert result == 1 |
| 71 |
result = interp.eval_graph(graph, [2]) |
| 72 |
assert result == 2 |
| 73 |
|
| 74 |
def test_call_initial_function(): |
| 75 |
tmpfile = str(udir.join('test_call_initial_function')) |
| 76 |
def f(x): |
| 77 |
return x * 6 |
| 78 |
def hello_world(): |
| 79 |
if we_are_translated(): |
| 80 |
fd = os.open(tmpfile, os.O_WRONLY | os.O_CREAT, 0644) |
| 81 |
os.close(fd) |
| 82 |
graph, t = translate(f, [int]) |
| 83 |
call_initial_function(t, hello_world) |
| 84 |
|
| 85 |
if os.path.exists(tmpfile): |
| 86 |
os.unlink(tmpfile) |
| 87 |
interp = LLInterpreter(t.rtyper) |
| 88 |
result = interp.eval_graph(graph, [7]) |
| 89 |
assert result == 42 |
| 90 |
assert os.path.isfile(tmpfile) |
| 91 |
|
| 92 |
def test_call_final_function(): |
| 93 |
tmpfile = str(udir.join('test_call_final_function')) |
| 94 |
def f(x): |
| 95 |
return x * 6 |
| 96 |
def goodbye_world(): |
| 97 |
if we_are_translated(): |
| 98 |
fd = os.open(tmpfile, os.O_WRONLY | os.O_CREAT, 0644) |
| 99 |
os.close(fd) |
| 100 |
graph, t = translate(f, [int]) |
| 101 |
call_final_function(t, goodbye_world) |
| 102 |
|
| 103 |
if os.path.exists(tmpfile): |
| 104 |
os.unlink(tmpfile) |
| 105 |
interp = LLInterpreter(t.rtyper) |
| 106 |
result = interp.eval_graph(graph, [7]) |
| 107 |
assert result == 42 |
| 108 |
assert os.path.isfile(tmpfile) |