Skip to content

Commit f1ac5d3

Browse files
committed
test re-using aclose().throw(...)
1 parent da89a69 commit f1ac5d3

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

Lib/test/test_asyncgen.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,42 @@ async def run():
16711671

16721672
self.loop.run_until_complete(run())
16731673

1674+
def test_async_gen_throw_same_aclose_coro_twice(self):
1675+
async def async_iterate():
1676+
yield 1
1677+
yield 2
1678+
1679+
it = async_iterate()
1680+
nxt = it.aclose()
1681+
with self.assertRaises(StopIteration):
1682+
nxt.throw(GeneratorExit)
1683+
1684+
with self.assertRaisesRegex(
1685+
RuntimeError,
1686+
r"cannot reuse already awaited aclose\(\)/athrow\(\)"
1687+
):
1688+
nxt.throw(GeneratorExit)
1689+
1690+
def test_async_gen_throw_custom_same_aclose_coro_twice(self):
1691+
async def async_iterate():
1692+
yield 1
1693+
yield 2
1694+
1695+
it = async_iterate()
1696+
1697+
class MyException(Exception):
1698+
pass
1699+
1700+
nxt = it.aclose()
1701+
with self.assertRaises(MyException):
1702+
nxt.throw(MyException)
1703+
1704+
with self.assertRaisesRegex(
1705+
RuntimeError,
1706+
r"cannot reuse already awaited aclose\(\)/athrow\(\)"
1707+
):
1708+
nxt.throw(MyException)
1709+
16741710
def test_async_gen_aclose_twice_with_different_coros(self):
16751711
# Regression test for https://bugs.python.org/issue39606
16761712
async def async_iterate():
@@ -1762,7 +1798,7 @@ class MyException(Exception):
17621798

17631799
g = gen()
17641800
with self.assertRaises(MyException):
1765-
gen.aclose().throw(MyException)
1801+
g.aclose().throw(MyException)
17661802

17671803

17681804
if __name__ == "__main__":

0 commit comments

Comments
 (0)