-
Notifications
You must be signed in to change notification settings - Fork 5
/
yieldfrom.py
360 lines (291 loc) · 11.8 KB
/
yieldfrom.py
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
"""Backporting of the ``yield from`` semantic from Python 3.x.
If you want to nest generators in Python 3.x, you can use the ``yield from``
keywords. This allows you to automatically iterate over sub-generators and
transparently pass exceptions and return values from the top level caller
to the lowest generator.
.. code-block:: python
def subgen():
yield 2
yield 3
def gen():
yield 1
yield from subgen() # Python 3.x only
yield 4
def main():
for i in gen():
print i,
>>> main()
... 1 2 3 4
This functionality is not available in Python 2.x, and we emulate it using the
:py:func:`yieldfrom` decorator and the helper :py:class:`From` class:
.. code-block:: python
from yieldfrom import yieldfrom, From
def subgen():
yield 2
yield 3
@yieldfrom
def gen():
yield 1
yield From(subgen())
yield 4
def main():
for i in gen():
print i,
>>> main()
... 1 2 3 4
Advanced usage allows returning a value from the subgenerator using
:py:exc:`StopIteration`. Using :py:func:`Return` does this conveniently:
.. code-block:: python
from yieldfrom import yieldfrom, From, Return
def subgen():
yield 2
yield 3
Return(100) # Raises `StopIteration(100)`
@yieldfrom
def gen():
yield 1
ret = (yield From(subgen()))
yield 4
yield ret
def main():
for i in gen():
print i,
>>> main()
... 1 2 3 4 100
Subgenerators can be nested on multiple levels, each one requiring additional
decoration by :py:func:`yieldfrom`:
.. code-block:: python
def subsubgen():
yield 2
@yieldfrom
def subgen():
yield From(subsubgen())
yield 3
@yieldfrom
def gen():
yield 1
yield From(subgen())
yield 4
def main():
for i in gen():
print i,
>>> main()
... 1 2 3 4
Exceptions thrown into the top-level generator can be handled in relevant
subgenerators:
.. code-block:: python
def subsubgen():
try:
yield 2
except ValueError:
yield 200
@yieldfrom
def subgen():
yield From(subsubgen())
yield 3
@yieldfrom
def gen():
yield 1
yield From(subgen())
yield 4
def main():
try:
g = gen()
while True:
i = next(g)
if i == 2:
i = g.throw(ValueError())
print i,
except StopIteration:
pass
>>> main()
... 1 200 3 4
Note that if you use ``yield From()`` on a simple iterable (``list``,
``tuple``, etc) then the individual members of the iterator will be yielded on
each iteration (perhaps in that case you need the usual ``yield``).
.. code-block:: python
@yieldfrom
def gen():
yield From([1, 2, 3])
yield [1, 2, 3]
def main():
for i in gen():
print i
>>> main()
... 1
... 2
... 3
... [1, 2, 3]
Passing non-iterable objects to :py:class:`From` will result in an empty
generator that does nothing.
.. code-block:: python
@yieldfrom
def gen():
yield From(None)
yield 1
def main():
for i in gen():
print i
>>> main()
... 1
This module is an adaptation of the following Python recipe:
http://code.activestate.com/recipes/576727
Modifications include bug fixes in exception handling, naming, documentation,
handling of empty generators, etc.
"""
__version__ = '1.0.6rc'
__all__ = ('yieldfrom', 'From', 'Return')
import sys
import functools
def Return(return_value):
"""Return a value from a generator by raising ``StopIteration``."""
raise StopIteration(return_value)
class From(object):
"""Helper class to wrap subiterators.
If the expression wrapped is not iterable, holds an empty generator.
"""
def __init__(self, iterable_expression):
try:
self.iterator = iter(iterable_expression)
except TypeError:
self.iterator = iter([])
def get_stop_iteration_value(e_stop):
return e_stop.args[0] if e_stop.args else None
def close_safely(gen):
"""Close generator, ignore if has no ``close`` attribute."""
try:
_close = gen.close
except AttributeError:
pass
else:
_close()
def yieldfrom(generator_func):
"""Decorate a function to enable ``yield From(generator)``.
Implements PEP380 (``yield from``) in Python 2.x.
"""
@functools.wraps(generator_func)
def wrapper(*args, **kwargs):
# gen in the function body that is decorated by `yieldfrom`. (it is
# a generator, too).
gen = generator_func(*args, **kwargs)
try:
# First poll of `gen`.
item = next(gen)
# OUTER loop: iterate over all the values yielded by `gen`.
while True:
# ------------------------------------------------------------
# Handling normal `yield`
# ------------------------------------------------------------
if not isinstance(item, From):
try:
sent = (yield item)
except Exception:
# Caller did `throw`. We push the exception back into
# `gen` and get the next item.
item = gen.throw(*sys.exc_info())
else:
# No exception was thrown, push the return value into
# `gen` and get the next item.
item = gen.send(sent)
# ------------------------------------------------------------
# Handling `yield From()`
# ------------------------------------------------------------
else:
subgen = item.iterator
try:
# First poll of `subgen`.
subitem = next(subgen)
except StopIteration as e_stop:
# `subgen` exhausted on first poll. Extract return
# value passed by `StopIteration`, push it into `gen`
# and get the next item.
item = gen.send(get_stop_iteration_value(e_stop))
except BaseException:
# `subgen` raised an exception before its first
# `yield`. We give `gen` a chance to handle the raised
# exception.
item = gen.throw(*sys.exc_info())
# `subgen` raised an exception and `gen` caught it.
# Execution will continue on the OUTER loop.
else:
# INNER loop: iterate on values yielded by
# subgenerator.
while True:
try:
# Yield what the subgenerator did.
sent = (yield subitem)
except GeneratorExit:
# Higher level caller called `close()`.
# Close the subgenerator if possible.
close_safely(subgen)
raise
except BaseException:
# Higher level caller called `throw()`.
try:
_throw = subgen.throw
except AttributeError:
# `subgen` doesn't have a `throw()` method.
# We consider it exhausted, so we push the
# exception into `gen` instead, and get
# the next item.
item = gen.throw(*sys.exc_info())
break # Restart OUTER loop.
else:
try:
# Push the exception into `subgen` and
# get the next subitem.
subitem = _throw(*sys.exc_info())
except StopIteration as e_stop:
# `subgen` is exhausted. Retrieve its
# return value, push it into `gen` and
# get the next item.
item = gen.send(
get_stop_iteration_value(e_stop))
break # Restart OUTER loop.
except BaseException:
# `subgen` did not handle the thrown
# exception (or raised a different
# one). We give `gen` a chance to
# handle the raised exception.
item = gen.throw(*sys.exc_info())
# Restart the OUTER loop because
# `subgen` raised an exception and
# `gen` caught it.
break
else:
continue # Restart INNER loop
else:
try:
# Re-poll `subgen`
# It would be possible to just call
# `send()` even when `sent` is None,
# however, for non-generator iterables this
# won't work.
if sent:
subitem = subgen.send(sent)
else:
subitem = next(subgen)
except StopIteration as e_stop:
# `subgen` is exhausted. Retrieve its
# return value, push it into `gen` and
# get the next item.
item = gen.send(
get_stop_iteration_value(e_stop))
break # Restart OUTER loop.
except BaseException:
# `subgen` raised an exception.
# We give `gen` a chance to
# handle the raised exception.
item = gen.throw(*sys.exc_info())
# Restart the OUTER loop because
# `subgen` raised an exception and
# `gen` caught it.
break
else:
continue # Restart INNER loop
finally:
# `gen` raised an exception or caller called `close()` or generator
# was garbage collected.
# Close the generator if possible.
close_safely(gen)
return wrapper