Repository navigation
gh-157361: Implement anext() in Python instead of C
#157362
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
+239
−281
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cdd7ca2
Implement anext() in Python instead of C
kumaraditya303 fcec7cf
use frozen modules instead
kumaraditya303 0903f03
add news
kumaraditya303 52befa6
address review and fix test
kumaraditya303 9db7837
fix importlib tests
kumaraditya303 15c8f84
Update Lib/_pybuiltins.py
kumaraditya303 cefda05
fix filename in tracebacks
kumaraditya303 d59d97e
use <frozen builtins> as filename in tracebacks
kumaraditya303 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
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,42 @@ | ||
| """Builtins implemented in Python. | ||
|
|
||
| This module is frozen into the interpreter and imported during startup, | ||
| before the import system exists. The names listed in ``__all__`` are | ||
| copied into the ``builtins`` module. | ||
| """ | ||
|
|
||
| __all__ = ['anext'] | ||
|
|
||
| _NOT_GIVEN = sentinel("_NOT_GIVEN") | ||
|
|
||
|
|
||
| def anext(async_iterator, default=_NOT_GIVEN, /): | ||
| """Return the next item from the async iterator. | ||
|
|
||
| If default is given and the async iterator is exhausted, | ||
| it is returned instead of raising StopAsyncIteration. | ||
| """ | ||
| cls = type(async_iterator) | ||
| try: | ||
| # Looked up on the type, like the C slot am_anext. | ||
| anext_method = cls.__anext__ | ||
| except AttributeError: | ||
|
kumaraditya303 marked this conversation as resolved.
|
||
| raise TypeError( | ||
| f"{cls.__name__!r} object is not an async iterator" | ||
| ) from None | ||
| awaitable = anext_method(async_iterator) | ||
| if default is _NOT_GIVEN: | ||
| return awaitable | ||
| return _anext_with_default(awaitable, default) | ||
|
|
||
|
|
||
| async def _anext_with_default(awaitable, default): | ||
| try: | ||
| return await awaitable | ||
| except StopAsyncIteration: | ||
| return default | ||
|
|
||
|
|
||
| for _name in __all__: | ||
| globals()[_name].__module__ = 'builtins' | ||
| del _name | ||
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
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
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
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
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
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
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Core_and_Builtins/2026-09-12-16-40-00.gh-issue-157361.anextpy.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,4 @@ | ||
| Implement :func:`anext` in Python instead of C, in a frozen ``_pybuiltins`` | ||
|
kumaraditya303 marked this conversation as resolved.
|
||
| module. The awaitable returned by ``anext(it, default)`` is now a plain | ||
| coroutine, so introspection tools such as :func:`asyncio.print_call_graph` | ||
| can see through it into :meth:`~object.__anext__`. | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.