Skip to content

gh-157361: Implement anext() in Python instead of C - #157362

Merged
kumaraditya303 merged 8 commits into
python:mainfrom
kumaraditya303:anext-in-python
Sep 16, 2026
Merged

kumaraditya303 merged 8 commits into
python:mainfrom
kumaraditya303:anext-in-python

Conversation

@kumaraditya303

@kumaraditya303 kumaraditya303 commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

Remove the C implementation of the anext() builtin and the anext_awaitable helper type. A _pybuiltins module is created with the implementation and that is frozen into the interpreter using the existing freeze infrastructure.

The Python version keeps the C semantics: __anext__ is looked up on the type and called eagerly, the one-argument form returns the awaitable unchanged, and the two-argument form wraps it in a coroutine that returns the default on StopAsyncIteration.

I'll implement aiter similarly in a followup.

Remove the C implementation of the anext() builtin and the
anext_awaitable helper type. Instead, compile a small Python source at
interpreter startup and copy the resulting anext() function into the
builtins dict.

The Python version keeps the C semantics: __anext__ is looked up on the
type and called eagerly, the one-argument form returns the awaitable
unchanged, and the two-argument form wraps it in a coroutine that
returns the default on StopAsyncIteration.

The source is executed from pycore_init_builtins() rather than
_PyBuiltin_Init(), since running bytecode requires the interpreter's
common constants, which are only set up after the builtins module is
created.
Comment thread Lib/_builtins.py Outdated
Comment thread Lib/_pybuiltins.py
Comment thread Lib/_builtins.py
@kumaraditya303 kumaraditya303 added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Sep 13, 2026
@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @kumaraditya303 for commit 9db7837 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F157362%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Sep 13, 2026
Comment thread Lib/_pybuiltins.py
Comment thread Lib/_pybuiltins.py Outdated
Comment thread Lib/test/test_asyncgen.py
@brettcannon
brettcannon removed their request for review September 14, 2026 22:18
kumaraditya303 and others added 3 commits September 16, 2026 09:06
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@kumaraditya303

Copy link
Copy Markdown
Contributor Author

I'm merging this to start working on implementing aiter similar to this, I will address any remaining issues in followup PR.

@kumaraditya303
kumaraditya303 merged commit 0a6c1ed into python:main Sep 16, 2026
54 checks passed
@kumaraditya303
kumaraditya303 deleted the anext-in-python branch September 16, 2026 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants