1def gen1():
2    yield 1
3    yield 2
4
5# Test that it's possible to close just created gen
6g = gen1()
7print(g.close())
8try:
9    next(g)
10except StopIteration:
11    print("StopIteration")
12
13# Test that it's possible to close gen in progress
14g = gen1()
15print(next(g))
16print(g.close())
17try:
18    next(g)
19    print("No StopIteration")
20except StopIteration:
21    print("StopIteration")
22
23# Test that it's possible to close finished gen
24g = gen1()
25print(list(g))
26print(g.close())
27try:
28    next(g)
29    print("No StopIteration")
30except StopIteration:
31    print("StopIteration")
32
33
34# Throwing GeneratorExit in response to close() is ok
35def gen2():
36    try:
37        yield 1
38        yield 2
39    except:
40        print('raising GeneratorExit')
41        raise GeneratorExit
42
43g = gen2()
44next(g)
45print(g.close())
46
47# Any other exception is propagated to the .close() caller
48def gen3():
49    try:
50        yield 1
51        yield 2
52    except:
53        raise ValueError
54
55g = gen3()
56next(g)
57try:
58    print(g.close())
59except ValueError:
60    print("ValueError")
61