| 1 |
import py |
| 2 |
|
| 3 |
from rpython.rtyper.test.tool import BaseRtypingTest |
| 4 |
|
| 5 |
|
| 6 |
class TestGenerator(BaseRtypingTest): |
| 7 |
|
| 8 |
def test_simple_explicit(self): |
| 9 |
def g(a, b, c): |
| 10 |
yield a |
| 11 |
yield b |
| 12 |
yield c |
| 13 |
def f(): |
| 14 |
gen = g(3, 5, 8) |
| 15 |
x = gen.next() * 100 |
| 16 |
x += gen.next() * 10 |
| 17 |
x += gen.next() |
| 18 |
return x |
| 19 |
res = self.interpret(f, []) |
| 20 |
assert res == 358 |
| 21 |
|
| 22 |
def test_cannot_merge(self): |
| 23 |
|
| 24 |
|
| 25 |
class MyGen: |
| 26 |
_immutable_ = True |
| 27 |
def next(self): |
| 28 |
raise NotImplementedError |
| 29 |
class MyG1(MyGen): |
| 30 |
_immutable_ = True |
| 31 |
def __init__(self, a): |
| 32 |
self._gen = self.g1(a) |
| 33 |
def next(self): |
| 34 |
return self._gen.next() |
| 35 |
@staticmethod |
| 36 |
def g1(a): |
| 37 |
yield a + 1 |
| 38 |
yield a + 2 |
| 39 |
class MyG2(MyGen): |
| 40 |
_immutable_ = True |
| 41 |
def __init__(self): |
| 42 |
self._gen = self.g2() |
| 43 |
def next(self): |
| 44 |
return self._gen.next() |
| 45 |
@staticmethod |
| 46 |
def g2(): |
| 47 |
yield 42 |
| 48 |
def f(n): |
| 49 |
if n > 0: |
| 50 |
gen = MyG1(n) |
| 51 |
else: |
| 52 |
gen = MyG2() |
| 53 |
return gen.next() |
| 54 |
res = self.interpret(f, [10]) |
| 55 |
assert res == 11 |
| 56 |
res = self.interpret(f, [0]) |
| 57 |
assert res == 42 |
| 58 |
|
| 59 |
def test_except_block(self): |
| 60 |
def foo(): |
| 61 |
raise ValueError |
| 62 |
def g(a, b, c): |
| 63 |
yield a |
| 64 |
yield b |
| 65 |
try: |
| 66 |
foo() |
| 67 |
except ValueError: |
| 68 |
pass |
| 69 |
yield c |
| 70 |
def f(): |
| 71 |
gen = g(3, 5, 8) |
| 72 |
x = gen.next() * 100 |
| 73 |
x += gen.next() * 10 |
| 74 |
x += gen.next() |
| 75 |
return x |
| 76 |
res = self.interpret(f, []) |
| 77 |
assert res == 358 |
| 78 |
|
| 79 |
@py.test.mark.xfail |
| 80 |
def test_different_exception(self): |
| 81 |
def h(c): |
| 82 |
if c == 8: |
| 83 |
raise ValueError |
| 84 |
def g(c): |
| 85 |
try: |
| 86 |
h(c) |
| 87 |
except Exception as e: |
| 88 |
if isinstance(e, ValueError): |
| 89 |
raise |
| 90 |
raise StopIteration |
| 91 |
yield c |
| 92 |
|
| 93 |
def f(x): |
| 94 |
try: |
| 95 |
for x in g(x): |
| 96 |
pass |
| 97 |
except ValueError: |
| 98 |
return -5 |
| 99 |
return 5 |
| 100 |
assert f(8) == -5 |
| 101 |
res = self.interpret(f, [8]) |
| 102 |
assert res == -5 |
| 103 |
|
| 104 |
def test_iterating_generator(self): |
| 105 |
def f(): |
| 106 |
yield 1 |
| 107 |
yield 2 |
| 108 |
yield 3 |
| 109 |
def g(): |
| 110 |
s = 0 |
| 111 |
for x in f(): |
| 112 |
s += x |
| 113 |
return s |
| 114 |
res = self.interpret(g, []) |
| 115 |
assert res == 6 |
| 116 |
|
| 117 |
def test_generator_with_unreachable_yields(self): |
| 118 |
def f(n): |
| 119 |
if n < 0: |
| 120 |
yield 42 |
| 121 |
yield n |
| 122 |
if n < 0: |
| 123 |
yield 43 |
| 124 |
yield n |
| 125 |
if n < 0: |
| 126 |
yield 44 |
| 127 |
def main(n): |
| 128 |
y = 0 |
| 129 |
for x in f(abs(n)): |
| 130 |
y += x |
| 131 |
return y |
| 132 |
res = self.interpret(main, [-100]) |
| 133 |
assert res == 200 |