Skip to content

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

Draft
kumaraditya303 wants to merge 3 commits into
python:mainfrom
kumaraditya303:anext-in-python
Draft

gh-157361: Implement anext() in Python instead of C#157362
kumaraditya303 wants to merge 3 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. 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.

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.
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.

1 participant