@@ -399,9 +399,8 @@ async def gen():
399
399
400
400
with self .assertWarns (DeprecationWarning ):
401
401
x = gen ().athrow (GeneratorExit , GeneratorExit (), None )
402
- with self .assertWarnsRegex (RuntimeWarning ,
403
- f"coroutine method 'athrow' of '{ gen .__qualname__ } ' "
404
- f"was never awaited" ):
402
+ with self .assertRaises (GeneratorExit ):
403
+ x .send (None )
405
404
del x
406
405
gc_collect ()
407
406
@@ -1572,11 +1571,6 @@ async def main():
1572
1571
self .assertIsInstance (message ['exception' ], ZeroDivisionError )
1573
1572
self .assertIn ('unhandled exception during asyncio.run() shutdown' ,
1574
1573
message ['message' ])
1575
- with self .assertWarnsRegex (RuntimeWarning ,
1576
- f"coroutine method 'aclose' of '{ async_iterate .__qualname__ } ' "
1577
- f"was never awaited" ):
1578
- del message , messages
1579
- gc_collect ()
1580
1574
1581
1575
def test_async_gen_expression_01 (self ):
1582
1576
async def arange (n ):
@@ -1630,10 +1624,6 @@ async def main():
1630
1624
asyncio .run (main ())
1631
1625
1632
1626
self .assertEqual ([], messages )
1633
- with self .assertWarnsRegex (RuntimeWarning ,
1634
- f"coroutine method 'aclose' of '{ async_iterate .__qualname__ } ' "
1635
- f"was never awaited" ):
1636
- gc_collect ()
1637
1627
1638
1628
def test_async_gen_await_same_anext_coro_twice (self ):
1639
1629
async def async_iterate ():
@@ -1671,6 +1661,62 @@ async def run():
1671
1661
1672
1662
self .loop .run_until_complete (run ())
1673
1663
1664
+ def test_async_gen_throw_same_aclose_coro_twice (self ):
1665
+ async def async_iterate ():
1666
+ yield 1
1667
+ yield 2
1668
+
1669
+ it = async_iterate ()
1670
+ nxt = it .aclose ()
1671
+ with self .assertRaises (StopIteration ):
1672
+ nxt .throw (GeneratorExit )
1673
+
1674
+ with self .assertRaisesRegex (
1675
+ RuntimeError ,
1676
+ r"cannot reuse already awaited aclose\(\)/athrow\(\)"
1677
+ ):
1678
+ nxt .throw (GeneratorExit )
1679
+
1680
+ def test_async_gen_throw_custom_same_aclose_coro_twice (self ):
1681
+ async def async_iterate ():
1682
+ yield 1
1683
+ yield 2
1684
+
1685
+ it = async_iterate ()
1686
+
1687
+ class MyException (Exception ):
1688
+ pass
1689
+
1690
+ nxt = it .aclose ()
1691
+ with self .assertRaises (MyException ):
1692
+ nxt .throw (MyException )
1693
+
1694
+ with self .assertRaisesRegex (
1695
+ RuntimeError ,
1696
+ r"cannot reuse already awaited aclose\(\)/athrow\(\)"
1697
+ ):
1698
+ nxt .throw (MyException )
1699
+
1700
+ def test_async_gen_throw_custom_same_athrow_coro_twice (self ):
1701
+ async def async_iterate ():
1702
+ yield 1
1703
+ yield 2
1704
+
1705
+ it = async_iterate ()
1706
+
1707
+ class MyException (Exception ):
1708
+ pass
1709
+
1710
+ nxt = it .athrow (MyException )
1711
+ with self .assertRaises (MyException ):
1712
+ nxt .throw (MyException )
1713
+
1714
+ with self .assertRaisesRegex (
1715
+ RuntimeError ,
1716
+ r"cannot reuse already awaited aclose\(\)/athrow\(\)"
1717
+ ):
1718
+ nxt .throw (MyException )
1719
+
1674
1720
def test_async_gen_aclose_twice_with_different_coros (self ):
1675
1721
# Regression test for https://bugs.python.org/issue39606
1676
1722
async def async_iterate ():
@@ -1752,6 +1798,19 @@ async def gen():
1752
1798
g .aclose ()
1753
1799
gc_collect ()
1754
1800
1801
+ def test_aclose_throw (self ):
1802
+ async def gen ():
1803
+ return
1804
+ yield
1805
+
1806
+ class MyException (Exception ):
1807
+ pass
1808
+
1809
+ g = gen ()
1810
+ with self .assertRaises (MyException ):
1811
+ g .aclose ().throw (MyException )
1812
+ del g
1813
+ gc_collect ()
1755
1814
1756
1815
1757
1816
if __name__ == "__main__" :
0 commit comments