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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
# mode: run
# tag: generators, pep479
from __future__ import generator_stop
import sys
if sys.version_info[0] >= 3:
# additionally test exception chaining
__doc__ = u"""
>>> g = test_raise_StopIteration_value()
>>> next(g)
1
>>> try: next(g)
... except RuntimeError as exc:
... print(type(exc.__context__) is StopIteration or type(exc.__context__), exc.__context__)
... else:
... print("NOT RAISED!")
True huhu
"""
def test_raise_StopIteration():
"""
>>> g = test_raise_StopIteration()
>>> next(g)
1
>>> next(g)
Traceback (most recent call last):
RuntimeError: generator raised StopIteration
"""
yield 1
raise StopIteration
def test_raise_StopIteration_value():
"""
>>> g = test_raise_StopIteration_value()
>>> next(g)
1
>>> next(g)
Traceback (most recent call last):
RuntimeError: generator raised StopIteration
"""
yield 1
raise StopIteration('huhu')
def test_return():
"""
>>> g = test_return()
>>> next(g)
1
>>> next(g)
Traceback (most recent call last):
StopIteration
"""
yield 1
return
def test_return_value():
"""
>>> g = test_return_value()
>>> next(g)
1
>>> next(g)
Traceback (most recent call last):
StopIteration: 2
"""
yield 1
return 2
def test_propagate_StopIteration(it):
"""
>>> results = []
>>> for x in test_propagate_StopIteration(iter([])):
... results.append(x)
Traceback (most recent call last):
RuntimeError: generator raised StopIteration
>>> results
[]
>>> for x in test_propagate_StopIteration(iter([1, 2])):
... results.append(x)
Traceback (most recent call last):
RuntimeError: generator raised StopIteration
>>> results
[1, 2]
"""
while True:
yield next(it)
def test_catch_StopIteration(it):
"""
>>> for x in test_catch_StopIteration(iter([])):
... print(x)
>>> for x in test_catch_StopIteration(iter([1, 2])):
... print(x)
1
2
"""
try:
while True:
yield next(it)
except StopIteration:
pass
else:
print("NOT RAISED!")
def test_yield_from(it):
"""
>>> for x in test_yield_from(iter([])):
... print(x)
>>> for x in test_yield_from(iter([1, 2])):
... print(x)
1
2
"""
yield from it
def test_yield_from_gen():
"""
>>> for x in test_yield_from_gen():
... print(x)
1
RETURN: 2
"""
x = yield from test_return_value()
print("RETURN: %s" % x)
def test_genexpr(it):
"""
>>> list(test_genexpr(iter([])))
[]
>>> list(test_genexpr(iter([1, 2])))
[1]
>>> list(test_genexpr(iter([1])))
Traceback (most recent call last):
RuntimeError: generator raised StopIteration
>>> list(test_genexpr(iter([1, 2, 3])))
Traceback (most recent call last):
RuntimeError: generator raised StopIteration
>>> list(test_genexpr(iter([1, 2])))
[1]
"""
return (x for x in it if next(it))
|