Skip to content
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

Coroutines & async generators disagree on the iteration protocol semantics #89251

Open
1st1 opened this issue Sep 2, 2021 · 2 comments
Open
Labels
3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@1st1
Copy link
Member

1st1 commented Sep 2, 2021

BPO 45088
Nosy @gvanrossum, @ambv, @1st1, @pablogsal

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2021-09-02.20:32:30.403>
labels = ['interpreter-core', 'type-bug', '3.10', '3.11']
title = 'Coroutines & async generators disagree on the iteration protocol semantics'
updated_at = <Date 2021-09-03.03:26:08.506>
user = 'https://github.com/1st1'

bugs.python.org fields:

activity = <Date 2021-09-03.03:26:08.506>
actor = 'yselivanov'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Interpreter Core']
creation = <Date 2021-09-02.20:32:30.403>
creator = 'yselivanov'
dependencies = []
files = []
hgrepos = []
issue_num = 45088
keywords = []
message_count = 1.0
messages = ['400951']
nosy_count = 4.0
nosy_names = ['gvanrossum', 'lukasz.langa', 'yselivanov', 'pablogsal']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'needs patch'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue45088'
versions = ['Python 3.10', 'Python 3.11']

@1st1
Copy link
Member Author

1st1 commented Sep 2, 2021

See this script:

https://gist.github.com/1st1/eccc32991dc2798f3fa0b4050ae2461d

Somehow an identity async function alters the behavior of manual iteration though the wrapped nested generator.

This is a very subtle bug and I'm not even sure if this is a bug or not. Opening the issue so that I don't forget about this and debug sometime later.

@1st1 1st1 added 3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Sep 2, 2021
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@iritkatriel
Copy link
Member

iritkatriel commented Nov 7, 2023

Copying Yury's script from the link to here:

import types

class MyError(Exception): pass

@types.coroutine
def _async_yield(v):
    return (yield v)


async def test():
    try:
        await _async_yield(1)
    except MyError:
        await _async_yield(2)
    return
    yield


def thin_anext(gen):
    return gen.__anext__()


async def thick_anext(gen):
    return await gen.__anext__()


def run(anext):
    print('\n\n+++ run', anext)

    gen = test()

    aw = anext(gen).__await__()
    print('1. send', aw.send(None))
    aw.close()

    aw = anext(gen).__await__()
    try:
        val = aw.throw(MyError, MyError(), None)
        print('2. throw: got value', val)
    except MyError as e:
        print('2. throw: got error', type(e), e)


run(thin_anext)
run(thick_anext)

Output:

+++ run <function thin_anext at 0x7fd1cd682840>
1. send 1
2. throw: got value 2


+++ run <function thick_anext at 0x7fd1cd6828e0>
1. send 1
2. throw: got error <class '__main__.MyError'> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants