Skip to content

Commit 1a2fdca

Browse files
committed
tests/basics: Split out generator.throw tests that pass multiple args.
The three-argument form of `.throw()` is deprecated since CPython 3.12. So split out into separate tests (with .exp files) the parts of the generator tests that test more than one argument. Signed-off-by: Damien George <damien@micropython.org>
1 parent 2e85252 commit 1a2fdca

6 files changed

Lines changed: 69 additions & 28 deletions

tests/basics/gen_yield_from_throw.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,6 @@ def gen2():
1717
except TypeError:
1818
print("got TypeError from downstream!")
1919

20-
# passing None as second argument to throw
21-
g = gen2()
22-
print(next(g))
23-
print(g.throw(ValueError, None))
24-
try:
25-
print(next(g))
26-
except TypeError:
27-
print("got TypeError from downstream!")
28-
29-
# passing an exception instance as second argument to throw
30-
g = gen2()
31-
print(next(g))
32-
print(g.throw(ValueError, ValueError(123)))
33-
try:
34-
print(next(g))
35-
except TypeError:
36-
print("got TypeError from downstream!")
37-
3820
# thrown value is caught and then generator returns normally
3921
def gen():
4022
try:
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Test generator .throw() with multiple arguments.
2+
# Using multiple arguments is deprecated since CPython 3.12.
3+
4+
5+
def gen():
6+
try:
7+
yield 1
8+
except ValueError as e:
9+
print("got ValueError from upstream!", repr(e.args))
10+
yield "str1"
11+
raise TypeError
12+
13+
14+
def gen2():
15+
print((yield from gen()))
16+
17+
18+
# Passing None as second argument to throw.
19+
g = gen2()
20+
print(next(g))
21+
print(g.throw(ValueError, None))
22+
try:
23+
print(next(g))
24+
except TypeError:
25+
print("got TypeError from downstream!")
26+
27+
# Passing an exception instance as second argument to throw.
28+
g = gen2()
29+
print(next(g))
30+
print(g.throw(ValueError, ValueError(123)))
31+
try:
32+
print(next(g))
33+
except TypeError:
34+
print("got TypeError from downstream!")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
1
2+
got ValueError from upstream! ()
3+
str1
4+
got TypeError from downstream!
5+
1
6+
got ValueError from upstream! (123,)
7+
str1
8+
got TypeError from downstream!

tests/basics/generator_throw.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,3 @@ def gen():
4141
g = gen()
4242
print(next(g))
4343
print(g.throw(GeneratorExit()))
44-
45-
# thrown an instance with None as second arg
46-
g = gen()
47-
print(next(g))
48-
print(g.throw(GeneratorExit(), None))
49-
50-
# thrown a class and instance
51-
g = gen()
52-
print(next(g))
53-
print(g.throw(GeneratorExit, GeneratorExit(123)))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Test generator .throw() with multiple arguments.
2+
# Using multiple arguments is deprecated since CPython 3.12.
3+
4+
# Generator ignores a thrown GeneratorExit (this is allowed).
5+
def gen():
6+
try:
7+
yield 123
8+
except GeneratorExit as e:
9+
print("GeneratorExit", repr(e.args))
10+
yield 456
11+
12+
13+
# Thrown an instance with None as second arg.
14+
g = gen()
15+
print(next(g))
16+
print(g.throw(GeneratorExit(), None))
17+
18+
# Thrown a class and instance.
19+
g = gen()
20+
print(next(g))
21+
print(g.throw(GeneratorExit, GeneratorExit(123)))
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
123
2+
GeneratorExit ()
3+
456
4+
123
5+
GeneratorExit (123,)
6+
456

0 commit comments

Comments
 (0)