mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
gh-131670: Fix crash in anext()
when __anext__
is sync and raises (#131682)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
parent
5fef4ff9ed
commit
929afd1d6e
3 changed files with 24 additions and 0 deletions
|
@ -1169,6 +1169,26 @@ class AsyncGenAsyncioTest(unittest.TestCase):
|
||||||
|
|
||||||
self.loop.run_until_complete(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):
|
def test_async_gen_asyncio_anext_stopiteration(self):
|
||||||
async def foo():
|
async def foo():
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix :func:`anext` failing on sync :meth:`~object.__anext__` raising an exception.
|
|
@ -1837,6 +1837,9 @@ builtin_anext_impl(PyObject *module, PyObject *aiterator,
|
||||||
}
|
}
|
||||||
|
|
||||||
awaitable = (*t->tp_as_async->am_anext)(aiterator);
|
awaitable = (*t->tp_as_async->am_anext)(aiterator);
|
||||||
|
if (awaitable == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
if (default_value == NULL) {
|
if (default_value == NULL) {
|
||||||
return awaitable;
|
return awaitable;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue