Skip to content

Preserve synchronous iteration over awaited collections - #12

Open
EiNSTeiN- wants to merge 2 commits into
ejfkdev:mainfrom
EiNSTeiN-:fix/awaited-synchronous-iteration
Open

EiNSTeiN- wants to merge 2 commits into
ejfkdev:mainfrom
EiNSTeiN-:fix/awaited-synchronous-iteration

Conversation

@EiNSTeiN-

Copy link
Copy Markdown

I am opening this PR to preserve synchronous iteration over an awaited collection. The code below currently decompiles with the wrong iteration protocol:

async def select(read):
    result = []
    for item in await read():
        result.append(item)
    return result

The loop is currently emitted as:

    async for item in read():
        result.append(item)

The producer must be awaited once and its result then consumed as a regular iterable through __iter__. An async for instead requires the asynchronous iteration protocol and no longer performs the source's await read() operation. A producer returning an ordinary list therefore no longer works.

When the FOR_ITER handler sees an Await expression, it currently removes the await wrapper and marks the loop asynchronous. With this change, FOR_ITER always selects synchronous iteration. GET_AITER/GET_ANEXT retain their separate genuine-async path, so async for item in read() keeps its existing meaning.

The source syntax begins in Python 3.5; the repair is not limited to a recent bytecode version.

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