gen-yield-from-close.py 1.55 KB
Newer Older
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
def gen():
    yield 1
    yield 2
    yield 3
    yield 4

def gen2():
    yield -1
    print((yield from gen()))
    yield 10
    yield 11

g = gen2()
print(next(g))
print(next(g))
g.close()
try:
    print(next(g))
except StopIteration:
    print("StopIteration")


# Now variation of same test, but with leaf generator
# swallowing GeneratorExit exception - its upstream gen
# generator should still receive one.
def gen3():
    yield 1
    try:
        yield 2
    except GeneratorExit:
        print("leaf caught GeneratorExit and swallowed it")
        return
    yield 3
    yield 4

def gen4():
    yield -1
    try:
        print((yield from gen3()))
    except GeneratorExit:
        print("delegating caught GeneratorExit")
        raise
    yield 10
    yield 11

g = gen4()
print(next(g))
print(next(g))
print(next(g))
g.close()
try:
    print(next(g))
except StopIteration:
    print("StopIteration")


# Yet another variation - leaf generator gets GeneratorExit,
# but raises StopIteration instead. This still should close chain properly.
def gen5():
    yield 1
    try:
        yield 2
    except GeneratorExit:
        print("leaf caught GeneratorExit and raised StopIteration instead")
        raise StopIteration(123)
    yield 3
    yield 4

def gen6():
    yield -1
    try:
        print((yield from gen5()))
    except GeneratorExit:
        print("delegating caught GeneratorExit")
        raise
    yield 10
    yield 11

g = gen6()
print(next(g))
print(next(g))
print(next(g))
g.close()
try:
    print(next(g))
except StopIteration:
    print("StopIteration")