Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Lib/test/test_asyncgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,26 @@ async def run():

self.loop.run_until_complete(run())

def test_sync_anext_raises_exception(self):
# See: https://github.com/python/cpython/issues/131670
msg = 'custom'
for exc_type in [
StopAsyncIteration,
StopIteration,
ValueError,
Exception,
]:
exc = exc_type(msg)
with self.subTest(exc=exc):
class A:
def __anext__(self):
raise exc

with self.assertRaisesRegex(exc_type, msg):
anext(A())
with self.assertRaisesRegex(exc_type, msg):
anext(A(), 1)

def test_async_gen_asyncio_anext_stopiteration(self):
async def foo():
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :func:`anext` failing on sync :meth:`~object.__anext__` raising an exception.
3 changes: 3 additions & 0 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,9 @@ builtin_anext_impl(PyObject *module, PyObject *aiterator,
}

awaitable = (*t->tp_as_async->am_anext)(aiterator);
if (awaitable == NULL) {
return NULL;
}
if (default_value == NULL) {
return awaitable;
}
Expand Down
Loading