Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(12460)

Side by Side Diff: Lib/test/test_pep380.py

Issue 11682: PEP 380 reference implementation for 3.3
Patch Set: Created 8 years, 2 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # -*- coding: utf-8 -*-
Benjamin Peterson 2011/07/09 18:41:14 This file is really hideous. It should be converte
Nick Coghlan 2011/09/20 13:21:02 Done.
2
3 """
4 Test suite for PEP 380 implementation
5
6 adapted from original tests written by Greg Ewing
7 see <http://www.cosc.canterbury.ac.nz/greg.ewing/python/yield-from/YieldFrom-Pyt hon3.1.2-rev5.zip>
8 """
9
10 import unittest
11 import io
12 import sys
13 import traceback
14
15 # XXX (ncoghlan): The line numbers make many of these tests quite fragile.
16 # Ideally the whole lot should be modified to use independent asserts rather
17 # than golden output comparisons
18
19 class PEP380Test(unittest.TestCase):
20 maxDiff = None
21 def setUp(self):
22 self.out = io.StringIO()
23 self.saved_stdout = sys.stdout
24 self.saved_stderr = sys.stderr
25 sys.stdout = sys.stderr = self.out
26
27 def test(self):
28 try:
29 self.case()
30 except:
31 et, ev, tb = sys.exc_info()
32 tb = tb.tb_next
33 traceback.print_exception(et, ev, tb)
34 # Trailing whitespace does not play nicely with checkin hooks
35 self.assertEqual('\n'.join(line.rstrip() for line in self.out.getvalue() .split('\n')),
36 '\n'.join(line.rstrip() for line in self.expected.split ('\n')))
37
38 def tearDown(self):
39 sys.stdout = self.saved_stdout
40 sys.stderr = self.saved_stderr
41 self.out.close()
42
43
44 class Test1(PEP380Test):
Benjamin Peterson 2011/07/09 18:41:14 What is the point of this test? Shouldn't the sema
Nick Coghlan 2011/09/20 13:21:02 Done.
45 """
46 Test grammar and code generation
47 """
48
49 expected = """\
50 ---------- g1 ----------
51 78 0 LOAD_CONST 0 (None)
52 3 YIELD_VALUE
53 4 POP_TOP
54
55 79 5 LOAD_CONST 1 (42)
56 8 YIELD_VALUE
57 9 POP_TOP
58 10 LOAD_CONST 0 (None)
59 13 RETURN_VALUE
60 ---------- g2 ----------
61 82 0 LOAD_GLOBAL 0 (x)
62 3 YIELD_FROM
63 4 POP_TOP
64 5 LOAD_CONST 0 (None)
65 8 RETURN_VALUE
66 ---------- g3 ----------
67 85 0 LOAD_FAST 0 (x)
68 3 YIELD_FROM
69 4 STORE_FAST 0 (x)
70 7 LOAD_CONST 0 (None)
71 10 RETURN_VALUE
72 """.format(__file__)
73
74 def case(self):
75 import dis
76
77 def g1():
78 yield
79 yield 42
80
81 def g2():
82 yield from x
83
84 def g3():
85 x = yield from x
86
87 def disgen(g):
88 print("---------- %s ----------" % g.__name__)
89 dis.dis(g)
90
91 disgen(g1)
92 disgen(g2)
93 disgen(g3)
94
95
96 class Test2(PEP380Test):
97 """
98 Test delegation of initial next() call to subgenerator
99 """
100
101 expected = """\
102 Starting g1
103 Starting g2
104 Yielded 42
105 Finishing g2
106 Finishing g1
107 """.format(__file__)
108
109 def case(self):
110 def g1():
111 print("Starting g1")
112 yield from g2()
113 print("Finishing g1")
114
115 def g2():
116 print("Starting g2")
117 yield 42
118 print("Finishing g2")
119
120 for x in g1():
121 print("Yielded", x)
122
123
124 class Test3(PEP380Test):
125 """
126 Test raising exception in initial next() call
127 """
128
129 expected = """\
130 Starting g1
131 Starting g2
132 Finishing g2
133 Finishing g1
134 Traceback (most recent call last):
135 File "{0}", line 159, in case
136 for x in g1():
137 File "{0}", line 148, in g1
138 yield from g2()
139 File "{0}", line 155, in g2
140 raise ValueError("spanish inquisition occurred")
141 ValueError: spanish inquisition occurred
142 """.format(__file__)
143
144 def case(self):
145 def g1():
146 try:
147 print("Starting g1")
148 yield from g2()
149 finally:
150 print("Finishing g1")
151
152 def g2():
153 try:
154 print("Starting g2")
155 raise ValueError("spanish inquisition occurred")
156 finally:
157 print("Finishing g2")
158
159 for x in g1():
160 print("Yielded", x)
161
162
163 class Test4(PEP380Test):
164 """
165 Test delegation of next() call to subgenerator
166 """
167
168 expected = """\
169 Starting g1
170 Yielded g1 ham
171 Starting g2
172 Yielded g2 spam
173 Yielded g2 more spam
174 Finishing g2
175 Yielded g1 eggs
176 Finishing g1
177 """.format(__file__)
178
179 def case(self):
180
181 def g1():
182 print("Starting g1")
183 yield "g1 ham"
184 yield from g2()
185 yield "g1 eggs"
186 print("Finishing g1")
187
188 def g2():
189 print("Starting g2")
190 yield "g2 spam"
191 yield "g2 more spam"
192 print("Finishing g2")
193
194 for x in g1():
195 print("Yielded", x)
196
197
198 class Test5(PEP380Test):
199 """
200 Test raising exception in delegated next() call
201 """
202
203 expected = """\
204 Starting g1
205 Yielded g1 ham
206 Starting g2
207 Yielded g2 spam
208 Finishing g2
209 Finishing g1
210 Traceback (most recent call last):
211 File "{0}", line 239, in case
212 for x in g1():
213 File "{0}", line 225, in g1
214 yield from g2()
215 File "{0}", line 234, in g2
216 raise ValueError("hovercraft is full of eels")
217 ValueError: hovercraft is full of eels
218 """.format(__file__)
219
220 def case(self):
221 def g1():
222 try:
223 print("Starting g1")
224 yield "g1 ham"
225 yield from g2()
226 yield "g1 eggs"
227 finally:
228 print("Finishing g1")
229
230 def g2():
231 try:
232 print("Starting g2")
233 yield "g2 spam"
234 raise ValueError("hovercraft is full of eels")
235 yield "g2 more spam"
236 finally:
237 print("Finishing g2")
238
239 for x in g1():
240 print("Yielded", x)
241
242
243 class Test6(PEP380Test):
244 """
245 Test delegation of send()
246 """
247
248 expected = """\
249 Starting g1
250 g1 received 1
251 Starting g2
252 Yielded g2 spam
253 g2 received 2
254 Yielded g2 more spam
255 g2 received 3
256 Finishing g2
257 Yielded g1 eggs
258 g1 received 4
259 Finishing g1
260 """.format(__file__)
261
262 def case(self):
263 def g1():
264 print("Starting g1")
265 x = yield "g1 ham"
266 print("g1 received", x)
267 yield from g2()
268 x = yield "g1 eggs"
269 print("g1 received", x)
270 print("Finishing g1")
271
272 def g2():
273 print("Starting g2")
274 x = yield "g2 spam"
275 print("g2 received", x)
276 x = yield "g2 more spam"
277 print("g2 received", x)
278 print("Finishing g2")
279
280 g = g1()
281 y = next(g)
282 x = 1
283 try:
284 while 1:
285 y = g.send(x)
286 print("Yielded", y)
287 x += 1
288 except StopIteration:
289 pass
290
291
292 class Test7(PEP380Test):
293 """
294 Test handling exception while delegating 'send'
295 """
296
297 expected = """\
298 Starting g1
299 g1 received 1
300 Starting g2
301 Yielded g2 spam
302 g2 received 2
303 Traceback (most recent call last):
304 File "{0}", line 337, in case
305 y = g.send(x)
306 File "{0}", line 318, in g1
307 yield from g2()
308 File "{0}", line 327, in g2
309 raise ValueError("hovercraft is full of eels")
310 ValueError: hovercraft is full of eels
311 """.format(__file__)
312
313 def case(self):
314 def g1():
315 print("Starting g1")
316 x = yield "g1 ham"
317 print("g1 received", x)
318 yield from g2()
319 x = yield "g1 eggs"
320 print("g1 received", x)
321 print("Finishing g1")
322
323 def g2():
324 print("Starting g2")
325 x = yield "g2 spam"
326 print("g2 received", x)
327 raise ValueError("hovercraft is full of eels")
328 x = yield "g2 more spam"
329 print("g2 received", x)
330 print("Finishing g2")
331
332 g = g1()
333 y = next(g)
334 x = 1
335 try:
336 while 1:
337 y = g.send(x)
338 print("Yielded", y)
339 x += 1
340 except StopIteration:
341 print("StopIteration")
342
343
344
345 class Test8(PEP380Test):
346 """
347 Test delegating 'close'
348 """
349
350 expected = """\
351 Starting g1
352 Yielded g1 ham
353 Starting g2
354 Yielded g2 spam
355 Finishing g2
356 Finishing g1
357 """.format(__file__)
358
359 def case(self):
360 def g1():
361 try:
362 print("Starting g1")
363 yield "g1 ham"
364 yield from g2()
365 yield "g1 eggs"
366 finally:
367 print("Finishing g1")
368
369 def g2():
370 try:
371 print("Starting g2")
372 yield "g2 spam"
373 yield "g2 more spam"
374 finally:
375 print("Finishing g2")
376
377 g = g1()
378 for i in range(2):
379 x = next(g)
380 print("Yielded", x)
381 g.close()
382
383
384 class Test9(PEP380Test):
385 """
386 Test handling exception while delegating 'close'
387 """
388
389 expected = """\
390 Starting g1
391 Yielded g1 ham
392 Starting g2
393 Yielded g2 spam
394 Finishing g2
395 Finishing g1
396 Traceback (most recent call last):
397 File "{0}", line 426, in g2
398 yield "g2 spam"
399 GeneratorExit
400
401 During handling of the above exception, another exception occurred:
402
403 Traceback (most recent call last):
404 File "{0}", line 436, in case
405 g.close()
406 File "{0}", line 418, in g1
407 yield from g2()
408 File "{0}", line 430, in g2
409 raise ValueError("nybbles have exploded with delight")
410 ValueError: nybbles have exploded with delight
411 """.format(__file__)
412
413 def case(self):
414 def g1():
415 try:
416 print("Starting g1")
417 yield "g1 ham"
418 yield from g2()
419 yield "g1 eggs"
420 finally:
421 print("Finishing g1")
422
423 def g2():
424 try:
425 print("Starting g2")
426 yield "g2 spam"
427 yield "g2 more spam"
428 finally:
429 print("Finishing g2")
430 raise ValueError("nybbles have exploded with delight")
431
432 g = g1()
433 for i in range(2):
434 x = next(g)
435 print("Yielded", x)
436 g.close()
437
438
439 class Test10(PEP380Test):
440 """
441 Test delegating 'throw'
442 """
443
444 expected = """\
445 Starting g1
446 Yielded g1 ham
447 Starting g2
448 Yielded g2 spam
449 Finishing g2
450 Finishing g1
451 Traceback (most recent call last):
452 File "{0}", line 484, in case
453 g.throw(e)
454 File "{0}", line 466, in g1
455 yield from g2()
456 File "{0}", line 474, in g2
457 yield "g2 spam"
458 ValueError: tomato ejected
459 """.format(__file__)
460
461 def case(self):
462 def g1():
463 try:
464 print("Starting g1")
465 yield "g1 ham"
466 yield from g2()
467 yield "g1 eggs"
468 finally:
469 print("Finishing g1")
470
471 def g2():
472 try:
473 print("Starting g2")
474 yield "g2 spam"
475 yield "g2 more spam"
476 finally:
477 print("Finishing g2")
478
479 g = g1()
480 for i in range(2):
481 x = next(g)
482 print("Yielded", x)
483 e = ValueError("tomato ejected")
484 g.throw(e)
485
486
487 class Test11(PEP380Test):
488 """
489 Test 'value' attribute of StopIteration exception
490 """
491
492 expected = """\
493 StopIteration:
494 value = None
495 StopIteration: spam
496 value = spam
497 StopIteration: spam
498 value = eggs
499 """.format(__file__)
500
501 def case(self):
502 def pex(e):
503 print("%s: %s" % (e.__class__.__name__, e))
504 print("value =", e.value)
505
506 e = StopIteration()
507 pex(e)
508 e = StopIteration("spam")
509 pex(e)
510 e.value = "eggs"
511 pex(e)
512
513
514 class Test12(PEP380Test):
515 """
516 Test generator return value
517 """
518
519 expected = """\
520 Starting g1
521 Yielded g1 ham
522 Starting g2
523 Yielded g2 spam
524 Yielded g2 more spam
525 Finishing g2
526 g2 returned None
527 Starting g2
528 Yielded g2 spam
529 Yielded g2 more spam
530 Finishing g2
531 g2 returned 42
532 Yielded g1 eggs
533 Finishing g1
534 """.format(__file__)
535
536 def case(self):
537 def g1():
538 print("Starting g1")
539 yield "g1 ham"
540 ret = yield from g2()
541 print("g2 returned", ret)
542 ret = yield from g2(42)
543 print("g2 returned", ret)
544 yield "g1 eggs"
545 print("Finishing g1")
546
547 def g2(v = None):
548 print("Starting g2")
549 yield "g2 spam"
550 yield "g2 more spam"
551 print("Finishing g2")
552 if v:
553 return v
554
555 for x in g1():
556 print("Yielded", x)
557
558
559 class Test13(PEP380Test):
560 """
561 Test delegation of next() to non-generator
562 """
563
564 expected = """\
565 Yielded 0
566 Yielded 1
567 Yielded 2
568 """.format(__file__)
569
570 def case(self):
571 def g():
572 yield from range(3)
573
574 for x in g():
575 print("Yielded", x)
576
577
578 class Test14(PEP380Test):
579 """
580 Test conversion of send(None) to next()
581 """
582
583 expected = """\
584 Yielded: 0
585 Yielded: 1
586 Yielded: 2
587 """.format(__file__)
588
589 def case(self):
590 def g():
591 yield from range(3)
592
593 gi = g()
594 for x in range(3):
595 y = gi.send(None)
596 print("Yielded:", y)
597
598
599 class Test15(PEP380Test):
600 """
601 Test delegation of close() to non-generator
602 """
603
604 expected = """\
605 starting g
606 finishing g
607 """.format(__file__)
608
609 def case(self):
610 def g():
611 try:
612 print("starting g")
613 yield from range(3)
614 print("g should not be here")
615 finally:
616 print("finishing g")
617
618 gi = g()
619 next(gi)
620 gi.close()
621
622
623 class Test16(PEP380Test):
624 """
625 Test delegating 'throw' to non-generator
626 """
627
628 expected = """\
629 Starting g
630 Yielded 0
631 Yielded 1
632 Yielded 2
633 Yielded 3
634 Yielded 4
635 Finishing g
636 Traceback (most recent call last):
637 File "{0}", line 657, in case
638 gi.throw(e)
639 File "{0}", line 648, in g
640 yield from range(10)
641 ValueError: tomato ejected
642 """.format(__file__)
643
644 def case(self):
645 def g():
646 try:
647 print("Starting g")
648 yield from range(10)
649 finally:
650 print("Finishing g")
651
652 gi = g()
653 for i in range(5):
654 x = next(gi)
655 print("Yielded", x)
656 e = ValueError("tomato ejected")
657 gi.throw(e)
658
659
660 class Test17(PEP380Test):
661 """
662 Test attempting to send to non-generator
663 """
664
665 expected = """\
666 starting g
667 finishing g
668 Traceback (most recent call last):
669 File "{0}", line 688, in case
670 y = gi.send(42)
671 File "{0}", line 680, in g
672 yield from range(3)
673 AttributeError: send
674 """.format(__file__)
675
676 def case(self):
677 def g():
678 try:
679 print("starting g")
680 yield from range(3)
681 print("g should not be here")
682 finally:
683 print("finishing g")
684
685 gi = g()
686 next(gi)
687 for x in range(3):
688 y = gi.send(42)
689 print("Should not have yielded:", y)
690
691
692 class Test18(PEP380Test):
693 """
694 Test exception in initial next() call
695 """
696
697 expected = """\
698 g1 about to yield from g2
699 Traceback (most recent call last):
700 File "{0}", line 719, in case
701 next(gi)
702 File "{0}", line 712, in g1
703 yield from g2()
704 File "{0}", line 716, in g2
705 yield 1/0
706 ZeroDivisionError: division by zero
707 """.format(__file__)
708
709 def case(self):
710 def g1():
711 print("g1 about to yield from g2")
712 yield from g2()
713 print("g1 should not be here")
714
715 def g2():
716 yield 1/0
717
718 gi = g1()
719 next(gi)
720
721
722 class Test19(PEP380Test):
723 """
724 Test attempted yield-from loop
725 """
726
727 expected = """\
728 g1: starting
729 Yielded: y1
730 g1: about to yield from g2
731 g2: starting
732 Yielded: y2
733 g2: about to yield from g1
734 Traceback (most recent call last):
735 File "{0}", line 760, in case
736 for y in gi:
737 File "{0}", line 756, in g2
738 yield from gi
739 File "{0}", line 749, in g1
740 yield from g2()
741 ValueError: generator already executing
742 """.format(__file__)
743
744 def case(self):
745 def g1():
746 print("g1: starting")
747 yield "y1"
748 print("g1: about to yield from g2")
749 yield from g2()
750 print("g1 should not be here")
751
752 def g2():
753 print("g2: starting")
754 yield "y2"
755 print("g2: about to yield from g1")
756 yield from gi
757 print("g2 should not be here")
758
759 gi = g1()
760 for y in gi:
761 print("Yielded:", y)
762
763
764 class Test20(PEP380Test):
765 """
766 Test returning value from delegated 'throw'
767 """
768
769 expected = """\
770 Starting g1
771 Yielded g1 ham
772 Starting g2
773 Yielded g2 spam
774 Caught LunchError in g2
775 Yielded g2 yet more spam
776 Yielded g1 eggs
777 Finishing g1
778 """.format(__file__)
779
780 def case(self):
781 def g1():
782 try:
783 print("Starting g1")
784 yield "g1 ham"
785 yield from g2()
786 yield "g1 eggs"
787 finally:
788 print("Finishing g1")
789
790 def g2():
791 try:
792 print("Starting g2")
793 yield "g2 spam"
794 yield "g2 more spam"
795 except LunchError:
796 print("Caught LunchError in g2")
797 yield "g2 lunch saved"
798 yield "g2 yet more spam"
799
800 class LunchError(Exception):
801 pass
802
803 g = g1()
804 for i in range(2):
805 x = next(g)
806 print("Yielded", x)
807 e = LunchError("tomato ejected")
808 g.throw(e)
809 for x in g:
810 print("Yielded", x)
811
812
813 class Test21(PEP380Test):
814 """
815 Test next and return with value
816 """
817
818 expected = """\
819 g starting
820 f resuming g
821 g returning None
822 f caught StopIteration()
823 g starting
824 f resuming g
825 g returning 42
826 f caught StopIteration(42,)
827 """.format(__file__)
828
829 def case(self):
830 def f(r):
831 gi = g(r)
832 next(gi)
833 try:
834 print("f resuming g")
835 next(gi)
836 print("f SHOULD NOT BE HERE")
837 except StopIteration as e:
838 print("f caught", repr(e))
839
840 def g(r):
841 print("g starting")
842 yield
843 print("g returning", r)
844 return r
845
846 f(None)
847 f(42)
848
849
850 class Test22(PEP380Test):
851 """
852 Test send and return with value
853 """
854
855 expected = """\
856 g starting
857 f sending spam to g
858 g received spam
859 g returning None
860 f caught StopIteration()
861 g starting
862 f sending spam to g
863 g received spam
864 g returning 42
865 f caught StopIteration(42,)
866 """.format(__file__)
867
868 def case(self):
869 def f(r):
870 gi = g(r)
871 next(gi)
872 try:
873 print("f sending spam to g")
874 gi.send("spam")
875 print("f SHOULD NOT BE HERE")
876 except StopIteration as e:
877 print("f caught", repr(e))
878
879 def g(r):
880 print("g starting")
881 x = yield
882 print("g received", x)
883 print("g returning", r)
884 return r
885
886 f(None)
887 f(42)
888
889
890 class Test23(PEP380Test):
891 """
892 Test parsing yield from as function argument
893 """
894
895 expected = """\
896 909 0 LOAD_GLOBAL 0 (f)
897 3 LOAD_GLOBAL 1 (x)
898 6 YIELD_FROM
899 7 CALL_FUNCTION 1
900 10 POP_TOP
901 11 LOAD_CONST 0 (None)
902 14 RETURN_VALUE
903 """.format(__file__)
904
905 def case(self):
906 from dis import dis
907
908 def g():
909 f(yield from x)
910
911 dis(g)
912
913
914 class Test24(PEP380Test):
Benjamin Peterson 2011/07/09 18:41:14 This ought to be in test_parser.
Nick Coghlan 2011/09/20 13:21:02 Done.
915 """
916 Test parser module
917 """
918
919 expected = """\
920 (257, (268, (269, (270, (276, (280, (336, (1, 'yield'), (337, (1, 'from'), (302, (306, (307, (308, (309, (312, (313, (314, (315, (316, (317, (318, (319, (320, ( 2, '1')))))))))))))))))))), (4, ''))), (4, ''), (0, ''))
921 True
922 (257, (268, (269, (270, (271, (272, (302, (306, (307, (308, (309, (312, (313, (3 14, (315, (316, (317, (318, (319, (320, (1, 'f')), (322, (7, '('), (330, (331, ( 338, (1, 'yield'), (1, 'from'), (302, (306, (307, (308, (309, (312, (313, (314, (315, (316, (317, (318, (319, (320, (2, '1')))))))))))))))))), (8, ')')))))))))) )))))))), (4, ''))), (4, ''), (0, ''))
923 True
924 """.format(__file__)
925
926 def case(self):
927 import parser
928
929 def test(src):
930 st1 = parser.suite(src)
931 tup1 = st1.totuple()
932 print(tup1)
933 st2 = parser.sequence2st(tup1)
934 tup2 = st2.totuple()
935 print(tup1 == tup2)
936
937 test("yield from 1")
938 test("f(yield from 1)")
939
940
941 class Test25(PEP380Test):
942 """
943 Test catching an exception thrown into a
944 subgenerator and returning a value
945 """
946
947 expected = """\
948 1
949 inner caught ValueError
950 inner returned 2 to outer
951 2
952 """.format(__file__)
953
954 def case(self):
955 def inner():
956 try:
957 yield 1
958 except ValueError:
959 print("inner caught ValueError") #pass
960 return 2
961
962 def outer():
963 v = yield from inner()
964 print("inner returned %r to outer" % v)
965 yield v
966
967 g = outer()
968 print(next(g)) # prints 1
969 print(g.throw(ValueError)) # prints 2
970
971
972 class Test26(PEP380Test):
973 """
974 Test throwing GeneratorExit into a subgenerator that
975 catches it and returns normally.
976 """
977
978 expected = """\
979 Enter g
980 Enter f
981 Traceback (most recent call last):
982 File "{0}", line 1005, in case
983 gi.throw(GeneratorExit)
984 File "{0}", line 1000, in g
985 yield from f()
986 GeneratorExit
987 """.format(__file__)
988
989 def case(self):
990 def f():
991 try:
992 print("Enter f")
993 yield
994 print("Exit f")
995 except GeneratorExit:
996 return
997
998 def g():
999 print("Enter g")
1000 yield from f()
1001 print("Exit g")
1002
1003 gi = g()
1004 next(gi)
1005 gi.throw(GeneratorExit)
1006
1007
1008 class Test27(PEP380Test):
1009 """
1010 Test throwing GeneratorExit into a subgenerator that
1011 catches it and yields.
1012 """
1013
1014 expected = """\
1015 Enter g
1016 Enter f
1017 Traceback (most recent call last):
1018 File "{0}", line 1041, in case
1019 gi.throw(GeneratorExit)
1020 File "{0}", line 1036, in g
1021 yield from f()
1022 RuntimeError: generator ignored GeneratorExit
1023 """.format(__file__)
1024
1025 def case(self):
1026 def f():
1027 try:
1028 print("Enter f")
1029 yield
1030 print("Exit f")
1031 except GeneratorExit:
1032 yield
1033
1034 def g():
1035 print("Enter g")
1036 yield from f()
1037 print("Exit g")
1038
1039 gi = g()
1040 next(gi)
1041 gi.throw(GeneratorExit)
1042
1043
1044 class Test28(PEP380Test):
1045 """
1046 Test throwing GeneratorExit into a subgenerator that
1047 catches it and raises a different exception.
1048 """
1049
1050 expected = """\
1051 Enter g
1052 Enter f
1053 Traceback (most recent call last):
1054 File "{0}", line 1074, in f
1055 yield
1056 GeneratorExit
1057
1058 During handling of the above exception, another exception occurred:
1059
1060 Traceback (most recent call last):
1061 File "{0}", line 1086, in case
1062 gi.throw(GeneratorExit)
1063 File "{0}", line 1081, in g
1064 yield from f()
1065 File "{0}", line 1077, in f
1066 raise ValueError("Vorpal bunny encountered")
1067 ValueError: Vorpal bunny encountered
1068 """.format(__file__)
1069
1070 def case(self):
1071 def f():
1072 try:
1073 print("Enter f")
1074 yield
1075 print("Exit f")
1076 except GeneratorExit:
1077 raise ValueError("Vorpal bunny encountered")
1078
1079 def g():
1080 print("Enter g")
1081 yield from f()
1082 print("Exit g")
1083
1084 gi = g()
1085 next(gi)
1086 gi.throw(GeneratorExit)
1087
1088
1089 def test_main():
1090 from test import support
1091 test_classes = [Test1, Test2, Test3, Test4, Test5, Test6, Test7, Test8, Test 9, Test10, Test11, Test12, Test13, Test14, Test15, Test16, Test17, Test18, Test1 9, Test20, Test21, Test22, Test23, Test24, Test25, Test26, Test27, Test28]
1092 support.run_unittest(*test_classes)
1093
1094
1095 if __name__ == '__main__':
1096 test_main()
OLDNEW

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+