| 1 |
from rpython.conftest import option |
| 2 |
from rpython.flowspace.objspace import build_flow |
| 3 |
from rpython.flowspace.model import Variable |
| 4 |
from rpython.flowspace.generator import ( |
| 5 |
make_generator_entry_graph, get_variable_names) |
| 6 |
from rpython.translator.simplify import join_blocks |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
def f_gen(n): |
| 12 |
i = 0 |
| 13 |
while i < n: |
| 14 |
yield i |
| 15 |
i += 1 |
| 16 |
|
| 17 |
class GeneratorIterator(object): |
| 18 |
def __init__(self, entry): |
| 19 |
self.current = entry |
| 20 |
def next(self): |
| 21 |
e = self.current |
| 22 |
self.current = None |
| 23 |
if isinstance(e, Yield1): |
| 24 |
n = e.n_0 |
| 25 |
i = e.i_0 |
| 26 |
i += 1 |
| 27 |
else: |
| 28 |
n = e.n_0 |
| 29 |
i = 0 |
| 30 |
if i < n: |
| 31 |
e = Yield1() |
| 32 |
e.n_0 = n |
| 33 |
e.i_0 = i |
| 34 |
self.current = e |
| 35 |
return i |
| 36 |
raise StopIteration |
| 37 |
|
| 38 |
def __iter__(self): |
| 39 |
return self |
| 40 |
|
| 41 |
class AbstractPosition(object): |
| 42 |
_immutable_ = True |
| 43 |
class Entry1(AbstractPosition): |
| 44 |
_immutable_ = True |
| 45 |
class Yield1(AbstractPosition): |
| 46 |
_immutable_ = True |
| 47 |
|
| 48 |
def f_explicit(n): |
| 49 |
e = Entry1() |
| 50 |
e.n_0 = n |
| 51 |
return GeneratorIterator(e) |
| 52 |
|
| 53 |
def test_explicit(): |
| 54 |
assert list(f_gen(10)) == list(f_explicit(10)) |
| 55 |
|
| 56 |
def test_get_variable_names(): |
| 57 |
lst = get_variable_names([Variable('a'), Variable('b_'), Variable('a')]) |
| 58 |
assert lst == ['g_a', 'g_b', 'g_a_'] |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
class TestGenerator: |
| 64 |
|
| 65 |
def test_replace_graph_with_bootstrap(self): |
| 66 |
def func(n, x, y, z): |
| 67 |
yield n |
| 68 |
yield n |
| 69 |
|
| 70 |
graph = make_generator_entry_graph(func) |
| 71 |
if option.view: |
| 72 |
graph.show() |
| 73 |
block = graph.startblock |
| 74 |
ops = block.operations |
| 75 |
assert ops[0].opname == 'simple_call' |
| 76 |
assert ops[1].opname == 'setattr' |
| 77 |
assert ops[1].args[1].value == 'g_n' |
| 78 |
assert ops[2].opname == 'setattr' |
| 79 |
assert ops[2].args[1].value == 'g_x' |
| 80 |
assert ops[3].opname == 'setattr' |
| 81 |
assert ops[3].args[1].value == 'g_y' |
| 82 |
assert ops[4].opname == 'setattr' |
| 83 |
assert ops[4].args[1].value == 'g_z' |
| 84 |
assert ops[5].opname == 'simple_call' |
| 85 |
assert ops[5].args[1] == ops[0].result |
| 86 |
assert len(ops) == 6 |
| 87 |
assert len(block.exits) == 1 |
| 88 |
assert block.exits[0].target is graph.returnblock |
| 89 |
|
| 90 |
def test_tweak_generator_graph(self): |
| 91 |
def f(n, x, y, z): |
| 92 |
z *= 10 |
| 93 |
yield n + 1 |
| 94 |
z -= 10 |
| 95 |
|
| 96 |
graph = make_generator_entry_graph(f) |
| 97 |
func1 = graph._tweaked_func |
| 98 |
if option.view: |
| 99 |
graph.show() |
| 100 |
GeneratorIterator = graph._tweaked_func._generator_next_method_of_ |
| 101 |
assert hasattr(GeneratorIterator, 'next') |
| 102 |
|
| 103 |
graph_next = build_flow(GeneratorIterator.next.im_func) |
| 104 |
join_blocks(graph_next) |
| 105 |
if option.view: |
| 106 |
graph_next.show() |
| 107 |
|
| 108 |
graph1 = build_flow(func1) |
| 109 |
if option.view: |
| 110 |
graph1.show() |
| 111 |
|
| 112 |
def test_automatic(self): |
| 113 |
def f(n, x, y, z): |
| 114 |
z *= 10 |
| 115 |
yield n + 1 |
| 116 |
z -= 10 |
| 117 |
|
| 118 |
graph = build_flow(f) |
| 119 |
if option.view: |
| 120 |
graph.show() |
| 121 |
block = graph.startblock |
| 122 |
assert len(block.exits) == 1 |
| 123 |
assert block.exits[0].target is graph.returnblock |