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

Fix pytest-socket raises IndexError with httpx #85

Merged
merged 1 commit into from Dec 12, 2021
Merged

Fix pytest-socket raises IndexError with httpx #85

merged 1 commit into from Dec 12, 2021

Conversation

ollipa
Copy link
Contributor

@ollipa ollipa commented Nov 25, 2021

Closes #84

@codeclimate
Copy link

codeclimate bot commented Nov 25, 2021

Code Climate has analyzed commit e8ddd80 and detected 0 issues on this pull request.

View more on Code Climate.

@miketheman miketheman added the enhancement New feature or request label Dec 12, 2021
@miketheman
Copy link
Owner

Thanks for the detailed report! I can't help but wonder that there's something else going on here - I've seen this behavior somewhere else and I can't recall what triggered it, but I don't think it was specific to the httpx library.

I think an alternate solution could be:

            try:
                is_unix_socket = args[0] == socket.AF_UNIX if args else None
            except AttributeError:

which would likely solve the same issue, but I think your approach is more Pythonic.

i wish I remembered how that was triggered, so would be able to not have to include another external package for testing, but until I can recall, this will do. Thanks!

@miketheman miketheman merged commit 9edd405 into miketheman:main Dec 12, 2021
@miketheman miketheman added this to the 0.50.0 milestone Dec 12, 2021
@ljodal
Copy link

ljodal commented Dec 23, 2021

@miketheman This is also triggered by asyncpg. Looks like AsyncIO is using kwargs-only in the socket.socket() call here.

/opt/hostedtoolcache/Python/3.10.1/x64/lib/python3.10/asyncio/base_events.py:641: in run_until_complete
    return future.result()
.venv/lib/python3.10/site-packages/pytest_asyncio/plugin.py:123: in setup
    res = await gen_obj.__anext__()
tests/conftest.py:52: in _connection
    connection = await asyncpg.connect()
.venv/lib/python3.10/site-packages/asyncpg/connection.py:2085: in connect
    return await connect_utils._connect(
.venv/lib/python3.10/site-packages/asyncpg/connect_utils.py:881: in _connect
    return await _connect_addr(
.venv/lib/python3.10/site-packages/asyncpg/connect_utils.py:781: in _connect_addr
    return await __connect_addr(params, timeout, True, *args)
.venv/lib/python3.10/site-packages/asyncpg/connect_utils.py:825: in __connect_addr
    tr, pr = await compat.wait_for(connector, timeout=timeout)
.venv/lib/python3.10/site-packages/asyncpg/compat.py:66: in wait_for
    return await asyncio.wait_for(fut, timeout)
/opt/hostedtoolcache/Python/3.10.1/x64/lib/python3.10/asyncio/tasks.py:445: in wait_for
    return fut.result()
.venv/lib/python3.10/site-packages/asyncpg/connect_utils.py:691: in _create_ssl_connection
    tr, pr = await loop.create_connection(
/opt/hostedtoolcache/Python/3.10.1/x64/lib/python3.10/asyncio/base_events.py:1040: in create_connection
    sock = await self._connect_sock(
/opt/hostedtoolcache/Python/3.10.1/x64/lib/python3.10/asyncio/base_events.py:937: in _connect_sock
    sock = socket.socket(family=family, type=type_, proto=proto)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

cls = <class 'pytest_socket.disable_socket.<locals>.GuardedSocket'>, args = ()
kwargs = {'family': <AddressFamily.AF_INET6: 10>, 'proto': 6, 'type': <SocketKind.SOCK_STREAM: 1>}

    def __new__(cls, *args, **kwargs):
        try:
>           is_unix_socket = args[0] == socket.AF_UNIX
E           IndexError: tuple index out of range

.venv/lib/python3.10/site-packages/pytest_socket.py:89: IndexError

@miketheman
Copy link
Owner

miketheman commented Dec 23, 2021

@ljodal Thanks for the report - can you try out the plugin with the latest code on main? The behavior has changed, and I'm planning on releasing 0.50.0 soon.

@ljodal
Copy link

ljodal commented Dec 23, 2021

@miketheman It works with latest main.

I'd argue that as family is specified as part of kwargs here a better fix in this case would be to do something like this:

if 'family' in kwargs:
    family = kwargs['family']
elif args:
    family = args[0]
else:
    # This seems to be the default?
    family = socket.AF_INET

try:
    is_unix_socket = family == socket.AF_UNIX
except AttributeError:
    is_unix_socket = False

Or at least adding a check in kwargs:

try:
    family = kwargs['family'] if 'family' in kwargs else args[0]
    is_unix_socket = family == socket.AF_UNIX
except (AttributeError, IndexError):
    is_unix_socket = False

@miketheman
Copy link
Owner

@ljodal glad to hear it! To be clear, the code in the PR is not the final fix - the kwargs have been updated to match the parent class' signature in #75 and has been since extracted to a common helper function _is_unix_socket().

@ljodal
Copy link

ljodal commented Dec 23, 2021

Ah, great! Should've checked main, sorry!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pytest-socket raises IndexError when used with httpx
3 participants