-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
bpo-43751: Fix anext() bug where it erroneously returned None #25238
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
Merged
Merged
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
065331f
Add another test for anext
sweeneyde ddeac2d
Fix anext(ait, default) returning None
sweeneyde 134449b
Add null check for getter
sweeneyde bfd57ad
Test anext() on generator and python-implemented async iterator class
sweeneyde d8c56ff
fix whitespace
sweeneyde ab54ece
📜🤖 Added by blurb_it.
blurb-it[bot] 056d9c6
Explicity call am_await() when applicable.
sweeneyde 58a56fb
Refactor tests
sweeneyde a113e19
Add test for async generator with __anext__ = types.coroutine(...)
sweeneyde 4039170
Update Objects/iterobject.c
sweeneyde 0130217
fix minor issues; add a test case for bad awaitables
sweeneyde 665f84c
merge with github
sweeneyde 5607d83
Add more tests for corner cases
sweeneyde 11fe9c2
make the corner-case tests pass
sweeneyde 6a6fa66
Add null check for new_awaitable; remove null check for am_await beca…
sweeneyde 857a2b0
Avoid leaking awaitable
sweeneyde File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Core and Builtins/2021-04-07-18-00-05.bpo-43751.8fHsqQ.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fixed a bug where ``anext(ait, default)`` would erroneously return None. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -316,7 +316,50 @@ anextawaitable_traverse(anextawaitableobject *obj, visitproc visit, void *arg) | |
static PyObject * | ||
anextawaitable_iternext(anextawaitableobject *obj) | ||
{ | ||
PyObject *result = PyIter_Next(obj->wrapped); | ||
/* Consider the following class: | ||
* | ||
* class A: | ||
* async def __anext__(self): | ||
* ... | ||
* a = A() | ||
* | ||
* Then `await anext(a)` should call | ||
* a.__anext__().__await__().__next__() | ||
* | ||
* On the other hand, given | ||
* | ||
* async def agen(): | ||
* yield 1 | ||
* yield 2 | ||
* gen = agen() | ||
* | ||
* Then `await anext(gen)` can just call | ||
* gen.__anext__().__next__() | ||
*/ | ||
assert(obj->wrapped != NULL); | ||
PyObject *awaitable = _PyCoro_GetAwaitableIter(obj->wrapped); | ||
if (awaitable == NULL) { | ||
return NULL; | ||
} | ||
if (Py_TYPE(awaitable)->tp_iternext == NULL) { | ||
/* _PyCoro_GetAwaitableIter returns a Coroutine, a Generator, | ||
* or an iterator. Of these, only coroutines lack tp_iternext. | ||
*/ | ||
assert(PyCoro_CheckExact(awaitable)); | ||
unaryfunc getter = Py_TYPE(awaitable)->tp_as_async->am_await; | ||
PyObject *new_awaitable = getter(awaitable); | ||
if (new_awaitable == NULL) { | ||
Py_DECREF(awaitable); | ||
return NULL; | ||
} | ||
Py_SETREF(awaitable, new_awaitable); | ||
if (Py_TYPE(awaitable)->tp_iternext == NULL) { | ||
PyErr_SetString(PyExc_TypeError, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is leaking |
||
"__await__ returned a non-iterable"); | ||
return NULL; | ||
} | ||
} | ||
PyObject *result = (*Py_TYPE(awaitable)->tp_iternext)(awaitable); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to decrement awaitable once you are done with it |
||
if (result != NULL) { | ||
return result; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can raise, you need to check for errors and propagate accordingly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, can you add a test that checks this code path?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the check, but I couldn't figure out how to make a test to check the code path, since
awaitable
is always a coroutine, sogetter
points to this in genobject.c:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to add a test to the
_testcapi
module, but this is going to be a bit too much for just this, so is fine if we don't add such a test.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, isn't this something you can get with a custom
__await__
in a class?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_PyCoro_GetAwaitableIter calls am_await and ensures it's iterable; this if block is only for coroutines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test_anext_bad_await
covers__await__
methods that raiseThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍