8000 gh-131670: Fix crash in `anext()` when `__anext__` is sync and raises · sobolevn/cpython@6862fb4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6862fb4

Browse files
committed
pythongh-131670: Fix crash in anext() when __anext__ is sync and raises
1 parent 1d6a2e6 commit 6862fb4

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

Lib/test/test_asyncgen.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,25 @@ async def run():
11691169

11701170
self.loop.run_until_complete(run())
11711171

1172+
def test_sync_anext_raises_exception(self):
1173+
# See: https://github.com/python/cpython/issues/131670
1174+
msg = 'custom'
1175+
for exc in [
1176+
StopAsyncIteration(msg),
1177+
StopIteration(msg),
1178+
ValueError(msg),
1179+
Exception(msg),
1180+
]:
1181+
with self.subTest(exc=exc):
1182+
class A:
1183+
def __anext__(self):
1184+< 8000 div class="diff-text-inner"> raise exc
1185+
1186+
with self.assertRaisesRegex(type(exc), msg):
1187+
anext(A())
1188+
with self.assertRaisesRegex(type(exc), msg):
1189+
anext(A(), 1)
1190+
11721191
def test_async_gen_asyncio_anext_stopiteration(self):
11731192
async def foo():
11741193
try:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :func:`anext` failing on sync :meth:`~object.__anext__` that raises
2+
:exc:`StopAsyncIteration`.

Python/bltinmodule.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,6 +1837,9 @@ builtin_anext_impl(PyObject *module, PyObject *aiterator,
18371837
}
18381838

18391839
awaitable = (*t->tp_as_async->am_anext)(aiterator);
1840+
if (awaitable == NULL) {
1841+
return NULL;
1842+
}
18401843
if (default_value == NULL) {
18411844
return awaitable;
18421845
}

0 commit comments

Comments
 (0)
0