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

Add support for all_errors to asyncio.create_connection #93973

Closed
pamelafox opened this issue Jun 17, 2022 · 2 comments
Closed

Add support for all_errors to asyncio.create_connection #93973

pamelafox opened this issue Jun 17, 2022 · 2 comments
Labels
3.12 bugs and security fixes topic-asyncio type-feature A feature request or enhancement

Comments

@pamelafox
Copy link
Contributor

Feature or enhancement

As a result of the discussion on #74166, @iritkatriel added the all_errors keyword argument to socket.py:create_connection. When set to True, multiple exceptions are raised using an ExceptionGroup instead of a single OSError with the messages combined. I propose adding the same all_errors keyword to asyncio.create_connection, which would raise an ExceptionGroup in the case of connection errors (not for other errors, however).

Pitch

An ExceptionGroup is much nicer to handle than parsing through a concatenated string. An issue in the websockets library indicates that developers would like that for asyncio.create_connection as well. I personally ran into it myself as well.

Example usage:

async def check_ports(host: str, start: int, end: int, max=10):
    for port in range(start, end):
        try:
            future = asyncio.open_connection(host=host, port=port, all_errors=True)
            r, w = await asyncio.wait_for(future, timeout=timeout)
            yield port
            w.close()
        except* ConnectionRefusedError:
            pass
        except* asyncio.TimeoutError:
            pass

Previous discussion

Related discussion on:
#74166

I will send a PR with the proposed change.

@pamelafox pamelafox added the type-feature A feature request or enhancement label Jun 17, 2022
@kumaraditya303 kumaraditya303 added topic-asyncio 3.12 bugs and security fixes labels Sep 4, 2022
@gvanrossum
Copy link
Member

Sounds like a useful feature indeed.

gvanrossum pushed a commit that referenced this issue Sep 5, 2022
Co-authored-by: Oleg Iarygin <dralife@yandex.ru>
@kumaraditya303
Copy link
Contributor

Fixed by #93974

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes topic-asyncio type-feature A feature request or enhancement
Projects
Status: Done
Development

No branches or pull requests

3 participants