1 | n/a | # -*- coding: utf-8 -*- |
---|
2 | n/a | |
---|
3 | n/a | """ |
---|
4 | n/a | Test suite for PEP 380 implementation |
---|
5 | n/a | |
---|
6 | n/a | adapted from original tests written by Greg Ewing |
---|
7 | n/a | see <http://www.cosc.canterbury.ac.nz/greg.ewing/python/yield-from/YieldFrom-Python3.1.2-rev5.zip> |
---|
8 | n/a | """ |
---|
9 | n/a | |
---|
10 | n/a | import unittest |
---|
11 | n/a | import inspect |
---|
12 | n/a | |
---|
13 | n/a | from test.support import captured_stderr, disable_gc, gc_collect |
---|
14 | n/a | |
---|
15 | n/a | class TestPEP380Operation(unittest.TestCase): |
---|
16 | n/a | """ |
---|
17 | n/a | Test semantics. |
---|
18 | n/a | """ |
---|
19 | n/a | |
---|
20 | n/a | def test_delegation_of_initial_next_to_subgenerator(self): |
---|
21 | n/a | """ |
---|
22 | n/a | Test delegation of initial next() call to subgenerator |
---|
23 | n/a | """ |
---|
24 | n/a | trace = [] |
---|
25 | n/a | def g1(): |
---|
26 | n/a | trace.append("Starting g1") |
---|
27 | n/a | yield from g2() |
---|
28 | n/a | trace.append("Finishing g1") |
---|
29 | n/a | def g2(): |
---|
30 | n/a | trace.append("Starting g2") |
---|
31 | n/a | yield 42 |
---|
32 | n/a | trace.append("Finishing g2") |
---|
33 | n/a | for x in g1(): |
---|
34 | n/a | trace.append("Yielded %s" % (x,)) |
---|
35 | n/a | self.assertEqual(trace,[ |
---|
36 | n/a | "Starting g1", |
---|
37 | n/a | "Starting g2", |
---|
38 | n/a | "Yielded 42", |
---|
39 | n/a | "Finishing g2", |
---|
40 | n/a | "Finishing g1", |
---|
41 | n/a | ]) |
---|
42 | n/a | |
---|
43 | n/a | def test_raising_exception_in_initial_next_call(self): |
---|
44 | n/a | """ |
---|
45 | n/a | Test raising exception in initial next() call |
---|
46 | n/a | """ |
---|
47 | n/a | trace = [] |
---|
48 | n/a | def g1(): |
---|
49 | n/a | try: |
---|
50 | n/a | trace.append("Starting g1") |
---|
51 | n/a | yield from g2() |
---|
52 | n/a | finally: |
---|
53 | n/a | trace.append("Finishing g1") |
---|
54 | n/a | def g2(): |
---|
55 | n/a | try: |
---|
56 | n/a | trace.append("Starting g2") |
---|
57 | n/a | raise ValueError("spanish inquisition occurred") |
---|
58 | n/a | finally: |
---|
59 | n/a | trace.append("Finishing g2") |
---|
60 | n/a | try: |
---|
61 | n/a | for x in g1(): |
---|
62 | n/a | trace.append("Yielded %s" % (x,)) |
---|
63 | n/a | except ValueError as e: |
---|
64 | n/a | self.assertEqual(e.args[0], "spanish inquisition occurred") |
---|
65 | n/a | else: |
---|
66 | n/a | self.fail("subgenerator failed to raise ValueError") |
---|
67 | n/a | self.assertEqual(trace,[ |
---|
68 | n/a | "Starting g1", |
---|
69 | n/a | "Starting g2", |
---|
70 | n/a | "Finishing g2", |
---|
71 | n/a | "Finishing g1", |
---|
72 | n/a | ]) |
---|
73 | n/a | |
---|
74 | n/a | def test_delegation_of_next_call_to_subgenerator(self): |
---|
75 | n/a | """ |
---|
76 | n/a | Test delegation of next() call to subgenerator |
---|
77 | n/a | """ |
---|
78 | n/a | trace = [] |
---|
79 | n/a | def g1(): |
---|
80 | n/a | trace.append("Starting g1") |
---|
81 | n/a | yield "g1 ham" |
---|
82 | n/a | yield from g2() |
---|
83 | n/a | yield "g1 eggs" |
---|
84 | n/a | trace.append("Finishing g1") |
---|
85 | n/a | def g2(): |
---|
86 | n/a | trace.append("Starting g2") |
---|
87 | n/a | yield "g2 spam" |
---|
88 | n/a | yield "g2 more spam" |
---|
89 | n/a | trace.append("Finishing g2") |
---|
90 | n/a | for x in g1(): |
---|
91 | n/a | trace.append("Yielded %s" % (x,)) |
---|
92 | n/a | self.assertEqual(trace,[ |
---|
93 | n/a | "Starting g1", |
---|
94 | n/a | "Yielded g1 ham", |
---|
95 | n/a | "Starting g2", |
---|
96 | n/a | "Yielded g2 spam", |
---|
97 | n/a | "Yielded g2 more spam", |
---|
98 | n/a | "Finishing g2", |
---|
99 | n/a | "Yielded g1 eggs", |
---|
100 | n/a | "Finishing g1", |
---|
101 | n/a | ]) |
---|
102 | n/a | |
---|
103 | n/a | def test_raising_exception_in_delegated_next_call(self): |
---|
104 | n/a | """ |
---|
105 | n/a | Test raising exception in delegated next() call |
---|
106 | n/a | """ |
---|
107 | n/a | trace = [] |
---|
108 | n/a | def g1(): |
---|
109 | n/a | try: |
---|
110 | n/a | trace.append("Starting g1") |
---|
111 | n/a | yield "g1 ham" |
---|
112 | n/a | yield from g2() |
---|
113 | n/a | yield "g1 eggs" |
---|
114 | n/a | finally: |
---|
115 | n/a | trace.append("Finishing g1") |
---|
116 | n/a | def g2(): |
---|
117 | n/a | try: |
---|
118 | n/a | trace.append("Starting g2") |
---|
119 | n/a | yield "g2 spam" |
---|
120 | n/a | raise ValueError("hovercraft is full of eels") |
---|
121 | n/a | yield "g2 more spam" |
---|
122 | n/a | finally: |
---|
123 | n/a | trace.append("Finishing g2") |
---|
124 | n/a | try: |
---|
125 | n/a | for x in g1(): |
---|
126 | n/a | trace.append("Yielded %s" % (x,)) |
---|
127 | n/a | except ValueError as e: |
---|
128 | n/a | self.assertEqual(e.args[0], "hovercraft is full of eels") |
---|
129 | n/a | else: |
---|
130 | n/a | self.fail("subgenerator failed to raise ValueError") |
---|
131 | n/a | self.assertEqual(trace,[ |
---|
132 | n/a | "Starting g1", |
---|
133 | n/a | "Yielded g1 ham", |
---|
134 | n/a | "Starting g2", |
---|
135 | n/a | "Yielded g2 spam", |
---|
136 | n/a | "Finishing g2", |
---|
137 | n/a | "Finishing g1", |
---|
138 | n/a | ]) |
---|
139 | n/a | |
---|
140 | n/a | def test_delegation_of_send(self): |
---|
141 | n/a | """ |
---|
142 | n/a | Test delegation of send() |
---|
143 | n/a | """ |
---|
144 | n/a | trace = [] |
---|
145 | n/a | def g1(): |
---|
146 | n/a | trace.append("Starting g1") |
---|
147 | n/a | x = yield "g1 ham" |
---|
148 | n/a | trace.append("g1 received %s" % (x,)) |
---|
149 | n/a | yield from g2() |
---|
150 | n/a | x = yield "g1 eggs" |
---|
151 | n/a | trace.append("g1 received %s" % (x,)) |
---|
152 | n/a | trace.append("Finishing g1") |
---|
153 | n/a | def g2(): |
---|
154 | n/a | trace.append("Starting g2") |
---|
155 | n/a | x = yield "g2 spam" |
---|
156 | n/a | trace.append("g2 received %s" % (x,)) |
---|
157 | n/a | x = yield "g2 more spam" |
---|
158 | n/a | trace.append("g2 received %s" % (x,)) |
---|
159 | n/a | trace.append("Finishing g2") |
---|
160 | n/a | g = g1() |
---|
161 | n/a | y = next(g) |
---|
162 | n/a | x = 1 |
---|
163 | n/a | try: |
---|
164 | n/a | while 1: |
---|
165 | n/a | y = g.send(x) |
---|
166 | n/a | trace.append("Yielded %s" % (y,)) |
---|
167 | n/a | x += 1 |
---|
168 | n/a | except StopIteration: |
---|
169 | n/a | pass |
---|
170 | n/a | self.assertEqual(trace,[ |
---|
171 | n/a | "Starting g1", |
---|
172 | n/a | "g1 received 1", |
---|
173 | n/a | "Starting g2", |
---|
174 | n/a | "Yielded g2 spam", |
---|
175 | n/a | "g2 received 2", |
---|
176 | n/a | "Yielded g2 more spam", |
---|
177 | n/a | "g2 received 3", |
---|
178 | n/a | "Finishing g2", |
---|
179 | n/a | "Yielded g1 eggs", |
---|
180 | n/a | "g1 received 4", |
---|
181 | n/a | "Finishing g1", |
---|
182 | n/a | ]) |
---|
183 | n/a | |
---|
184 | n/a | def test_handling_exception_while_delegating_send(self): |
---|
185 | n/a | """ |
---|
186 | n/a | Test handling exception while delegating 'send' |
---|
187 | n/a | """ |
---|
188 | n/a | trace = [] |
---|
189 | n/a | def g1(): |
---|
190 | n/a | trace.append("Starting g1") |
---|
191 | n/a | x = yield "g1 ham" |
---|
192 | n/a | trace.append("g1 received %s" % (x,)) |
---|
193 | n/a | yield from g2() |
---|
194 | n/a | x = yield "g1 eggs" |
---|
195 | n/a | trace.append("g1 received %s" % (x,)) |
---|
196 | n/a | trace.append("Finishing g1") |
---|
197 | n/a | def g2(): |
---|
198 | n/a | trace.append("Starting g2") |
---|
199 | n/a | x = yield "g2 spam" |
---|
200 | n/a | trace.append("g2 received %s" % (x,)) |
---|
201 | n/a | raise ValueError("hovercraft is full of eels") |
---|
202 | n/a | x = yield "g2 more spam" |
---|
203 | n/a | trace.append("g2 received %s" % (x,)) |
---|
204 | n/a | trace.append("Finishing g2") |
---|
205 | n/a | def run(): |
---|
206 | n/a | g = g1() |
---|
207 | n/a | y = next(g) |
---|
208 | n/a | x = 1 |
---|
209 | n/a | try: |
---|
210 | n/a | while 1: |
---|
211 | n/a | y = g.send(x) |
---|
212 | n/a | trace.append("Yielded %s" % (y,)) |
---|
213 | n/a | x += 1 |
---|
214 | n/a | except StopIteration: |
---|
215 | n/a | trace.append("StopIteration") |
---|
216 | n/a | self.assertRaises(ValueError,run) |
---|
217 | n/a | self.assertEqual(trace,[ |
---|
218 | n/a | "Starting g1", |
---|
219 | n/a | "g1 received 1", |
---|
220 | n/a | "Starting g2", |
---|
221 | n/a | "Yielded g2 spam", |
---|
222 | n/a | "g2 received 2", |
---|
223 | n/a | ]) |
---|
224 | n/a | |
---|
225 | n/a | def test_delegating_close(self): |
---|
226 | n/a | """ |
---|
227 | n/a | Test delegating 'close' |
---|
228 | n/a | """ |
---|
229 | n/a | trace = [] |
---|
230 | n/a | def g1(): |
---|
231 | n/a | try: |
---|
232 | n/a | trace.append("Starting g1") |
---|
233 | n/a | yield "g1 ham" |
---|
234 | n/a | yield from g2() |
---|
235 | n/a | yield "g1 eggs" |
---|
236 | n/a | finally: |
---|
237 | n/a | trace.append("Finishing g1") |
---|
238 | n/a | def g2(): |
---|
239 | n/a | try: |
---|
240 | n/a | trace.append("Starting g2") |
---|
241 | n/a | yield "g2 spam" |
---|
242 | n/a | yield "g2 more spam" |
---|
243 | n/a | finally: |
---|
244 | n/a | trace.append("Finishing g2") |
---|
245 | n/a | g = g1() |
---|
246 | n/a | for i in range(2): |
---|
247 | n/a | x = next(g) |
---|
248 | n/a | trace.append("Yielded %s" % (x,)) |
---|
249 | n/a | g.close() |
---|
250 | n/a | self.assertEqual(trace,[ |
---|
251 | n/a | "Starting g1", |
---|
252 | n/a | "Yielded g1 ham", |
---|
253 | n/a | "Starting g2", |
---|
254 | n/a | "Yielded g2 spam", |
---|
255 | n/a | "Finishing g2", |
---|
256 | n/a | "Finishing g1" |
---|
257 | n/a | ]) |
---|
258 | n/a | |
---|
259 | n/a | def test_handing_exception_while_delegating_close(self): |
---|
260 | n/a | """ |
---|
261 | n/a | Test handling exception while delegating 'close' |
---|
262 | n/a | """ |
---|
263 | n/a | trace = [] |
---|
264 | n/a | def g1(): |
---|
265 | n/a | try: |
---|
266 | n/a | trace.append("Starting g1") |
---|
267 | n/a | yield "g1 ham" |
---|
268 | n/a | yield from g2() |
---|
269 | n/a | yield "g1 eggs" |
---|
270 | n/a | finally: |
---|
271 | n/a | trace.append("Finishing g1") |
---|
272 | n/a | def g2(): |
---|
273 | n/a | try: |
---|
274 | n/a | trace.append("Starting g2") |
---|
275 | n/a | yield "g2 spam" |
---|
276 | n/a | yield "g2 more spam" |
---|
277 | n/a | finally: |
---|
278 | n/a | trace.append("Finishing g2") |
---|
279 | n/a | raise ValueError("nybbles have exploded with delight") |
---|
280 | n/a | try: |
---|
281 | n/a | g = g1() |
---|
282 | n/a | for i in range(2): |
---|
283 | n/a | x = next(g) |
---|
284 | n/a | trace.append("Yielded %s" % (x,)) |
---|
285 | n/a | g.close() |
---|
286 | n/a | except ValueError as e: |
---|
287 | n/a | self.assertEqual(e.args[0], "nybbles have exploded with delight") |
---|
288 | n/a | self.assertIsInstance(e.__context__, GeneratorExit) |
---|
289 | n/a | else: |
---|
290 | n/a | self.fail("subgenerator failed to raise ValueError") |
---|
291 | n/a | self.assertEqual(trace,[ |
---|
292 | n/a | "Starting g1", |
---|
293 | n/a | "Yielded g1 ham", |
---|
294 | n/a | "Starting g2", |
---|
295 | n/a | "Yielded g2 spam", |
---|
296 | n/a | "Finishing g2", |
---|
297 | n/a | "Finishing g1", |
---|
298 | n/a | ]) |
---|
299 | n/a | |
---|
300 | n/a | def test_delegating_throw(self): |
---|
301 | n/a | """ |
---|
302 | n/a | Test delegating 'throw' |
---|
303 | n/a | """ |
---|
304 | n/a | trace = [] |
---|
305 | n/a | def g1(): |
---|
306 | n/a | try: |
---|
307 | n/a | trace.append("Starting g1") |
---|
308 | n/a | yield "g1 ham" |
---|
309 | n/a | yield from g2() |
---|
310 | n/a | yield "g1 eggs" |
---|
311 | n/a | finally: |
---|
312 | n/a | trace.append("Finishing g1") |
---|
313 | n/a | def g2(): |
---|
314 | n/a | try: |
---|
315 | n/a | trace.append("Starting g2") |
---|
316 | n/a | yield "g2 spam" |
---|
317 | n/a | yield "g2 more spam" |
---|
318 | n/a | finally: |
---|
319 | n/a | trace.append("Finishing g2") |
---|
320 | n/a | try: |
---|
321 | n/a | g = g1() |
---|
322 | n/a | for i in range(2): |
---|
323 | n/a | x = next(g) |
---|
324 | n/a | trace.append("Yielded %s" % (x,)) |
---|
325 | n/a | e = ValueError("tomato ejected") |
---|
326 | n/a | g.throw(e) |
---|
327 | n/a | except ValueError as e: |
---|
328 | n/a | self.assertEqual(e.args[0], "tomato ejected") |
---|
329 | n/a | else: |
---|
330 | n/a | self.fail("subgenerator failed to raise ValueError") |
---|
331 | n/a | self.assertEqual(trace,[ |
---|
332 | n/a | "Starting g1", |
---|
333 | n/a | "Yielded g1 ham", |
---|
334 | n/a | "Starting g2", |
---|
335 | n/a | "Yielded g2 spam", |
---|
336 | n/a | "Finishing g2", |
---|
337 | n/a | "Finishing g1", |
---|
338 | n/a | ]) |
---|
339 | n/a | |
---|
340 | n/a | def test_value_attribute_of_StopIteration_exception(self): |
---|
341 | n/a | """ |
---|
342 | n/a | Test 'value' attribute of StopIteration exception |
---|
343 | n/a | """ |
---|
344 | n/a | trace = [] |
---|
345 | n/a | def pex(e): |
---|
346 | n/a | trace.append("%s: %s" % (e.__class__.__name__, e)) |
---|
347 | n/a | trace.append("value = %s" % (e.value,)) |
---|
348 | n/a | e = StopIteration() |
---|
349 | n/a | pex(e) |
---|
350 | n/a | e = StopIteration("spam") |
---|
351 | n/a | pex(e) |
---|
352 | n/a | e.value = "eggs" |
---|
353 | n/a | pex(e) |
---|
354 | n/a | self.assertEqual(trace,[ |
---|
355 | n/a | "StopIteration: ", |
---|
356 | n/a | "value = None", |
---|
357 | n/a | "StopIteration: spam", |
---|
358 | n/a | "value = spam", |
---|
359 | n/a | "StopIteration: spam", |
---|
360 | n/a | "value = eggs", |
---|
361 | n/a | ]) |
---|
362 | n/a | |
---|
363 | n/a | |
---|
364 | n/a | def test_exception_value_crash(self): |
---|
365 | n/a | # There used to be a refcount error when the return value |
---|
366 | n/a | # stored in the StopIteration has a refcount of 1. |
---|
367 | n/a | def g1(): |
---|
368 | n/a | yield from g2() |
---|
369 | n/a | def g2(): |
---|
370 | n/a | yield "g2" |
---|
371 | n/a | return [42] |
---|
372 | n/a | self.assertEqual(list(g1()), ["g2"]) |
---|
373 | n/a | |
---|
374 | n/a | |
---|
375 | n/a | def test_generator_return_value(self): |
---|
376 | n/a | """ |
---|
377 | n/a | Test generator return value |
---|
378 | n/a | """ |
---|
379 | n/a | trace = [] |
---|
380 | n/a | def g1(): |
---|
381 | n/a | trace.append("Starting g1") |
---|
382 | n/a | yield "g1 ham" |
---|
383 | n/a | ret = yield from g2() |
---|
384 | n/a | trace.append("g2 returned %r" % (ret,)) |
---|
385 | n/a | for v in 1, (2,), StopIteration(3): |
---|
386 | n/a | ret = yield from g2(v) |
---|
387 | n/a | trace.append("g2 returned %r" % (ret,)) |
---|
388 | n/a | yield "g1 eggs" |
---|
389 | n/a | trace.append("Finishing g1") |
---|
390 | n/a | def g2(v = None): |
---|
391 | n/a | trace.append("Starting g2") |
---|
392 | n/a | yield "g2 spam" |
---|
393 | n/a | yield "g2 more spam" |
---|
394 | n/a | trace.append("Finishing g2") |
---|
395 | n/a | if v: |
---|
396 | n/a | return v |
---|
397 | n/a | for x in g1(): |
---|
398 | n/a | trace.append("Yielded %s" % (x,)) |
---|
399 | n/a | self.assertEqual(trace,[ |
---|
400 | n/a | "Starting g1", |
---|
401 | n/a | "Yielded g1 ham", |
---|
402 | n/a | "Starting g2", |
---|
403 | n/a | "Yielded g2 spam", |
---|
404 | n/a | "Yielded g2 more spam", |
---|
405 | n/a | "Finishing g2", |
---|
406 | n/a | "g2 returned None", |
---|
407 | n/a | "Starting g2", |
---|
408 | n/a | "Yielded g2 spam", |
---|
409 | n/a | "Yielded g2 more spam", |
---|
410 | n/a | "Finishing g2", |
---|
411 | n/a | "g2 returned 1", |
---|
412 | n/a | "Starting g2", |
---|
413 | n/a | "Yielded g2 spam", |
---|
414 | n/a | "Yielded g2 more spam", |
---|
415 | n/a | "Finishing g2", |
---|
416 | n/a | "g2 returned (2,)", |
---|
417 | n/a | "Starting g2", |
---|
418 | n/a | "Yielded g2 spam", |
---|
419 | n/a | "Yielded g2 more spam", |
---|
420 | n/a | "Finishing g2", |
---|
421 | n/a | "g2 returned StopIteration(3,)", |
---|
422 | n/a | "Yielded g1 eggs", |
---|
423 | n/a | "Finishing g1", |
---|
424 | n/a | ]) |
---|
425 | n/a | |
---|
426 | n/a | def test_delegation_of_next_to_non_generator(self): |
---|
427 | n/a | """ |
---|
428 | n/a | Test delegation of next() to non-generator |
---|
429 | n/a | """ |
---|
430 | n/a | trace = [] |
---|
431 | n/a | def g(): |
---|
432 | n/a | yield from range(3) |
---|
433 | n/a | for x in g(): |
---|
434 | n/a | trace.append("Yielded %s" % (x,)) |
---|
435 | n/a | self.assertEqual(trace,[ |
---|
436 | n/a | "Yielded 0", |
---|
437 | n/a | "Yielded 1", |
---|
438 | n/a | "Yielded 2", |
---|
439 | n/a | ]) |
---|
440 | n/a | |
---|
441 | n/a | |
---|
442 | n/a | def test_conversion_of_sendNone_to_next(self): |
---|
443 | n/a | """ |
---|
444 | n/a | Test conversion of send(None) to next() |
---|
445 | n/a | """ |
---|
446 | n/a | trace = [] |
---|
447 | n/a | def g(): |
---|
448 | n/a | yield from range(3) |
---|
449 | n/a | gi = g() |
---|
450 | n/a | for x in range(3): |
---|
451 | n/a | y = gi.send(None) |
---|
452 | n/a | trace.append("Yielded: %s" % (y,)) |
---|
453 | n/a | self.assertEqual(trace,[ |
---|
454 | n/a | "Yielded: 0", |
---|
455 | n/a | "Yielded: 1", |
---|
456 | n/a | "Yielded: 2", |
---|
457 | n/a | ]) |
---|
458 | n/a | |
---|
459 | n/a | def test_delegation_of_close_to_non_generator(self): |
---|
460 | n/a | """ |
---|
461 | n/a | Test delegation of close() to non-generator |
---|
462 | n/a | """ |
---|
463 | n/a | trace = [] |
---|
464 | n/a | def g(): |
---|
465 | n/a | try: |
---|
466 | n/a | trace.append("starting g") |
---|
467 | n/a | yield from range(3) |
---|
468 | n/a | trace.append("g should not be here") |
---|
469 | n/a | finally: |
---|
470 | n/a | trace.append("finishing g") |
---|
471 | n/a | gi = g() |
---|
472 | n/a | next(gi) |
---|
473 | n/a | with captured_stderr() as output: |
---|
474 | n/a | gi.close() |
---|
475 | n/a | self.assertEqual(output.getvalue(), '') |
---|
476 | n/a | self.assertEqual(trace,[ |
---|
477 | n/a | "starting g", |
---|
478 | n/a | "finishing g", |
---|
479 | n/a | ]) |
---|
480 | n/a | |
---|
481 | n/a | def test_delegating_throw_to_non_generator(self): |
---|
482 | n/a | """ |
---|
483 | n/a | Test delegating 'throw' to non-generator |
---|
484 | n/a | """ |
---|
485 | n/a | trace = [] |
---|
486 | n/a | def g(): |
---|
487 | n/a | try: |
---|
488 | n/a | trace.append("Starting g") |
---|
489 | n/a | yield from range(10) |
---|
490 | n/a | finally: |
---|
491 | n/a | trace.append("Finishing g") |
---|
492 | n/a | try: |
---|
493 | n/a | gi = g() |
---|
494 | n/a | for i in range(5): |
---|
495 | n/a | x = next(gi) |
---|
496 | n/a | trace.append("Yielded %s" % (x,)) |
---|
497 | n/a | e = ValueError("tomato ejected") |
---|
498 | n/a | gi.throw(e) |
---|
499 | n/a | except ValueError as e: |
---|
500 | n/a | self.assertEqual(e.args[0],"tomato ejected") |
---|
501 | n/a | else: |
---|
502 | n/a | self.fail("subgenerator failed to raise ValueError") |
---|
503 | n/a | self.assertEqual(trace,[ |
---|
504 | n/a | "Starting g", |
---|
505 | n/a | "Yielded 0", |
---|
506 | n/a | "Yielded 1", |
---|
507 | n/a | "Yielded 2", |
---|
508 | n/a | "Yielded 3", |
---|
509 | n/a | "Yielded 4", |
---|
510 | n/a | "Finishing g", |
---|
511 | n/a | ]) |
---|
512 | n/a | |
---|
513 | n/a | def test_attempting_to_send_to_non_generator(self): |
---|
514 | n/a | """ |
---|
515 | n/a | Test attempting to send to non-generator |
---|
516 | n/a | """ |
---|
517 | n/a | trace = [] |
---|
518 | n/a | def g(): |
---|
519 | n/a | try: |
---|
520 | n/a | trace.append("starting g") |
---|
521 | n/a | yield from range(3) |
---|
522 | n/a | trace.append("g should not be here") |
---|
523 | n/a | finally: |
---|
524 | n/a | trace.append("finishing g") |
---|
525 | n/a | try: |
---|
526 | n/a | gi = g() |
---|
527 | n/a | next(gi) |
---|
528 | n/a | for x in range(3): |
---|
529 | n/a | y = gi.send(42) |
---|
530 | n/a | trace.append("Should not have yielded: %s" % (y,)) |
---|
531 | n/a | except AttributeError as e: |
---|
532 | n/a | self.assertIn("send", e.args[0]) |
---|
533 | n/a | else: |
---|
534 | n/a | self.fail("was able to send into non-generator") |
---|
535 | n/a | self.assertEqual(trace,[ |
---|
536 | n/a | "starting g", |
---|
537 | n/a | "finishing g", |
---|
538 | n/a | ]) |
---|
539 | n/a | |
---|
540 | n/a | def test_broken_getattr_handling(self): |
---|
541 | n/a | """ |
---|
542 | n/a | Test subiterator with a broken getattr implementation |
---|
543 | n/a | """ |
---|
544 | n/a | class Broken: |
---|
545 | n/a | def __iter__(self): |
---|
546 | n/a | return self |
---|
547 | n/a | def __next__(self): |
---|
548 | n/a | return 1 |
---|
549 | n/a | def __getattr__(self, attr): |
---|
550 | n/a | 1/0 |
---|
551 | n/a | |
---|
552 | n/a | def g(): |
---|
553 | n/a | yield from Broken() |
---|
554 | n/a | |
---|
555 | n/a | with self.assertRaises(ZeroDivisionError): |
---|
556 | n/a | gi = g() |
---|
557 | n/a | self.assertEqual(next(gi), 1) |
---|
558 | n/a | gi.send(1) |
---|
559 | n/a | |
---|
560 | n/a | with self.assertRaises(ZeroDivisionError): |
---|
561 | n/a | gi = g() |
---|
562 | n/a | self.assertEqual(next(gi), 1) |
---|
563 | n/a | gi.throw(AttributeError) |
---|
564 | n/a | |
---|
565 | n/a | with captured_stderr() as output: |
---|
566 | n/a | gi = g() |
---|
567 | n/a | self.assertEqual(next(gi), 1) |
---|
568 | n/a | gi.close() |
---|
569 | n/a | self.assertIn('ZeroDivisionError', output.getvalue()) |
---|
570 | n/a | |
---|
571 | n/a | def test_exception_in_initial_next_call(self): |
---|
572 | n/a | """ |
---|
573 | n/a | Test exception in initial next() call |
---|
574 | n/a | """ |
---|
575 | n/a | trace = [] |
---|
576 | n/a | def g1(): |
---|
577 | n/a | trace.append("g1 about to yield from g2") |
---|
578 | n/a | yield from g2() |
---|
579 | n/a | trace.append("g1 should not be here") |
---|
580 | n/a | def g2(): |
---|
581 | n/a | yield 1/0 |
---|
582 | n/a | def run(): |
---|
583 | n/a | gi = g1() |
---|
584 | n/a | next(gi) |
---|
585 | n/a | self.assertRaises(ZeroDivisionError,run) |
---|
586 | n/a | self.assertEqual(trace,[ |
---|
587 | n/a | "g1 about to yield from g2" |
---|
588 | n/a | ]) |
---|
589 | n/a | |
---|
590 | n/a | def test_attempted_yield_from_loop(self): |
---|
591 | n/a | """ |
---|
592 | n/a | Test attempted yield-from loop |
---|
593 | n/a | """ |
---|
594 | n/a | trace = [] |
---|
595 | n/a | def g1(): |
---|
596 | n/a | trace.append("g1: starting") |
---|
597 | n/a | yield "y1" |
---|
598 | n/a | trace.append("g1: about to yield from g2") |
---|
599 | n/a | yield from g2() |
---|
600 | n/a | trace.append("g1 should not be here") |
---|
601 | n/a | |
---|
602 | n/a | def g2(): |
---|
603 | n/a | trace.append("g2: starting") |
---|
604 | n/a | yield "y2" |
---|
605 | n/a | trace.append("g2: about to yield from g1") |
---|
606 | n/a | yield from gi |
---|
607 | n/a | trace.append("g2 should not be here") |
---|
608 | n/a | try: |
---|
609 | n/a | gi = g1() |
---|
610 | n/a | for y in gi: |
---|
611 | n/a | trace.append("Yielded: %s" % (y,)) |
---|
612 | n/a | except ValueError as e: |
---|
613 | n/a | self.assertEqual(e.args[0],"generator already executing") |
---|
614 | n/a | else: |
---|
615 | n/a | self.fail("subgenerator didn't raise ValueError") |
---|
616 | n/a | self.assertEqual(trace,[ |
---|
617 | n/a | "g1: starting", |
---|
618 | n/a | "Yielded: y1", |
---|
619 | n/a | "g1: about to yield from g2", |
---|
620 | n/a | "g2: starting", |
---|
621 | n/a | "Yielded: y2", |
---|
622 | n/a | "g2: about to yield from g1", |
---|
623 | n/a | ]) |
---|
624 | n/a | |
---|
625 | n/a | def test_returning_value_from_delegated_throw(self): |
---|
626 | n/a | """ |
---|
627 | n/a | Test returning value from delegated 'throw' |
---|
628 | n/a | """ |
---|
629 | n/a | trace = [] |
---|
630 | n/a | def g1(): |
---|
631 | n/a | try: |
---|
632 | n/a | trace.append("Starting g1") |
---|
633 | n/a | yield "g1 ham" |
---|
634 | n/a | yield from g2() |
---|
635 | n/a | yield "g1 eggs" |
---|
636 | n/a | finally: |
---|
637 | n/a | trace.append("Finishing g1") |
---|
638 | n/a | def g2(): |
---|
639 | n/a | try: |
---|
640 | n/a | trace.append("Starting g2") |
---|
641 | n/a | yield "g2 spam" |
---|
642 | n/a | yield "g2 more spam" |
---|
643 | n/a | except LunchError: |
---|
644 | n/a | trace.append("Caught LunchError in g2") |
---|
645 | n/a | yield "g2 lunch saved" |
---|
646 | n/a | yield "g2 yet more spam" |
---|
647 | n/a | class LunchError(Exception): |
---|
648 | n/a | pass |
---|
649 | n/a | g = g1() |
---|
650 | n/a | for i in range(2): |
---|
651 | n/a | x = next(g) |
---|
652 | n/a | trace.append("Yielded %s" % (x,)) |
---|
653 | n/a | e = LunchError("tomato ejected") |
---|
654 | n/a | g.throw(e) |
---|
655 | n/a | for x in g: |
---|
656 | n/a | trace.append("Yielded %s" % (x,)) |
---|
657 | n/a | self.assertEqual(trace,[ |
---|
658 | n/a | "Starting g1", |
---|
659 | n/a | "Yielded g1 ham", |
---|
660 | n/a | "Starting g2", |
---|
661 | n/a | "Yielded g2 spam", |
---|
662 | n/a | "Caught LunchError in g2", |
---|
663 | n/a | "Yielded g2 yet more spam", |
---|
664 | n/a | "Yielded g1 eggs", |
---|
665 | n/a | "Finishing g1", |
---|
666 | n/a | ]) |
---|
667 | n/a | |
---|
668 | n/a | def test_next_and_return_with_value(self): |
---|
669 | n/a | """ |
---|
670 | n/a | Test next and return with value |
---|
671 | n/a | """ |
---|
672 | n/a | trace = [] |
---|
673 | n/a | def f(r): |
---|
674 | n/a | gi = g(r) |
---|
675 | n/a | next(gi) |
---|
676 | n/a | try: |
---|
677 | n/a | trace.append("f resuming g") |
---|
678 | n/a | next(gi) |
---|
679 | n/a | trace.append("f SHOULD NOT BE HERE") |
---|
680 | n/a | except StopIteration as e: |
---|
681 | n/a | trace.append("f caught %r" % (e,)) |
---|
682 | n/a | def g(r): |
---|
683 | n/a | trace.append("g starting") |
---|
684 | n/a | yield |
---|
685 | n/a | trace.append("g returning %r" % (r,)) |
---|
686 | n/a | return r |
---|
687 | n/a | f(None) |
---|
688 | n/a | f(1) |
---|
689 | n/a | f((2,)) |
---|
690 | n/a | f(StopIteration(3)) |
---|
691 | n/a | self.assertEqual(trace,[ |
---|
692 | n/a | "g starting", |
---|
693 | n/a | "f resuming g", |
---|
694 | n/a | "g returning None", |
---|
695 | n/a | "f caught StopIteration()", |
---|
696 | n/a | "g starting", |
---|
697 | n/a | "f resuming g", |
---|
698 | n/a | "g returning 1", |
---|
699 | n/a | "f caught StopIteration(1,)", |
---|
700 | n/a | "g starting", |
---|
701 | n/a | "f resuming g", |
---|
702 | n/a | "g returning (2,)", |
---|
703 | n/a | "f caught StopIteration((2,),)", |
---|
704 | n/a | "g starting", |
---|
705 | n/a | "f resuming g", |
---|
706 | n/a | "g returning StopIteration(3,)", |
---|
707 | n/a | "f caught StopIteration(StopIteration(3,),)", |
---|
708 | n/a | ]) |
---|
709 | n/a | |
---|
710 | n/a | def test_send_and_return_with_value(self): |
---|
711 | n/a | """ |
---|
712 | n/a | Test send and return with value |
---|
713 | n/a | """ |
---|
714 | n/a | trace = [] |
---|
715 | n/a | def f(r): |
---|
716 | n/a | gi = g(r) |
---|
717 | n/a | next(gi) |
---|
718 | n/a | try: |
---|
719 | n/a | trace.append("f sending spam to g") |
---|
720 | n/a | gi.send("spam") |
---|
721 | n/a | trace.append("f SHOULD NOT BE HERE") |
---|
722 | n/a | except StopIteration as e: |
---|
723 | n/a | trace.append("f caught %r" % (e,)) |
---|
724 | n/a | def g(r): |
---|
725 | n/a | trace.append("g starting") |
---|
726 | n/a | x = yield |
---|
727 | n/a | trace.append("g received %r" % (x,)) |
---|
728 | n/a | trace.append("g returning %r" % (r,)) |
---|
729 | n/a | return r |
---|
730 | n/a | f(None) |
---|
731 | n/a | f(1) |
---|
732 | n/a | f((2,)) |
---|
733 | n/a | f(StopIteration(3)) |
---|
734 | n/a | self.assertEqual(trace, [ |
---|
735 | n/a | "g starting", |
---|
736 | n/a | "f sending spam to g", |
---|
737 | n/a | "g received 'spam'", |
---|
738 | n/a | "g returning None", |
---|
739 | n/a | "f caught StopIteration()", |
---|
740 | n/a | "g starting", |
---|
741 | n/a | "f sending spam to g", |
---|
742 | n/a | "g received 'spam'", |
---|
743 | n/a | "g returning 1", |
---|
744 | n/a | 'f caught StopIteration(1,)', |
---|
745 | n/a | 'g starting', |
---|
746 | n/a | 'f sending spam to g', |
---|
747 | n/a | "g received 'spam'", |
---|
748 | n/a | 'g returning (2,)', |
---|
749 | n/a | 'f caught StopIteration((2,),)', |
---|
750 | n/a | 'g starting', |
---|
751 | n/a | 'f sending spam to g', |
---|
752 | n/a | "g received 'spam'", |
---|
753 | n/a | 'g returning StopIteration(3,)', |
---|
754 | n/a | 'f caught StopIteration(StopIteration(3,),)' |
---|
755 | n/a | ]) |
---|
756 | n/a | |
---|
757 | n/a | def test_catching_exception_from_subgen_and_returning(self): |
---|
758 | n/a | """ |
---|
759 | n/a | Test catching an exception thrown into a |
---|
760 | n/a | subgenerator and returning a value |
---|
761 | n/a | """ |
---|
762 | n/a | def inner(): |
---|
763 | n/a | try: |
---|
764 | n/a | yield 1 |
---|
765 | n/a | except ValueError: |
---|
766 | n/a | trace.append("inner caught ValueError") |
---|
767 | n/a | return value |
---|
768 | n/a | |
---|
769 | n/a | def outer(): |
---|
770 | n/a | v = yield from inner() |
---|
771 | n/a | trace.append("inner returned %r to outer" % (v,)) |
---|
772 | n/a | yield v |
---|
773 | n/a | |
---|
774 | n/a | for value in 2, (2,), StopIteration(2): |
---|
775 | n/a | trace = [] |
---|
776 | n/a | g = outer() |
---|
777 | n/a | trace.append(next(g)) |
---|
778 | n/a | trace.append(repr(g.throw(ValueError))) |
---|
779 | n/a | self.assertEqual(trace, [ |
---|
780 | n/a | 1, |
---|
781 | n/a | "inner caught ValueError", |
---|
782 | n/a | "inner returned %r to outer" % (value,), |
---|
783 | n/a | repr(value), |
---|
784 | n/a | ]) |
---|
785 | n/a | |
---|
786 | n/a | def test_throwing_GeneratorExit_into_subgen_that_returns(self): |
---|
787 | n/a | """ |
---|
788 | n/a | Test throwing GeneratorExit into a subgenerator that |
---|
789 | n/a | catches it and returns normally. |
---|
790 | n/a | """ |
---|
791 | n/a | trace = [] |
---|
792 | n/a | def f(): |
---|
793 | n/a | try: |
---|
794 | n/a | trace.append("Enter f") |
---|
795 | n/a | yield |
---|
796 | n/a | trace.append("Exit f") |
---|
797 | n/a | except GeneratorExit: |
---|
798 | n/a | return |
---|
799 | n/a | def g(): |
---|
800 | n/a | trace.append("Enter g") |
---|
801 | n/a | yield from f() |
---|
802 | n/a | trace.append("Exit g") |
---|
803 | n/a | try: |
---|
804 | n/a | gi = g() |
---|
805 | n/a | next(gi) |
---|
806 | n/a | gi.throw(GeneratorExit) |
---|
807 | n/a | except GeneratorExit: |
---|
808 | n/a | pass |
---|
809 | n/a | else: |
---|
810 | n/a | self.fail("subgenerator failed to raise GeneratorExit") |
---|
811 | n/a | self.assertEqual(trace,[ |
---|
812 | n/a | "Enter g", |
---|
813 | n/a | "Enter f", |
---|
814 | n/a | ]) |
---|
815 | n/a | |
---|
816 | n/a | def test_throwing_GeneratorExit_into_subgenerator_that_yields(self): |
---|
817 | n/a | """ |
---|
818 | n/a | Test throwing GeneratorExit into a subgenerator that |
---|
819 | n/a | catches it and yields. |
---|
820 | n/a | """ |
---|
821 | n/a | trace = [] |
---|
822 | n/a | def f(): |
---|
823 | n/a | try: |
---|
824 | n/a | trace.append("Enter f") |
---|
825 | n/a | yield |
---|
826 | n/a | trace.append("Exit f") |
---|
827 | n/a | except GeneratorExit: |
---|
828 | n/a | yield |
---|
829 | n/a | def g(): |
---|
830 | n/a | trace.append("Enter g") |
---|
831 | n/a | yield from f() |
---|
832 | n/a | trace.append("Exit g") |
---|
833 | n/a | try: |
---|
834 | n/a | gi = g() |
---|
835 | n/a | next(gi) |
---|
836 | n/a | gi.throw(GeneratorExit) |
---|
837 | n/a | except RuntimeError as e: |
---|
838 | n/a | self.assertEqual(e.args[0], "generator ignored GeneratorExit") |
---|
839 | n/a | else: |
---|
840 | n/a | self.fail("subgenerator failed to raise GeneratorExit") |
---|
841 | n/a | self.assertEqual(trace,[ |
---|
842 | n/a | "Enter g", |
---|
843 | n/a | "Enter f", |
---|
844 | n/a | ]) |
---|
845 | n/a | |
---|
846 | n/a | def test_throwing_GeneratorExit_into_subgen_that_raises(self): |
---|
847 | n/a | """ |
---|
848 | n/a | Test throwing GeneratorExit into a subgenerator that |
---|
849 | n/a | catches it and raises a different exception. |
---|
850 | n/a | """ |
---|
851 | n/a | trace = [] |
---|
852 | n/a | def f(): |
---|
853 | n/a | try: |
---|
854 | n/a | trace.append("Enter f") |
---|
855 | n/a | yield |
---|
856 | n/a | trace.append("Exit f") |
---|
857 | n/a | except GeneratorExit: |
---|
858 | n/a | raise ValueError("Vorpal bunny encountered") |
---|
859 | n/a | def g(): |
---|
860 | n/a | trace.append("Enter g") |
---|
861 | n/a | yield from f() |
---|
862 | n/a | trace.append("Exit g") |
---|
863 | n/a | try: |
---|
864 | n/a | gi = g() |
---|
865 | n/a | next(gi) |
---|
866 | n/a | gi.throw(GeneratorExit) |
---|
867 | n/a | except ValueError as e: |
---|
868 | n/a | self.assertEqual(e.args[0], "Vorpal bunny encountered") |
---|
869 | n/a | self.assertIsInstance(e.__context__, GeneratorExit) |
---|
870 | n/a | else: |
---|
871 | n/a | self.fail("subgenerator failed to raise ValueError") |
---|
872 | n/a | self.assertEqual(trace,[ |
---|
873 | n/a | "Enter g", |
---|
874 | n/a | "Enter f", |
---|
875 | n/a | ]) |
---|
876 | n/a | |
---|
877 | n/a | def test_yield_from_empty(self): |
---|
878 | n/a | def g(): |
---|
879 | n/a | yield from () |
---|
880 | n/a | self.assertRaises(StopIteration, next, g()) |
---|
881 | n/a | |
---|
882 | n/a | def test_delegating_generators_claim_to_be_running(self): |
---|
883 | n/a | # Check with basic iteration |
---|
884 | n/a | def one(): |
---|
885 | n/a | yield 0 |
---|
886 | n/a | yield from two() |
---|
887 | n/a | yield 3 |
---|
888 | n/a | def two(): |
---|
889 | n/a | yield 1 |
---|
890 | n/a | try: |
---|
891 | n/a | yield from g1 |
---|
892 | n/a | except ValueError: |
---|
893 | n/a | pass |
---|
894 | n/a | yield 2 |
---|
895 | n/a | g1 = one() |
---|
896 | n/a | self.assertEqual(list(g1), [0, 1, 2, 3]) |
---|
897 | n/a | # Check with send |
---|
898 | n/a | g1 = one() |
---|
899 | n/a | res = [next(g1)] |
---|
900 | n/a | try: |
---|
901 | n/a | while True: |
---|
902 | n/a | res.append(g1.send(42)) |
---|
903 | n/a | except StopIteration: |
---|
904 | n/a | pass |
---|
905 | n/a | self.assertEqual(res, [0, 1, 2, 3]) |
---|
906 | n/a | # Check with throw |
---|
907 | n/a | class MyErr(Exception): |
---|
908 | n/a | pass |
---|
909 | n/a | def one(): |
---|
910 | n/a | try: |
---|
911 | n/a | yield 0 |
---|
912 | n/a | except MyErr: |
---|
913 | n/a | pass |
---|
914 | n/a | yield from two() |
---|
915 | n/a | try: |
---|
916 | n/a | yield 3 |
---|
917 | n/a | except MyErr: |
---|
918 | n/a | pass |
---|
919 | n/a | def two(): |
---|
920 | n/a | try: |
---|
921 | n/a | yield 1 |
---|
922 | n/a | except MyErr: |
---|
923 | n/a | pass |
---|
924 | n/a | try: |
---|
925 | n/a | yield from g1 |
---|
926 | n/a | except ValueError: |
---|
927 | n/a | pass |
---|
928 | n/a | try: |
---|
929 | n/a | yield 2 |
---|
930 | n/a | except MyErr: |
---|
931 | n/a | pass |
---|
932 | n/a | g1 = one() |
---|
933 | n/a | res = [next(g1)] |
---|
934 | n/a | try: |
---|
935 | n/a | while True: |
---|
936 | n/a | res.append(g1.throw(MyErr)) |
---|
937 | n/a | except StopIteration: |
---|
938 | n/a | pass |
---|
939 | n/a | # Check with close |
---|
940 | n/a | class MyIt(object): |
---|
941 | n/a | def __iter__(self): |
---|
942 | n/a | return self |
---|
943 | n/a | def __next__(self): |
---|
944 | n/a | return 42 |
---|
945 | n/a | def close(self_): |
---|
946 | n/a | self.assertTrue(g1.gi_running) |
---|
947 | n/a | self.assertRaises(ValueError, next, g1) |
---|
948 | n/a | def one(): |
---|
949 | n/a | yield from MyIt() |
---|
950 | n/a | g1 = one() |
---|
951 | n/a | next(g1) |
---|
952 | n/a | g1.close() |
---|
953 | n/a | |
---|
954 | n/a | def test_delegator_is_visible_to_debugger(self): |
---|
955 | n/a | def call_stack(): |
---|
956 | n/a | return [f[3] for f in inspect.stack()] |
---|
957 | n/a | |
---|
958 | n/a | def gen(): |
---|
959 | n/a | yield call_stack() |
---|
960 | n/a | yield call_stack() |
---|
961 | n/a | yield call_stack() |
---|
962 | n/a | |
---|
963 | n/a | def spam(g): |
---|
964 | n/a | yield from g |
---|
965 | n/a | |
---|
966 | n/a | def eggs(g): |
---|
967 | n/a | yield from g |
---|
968 | n/a | |
---|
969 | n/a | for stack in spam(gen()): |
---|
970 | n/a | self.assertTrue('spam' in stack) |
---|
971 | n/a | |
---|
972 | n/a | for stack in spam(eggs(gen())): |
---|
973 | n/a | self.assertTrue('spam' in stack and 'eggs' in stack) |
---|
974 | n/a | |
---|
975 | n/a | def test_custom_iterator_return(self): |
---|
976 | n/a | # See issue #15568 |
---|
977 | n/a | class MyIter: |
---|
978 | n/a | def __iter__(self): |
---|
979 | n/a | return self |
---|
980 | n/a | def __next__(self): |
---|
981 | n/a | raise StopIteration(42) |
---|
982 | n/a | def gen(): |
---|
983 | n/a | nonlocal ret |
---|
984 | n/a | ret = yield from MyIter() |
---|
985 | n/a | ret = None |
---|
986 | n/a | list(gen()) |
---|
987 | n/a | self.assertEqual(ret, 42) |
---|
988 | n/a | |
---|
989 | n/a | def test_close_with_cleared_frame(self): |
---|
990 | n/a | # See issue #17669. |
---|
991 | n/a | # |
---|
992 | n/a | # Create a stack of generators: outer() delegating to inner() |
---|
993 | n/a | # delegating to innermost(). The key point is that the instance of |
---|
994 | n/a | # inner is created first: this ensures that its frame appears before |
---|
995 | n/a | # the instance of outer in the GC linked list. |
---|
996 | n/a | # |
---|
997 | n/a | # At the gc.collect call: |
---|
998 | n/a | # - frame_clear is called on the inner_gen frame. |
---|
999 | n/a | # - gen_dealloc is called on the outer_gen generator (the only |
---|
1000 | n/a | # reference is in the frame's locals). |
---|
1001 | n/a | # - gen_close is called on the outer_gen generator. |
---|
1002 | n/a | # - gen_close_iter is called to close the inner_gen generator, which |
---|
1003 | n/a | # in turn calls gen_close, and gen_yf. |
---|
1004 | n/a | # |
---|
1005 | n/a | # Previously, gen_yf would crash since inner_gen's frame had been |
---|
1006 | n/a | # cleared (and in particular f_stacktop was NULL). |
---|
1007 | n/a | |
---|
1008 | n/a | def innermost(): |
---|
1009 | n/a | yield |
---|
1010 | n/a | def inner(): |
---|
1011 | n/a | outer_gen = yield |
---|
1012 | n/a | yield from innermost() |
---|
1013 | n/a | def outer(): |
---|
1014 | n/a | inner_gen = yield |
---|
1015 | n/a | yield from inner_gen |
---|
1016 | n/a | |
---|
1017 | n/a | with disable_gc(): |
---|
1018 | n/a | inner_gen = inner() |
---|
1019 | n/a | outer_gen = outer() |
---|
1020 | n/a | outer_gen.send(None) |
---|
1021 | n/a | outer_gen.send(inner_gen) |
---|
1022 | n/a | outer_gen.send(outer_gen) |
---|
1023 | n/a | |
---|
1024 | n/a | del outer_gen |
---|
1025 | n/a | del inner_gen |
---|
1026 | n/a | gc_collect() |
---|
1027 | n/a | |
---|
1028 | n/a | def test_send_tuple_with_custom_generator(self): |
---|
1029 | n/a | # See issue #21209. |
---|
1030 | n/a | class MyGen: |
---|
1031 | n/a | def __iter__(self): |
---|
1032 | n/a | return self |
---|
1033 | n/a | def __next__(self): |
---|
1034 | n/a | return 42 |
---|
1035 | n/a | def send(self, what): |
---|
1036 | n/a | nonlocal v |
---|
1037 | n/a | v = what |
---|
1038 | n/a | return None |
---|
1039 | n/a | def outer(): |
---|
1040 | n/a | v = yield from MyGen() |
---|
1041 | n/a | g = outer() |
---|
1042 | n/a | next(g) |
---|
1043 | n/a | v = None |
---|
1044 | n/a | g.send((1, 2, 3, 4)) |
---|
1045 | n/a | self.assertEqual(v, (1, 2, 3, 4)) |
---|
1046 | n/a | |
---|
1047 | n/a | |
---|
1048 | n/a | if __name__ == '__main__': |
---|
1049 | n/a | unittest.main() |
---|