2
# tag: generators, pep479
4
from __future__ import generator_stop
6
# additionally test exception chaining
8
>>> g = test_raise_StopIteration_value()
12
... except RuntimeError as exc:
13
... print(type(exc.__context__) is StopIteration or type(exc.__context__), exc.__context__)
14
... print(type(exc.__cause__) is StopIteration or type(exc.__cause__), exc.__context__)
16
... print("NOT RAISED!")
22
def test_raise_StopIteration():
24
>>> g = test_raise_StopIteration()
28
Traceback (most recent call last):
29
RuntimeError: generator raised StopIteration
35
def test_raise_StopIteration_value():
37
>>> g = test_raise_StopIteration_value()
41
Traceback (most recent call last):
42
RuntimeError: generator raised StopIteration
45
raise StopIteration('huhu')
54
Traceback (most recent call last):
61
def test_return_value():
63
>>> g = test_return_value()
67
Traceback (most recent call last):
74
def test_propagate_StopIteration(it):
77
>>> for x in test_propagate_StopIteration(iter([])):
79
Traceback (most recent call last):
80
RuntimeError: generator raised StopIteration
84
>>> for x in test_propagate_StopIteration(iter([1, 2])):
86
Traceback (most recent call last):
87
RuntimeError: generator raised StopIteration
95
def test_catch_StopIteration(it):
97
>>> for x in test_catch_StopIteration(iter([])):
100
>>> for x in test_catch_StopIteration(iter([1, 2])):
108
except StopIteration:
114
def test_yield_from(it):
116
>>> for x in test_yield_from(iter([])):
119
>>> for x in test_yield_from(iter([1, 2])):
127
def test_yield_from_gen():
129
>>> for x in test_yield_from_gen():
134
x = yield from test_return_value()
135
print("RETURN: %s" % x)
140
>>> list(test_genexpr(iter([])))
142
>>> list(test_genexpr(iter([1, 2])))
145
>>> list(test_genexpr(iter([1])))
146
Traceback (most recent call last):
147
RuntimeError: generator raised StopIteration
149
>>> list(test_genexpr(iter([1, 2, 3])))
150
Traceback (most recent call last):
151
RuntimeError: generator raised StopIteration
153
>>> list(test_genexpr(iter([1, 2])))
156
return (x for x in it if next(it))