File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ def gen ():
2+ yield 1
3+ yield 2
4+ yield 3
5+ yield 4
6+
7+ def gen2 ():
8+ yield - 1
9+ print ((yield from gen ()))
10+ yield 10
11+ yield 11
12+
13+ g = gen2 ()
14+ print (next (g ))
15+ print (next (g ))
16+ g .close ()
17+ try :
18+ print (next (g ))
19+ except StopIteration :
20+ print ("StopIteration" )
21+
22+
23+ # Now variation of same test, but with leaf generator
24+ # swallowing GeneratorExit exception - its upstream gen
25+ # generator should still receive one.
26+ def gen3 ():
27+ yield 1
28+ try :
29+ yield 2
30+ except GeneratorExit :
31+ print ("leaf caught GeneratorExit and swallowed it" )
32+ return
33+ yield 3
34+ yield 4
35+
36+ def gen4 ():
37+ yield - 1
38+ try :
39+ print ((yield from gen3 ()))
40+ except GeneratorExit :
41+ print ("delegating caught GeneratorExit" )
42+ raise
43+ yield 10
44+ yield 11
45+
46+ g = gen4 ()
47+ print (next (g ))
48+ print (next (g ))
49+ print (next (g ))
50+ g .close ()
51+ try :
52+ print (next (g ))
53+ except StopIteration :
54+ print ("StopIteration" )
55+
56+
57+ # Yet another variation - leaf generator gets GeneratorExit,
58+ # but raises StopIteration instead. This still should close chain properly.
59+ def gen5 ():
60+ yield 1
61+ try :
62+ yield 2
63+ except GeneratorExit :
64+ print ("leaf caught GeneratorExit and raised StopIteration instead" )
65+ raise StopIteration (123 )
66+ yield 3
67+ yield 4
68+
69+ def gen6 ():
70+ yield - 1
71+ try :
72+ print ((yield from gen5 ()))
73+ except GeneratorExit :
74+ print ("delegating caught GeneratorExit" )
75+ raise
76+ yield 10
77+ yield 11
78+
79+ g = gen6 ()
80+ print (next (g ))
81+ print (next (g ))
82+ print (next (g ))
83+ g .close ()
84+ try :
85+ print (next (g ))
86+ except StopIteration :
87+ print ("StopIteration" )
Original file line number Diff line number Diff line change 1+ def gen ():
2+ yield 1
3+ yield 2
4+ raise ValueError
5+
6+ def gen2 ():
7+ try :
8+ print ((yield from gen ()))
9+ except ValueError :
10+ print ("caught ValueError from downstream" )
11+
12+ g = gen2 ()
13+ print (list (g ))
Original file line number Diff line number Diff line change 1+ def gen ():
2+ print ("sent:" , (yield 1 ))
3+ yield 2
4+
5+ def gen2 ():
6+ print ((yield from gen ()))
7+
8+ g = gen2 ()
9+ next (g )
10+ print ("yielded:" , g .send ("val" ))
11+ try :
12+ next (g )
13+ except StopIteration :
14+ print ("StopIteration" )
Original file line number Diff line number Diff line change 1+ def gen ():
2+ try :
3+ yield 1
4+ except ValueError :
5+ print ("got ValueError from upstream!" )
6+ yield "str1"
7+ raise TypeError
8+
9+ def gen2 ():
10+ print ((yield from gen ()))
11+
12+ g = gen2 ()
13+ print (next (g ))
14+ print (g .throw (ValueError ))
15+ try :
16+ print (next (g ))
17+ except TypeError :
18+ print ("got TypeError from downstream!" )
19+
Original file line number Diff line number Diff line change 1+ # Case of terminating subgen using return with value
2+ def gen ():
3+ yield 1
4+ yield 2
5+ return 3
6+
7+ def gen2 ():
8+ print ("here1" )
9+ print ((yield from gen ()))
10+ print ("here2" )
11+
12+ g = gen2 ()
13+ print (list (g ))
14+
15+
16+ # Like above, but terminate subgen using StopIteration
17+ def gen3 ():
18+ yield 1
19+ yield 2
20+ raise StopIteration
21+
22+ def gen4 ():
23+ print ("here1" )
24+ print ((yield from gen3 ()))
25+ print ("here2" )
26+
27+ g = gen4 ()
28+ print (list (g ))
29+
30+ # Like above, but terminate subgen using StopIteration with value
31+ def gen5 ():
32+ yield 1
33+ yield 2
34+ raise StopIteration (123 )
35+
36+ def gen6 ():
37+ print ("here1" )
38+ print ((yield from gen5 ()))
39+ print ("here2" )
40+
41+ g = gen6 ()
42+ print (list (g ))
You can’t perform that action at this time.
0 commit comments