|
3 | 3 | import unittest |
4 | 4 |
|
5 | 5 | from test.support.import_helper import import_module |
| 6 | +from test.support import gc_collect |
6 | 7 | asyncio = import_module("asyncio") |
7 | 8 |
|
8 | 9 |
|
@@ -1287,6 +1288,85 @@ async def wait(): |
1287 | 1288 |
|
1288 | 1289 | self.assertEqual(finalized, 2) |
1289 | 1290 |
|
| 1291 | + def test_async_gen_asyncio_shutdown_02(self): |
| 1292 | + messages = [] |
| 1293 | + |
| 1294 | + def exception_handler(loop, context): |
| 1295 | + messages.append(context) |
| 1296 | + |
| 1297 | + async def async_iterate(): |
| 1298 | + yield 1 |
| 1299 | + yield 2 |
| 1300 | + |
| 1301 | + it = async_iterate() |
| 1302 | + async def main(): |
| 1303 | + loop = asyncio.get_running_loop() |
| 1304 | + loop.set_exception_handler(exception_handler) |
| 1305 | + |
| 1306 | + async for i in it: |
| 1307 | + break |
| 1308 | + |
| 1309 | + asyncio.run(main()) |
| 1310 | + |
| 1311 | + self.assertEqual(messages, []) |
| 1312 | + |
| 1313 | + def test_async_gen_asyncio_shutdown_exception_01(self): |
| 1314 | + messages = [] |
| 1315 | + |
| 1316 | + def exception_handler(loop, context): |
| 1317 | + messages.append(context) |
| 1318 | + |
| 1319 | + async def async_iterate(): |
| 1320 | + try: |
| 1321 | + yield 1 |
| 1322 | + yield 2 |
| 1323 | + finally: |
| 1324 | + 1/0 |
| 1325 | + |
| 1326 | + it = async_iterate() |
| 1327 | + async def main(): |
| 1328 | + loop = asyncio.get_running_loop() |
| 1329 | + loop.set_exception_handler(exception_handler) |
| 1330 | + |
| 1331 | + async for i in it: |
| 1332 | + break |
| 1333 | + |
| 1334 | + asyncio.run(main()) |
| 1335 | + |
| 1336 | + message, = messages |
| 1337 | + self.assertEqual(message['asyncgen'], it) |
| 1338 | + self.assertIsInstance(message['exception'], ZeroDivisionError) |
| 1339 | + self.assertIn('an error occurred during closing of asynchronous generator', |
| 1340 | + message['message']) |
| 1341 | + |
| 1342 | + def test_async_gen_asyncio_shutdown_exception_02(self): |
| 1343 | + messages = [] |
| 1344 | + |
| 1345 | + def exception_handler(loop, context): |
| 1346 | + messages.append(context) |
| 1347 | + |
| 1348 | + async def async_iterate(): |
| 1349 | + try: |
| 1350 | + yield 1 |
| 1351 | + yield 2 |
| 1352 | + finally: |
| 1353 | + 1/0 |
| 1354 | + |
| 1355 | + async def main(): |
| 1356 | + loop = asyncio.get_running_loop() |
| 1357 | + loop.set_exception_handler(exception_handler) |
| 1358 | + |
| 1359 | + async for i in async_iterate(): |
| 1360 | + break |
| 1361 | + gc_collect() |
| 1362 | + |
| 1363 | + asyncio.run(main()) |
| 1364 | + |
| 1365 | + message, = messages |
| 1366 | + self.assertIsInstance(message['exception'], ZeroDivisionError) |
| 1367 | + self.assertIn('unhandled exception during asyncio.run() shutdown', |
| 1368 | + message['message']) |
| 1369 | + |
1290 | 1370 | def test_async_gen_expression_01(self): |
1291 | 1371 | async def arange(n): |
1292 | 1372 | for i in range(n): |
|
0 commit comments