-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
await anext() returns None when default is given #87917
Comments
The new anext() builtin in Python 3.10.0a7 doesn't seem to work properly when a default value is provided as the second argument. Here's an example: import asyncio
async def f():
yield 'A'
yield 'B'
async def main():
g = f()
print(await anext(g, 'Z')) # Prints 'None' instead of 'A'!!!
print(await anext(g, 'Z')) # Prints 'None' instead of 'B'!!!
print(await anext(g, 'Z')) # Prints 'Z'
g = f()
print(await anext(g)) # Prints 'A'
print(await anext(g)) # Prints 'B'
print(await anext(g)) # Raises StopAsyncIteration
asyncio.run(main()) As indicated above, anext() works fine when no default is given (in the second half of main()), but produces None in every iteration when a default is given (in the first half of main()) except when the iterator is exhausted. |
I can open a PR this evening, but I think this is close to the issue: PyIter_Next() already silences StopIteration, so checking for it afterward fails. diff --git a/Objects/iterobject.c b/Objects/iterobject.c
index f0c6b79917..95f4659dc9 100644
--- a/Objects/iterobject.c
+++ b/Objects/iterobject.c
@@ -316,7 +316,7 @@ anextawaitable_traverse(anextawaitableobject *obj, visitproc visit, void *arg)
static PyObject *
anextawaitable_iternext(anextawaitableobject *obj)
{
- PyObject *result = PyIter_Next(obj->wrapped);
+ PyObject *result = (*Py_TYPE(obj->wrapped)->tp_iternext)(obj->wrapped);
if (result != NULL) {
return result;
} |
That change fixes that bug, but I think there may be another bug involving when a custom async iterator is passed rather than an async generator. This is at the limit of my knowledge, so any guidance would be appreciated. The test I wrote in the PR currently fails, due to some But it definitely seems like the aiter()/anext() code needs more test coverage. |
Regarding the custom async iterator, I don't know if this is the problem you're referring to, but the following code seems to terminate abruptly when running main2() (main1() is fine). This is without your changes, though. import asyncio
class CustomAsyncIter:
def __init__(self):
self.iterator = iter(['A', 'B'])
def __aiter__(self):
return self
async def __anext__(self):
try:
x = next(self.iterator)
except StopIteration:
raise StopAsyncIteration from None
await asyncio.sleep(1)
return x
async def main1():
iter1 = CustomAsyncIter()
print(await anext(iter1)) # Prints 'A'
print(await anext(iter1)) # Prints 'B'
print(await anext(iter1)) # Raises StopAsyncIteration
async def main2():
iter1 = CustomAsyncIter()
print('Before') # Prints 'Before'
print(await anext(iter1, 'Z')) # Silently terminates the script!!!
print('After') # This never gets executed
asyncio.run(main2()) |
Okay, the PR should fix those problems now. I am still apprehensive about whether all of the corner cases are covered, so reviews are welcome, as are suggestions of more test cases. |
Thanks! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: