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

Regression: unix socket bind does not work with a PathLike argument #3041

Closed
regnarg opened this issue Jul 31, 2024 · 3 comments · Fixed by #3042
Closed

Regression: unix socket bind does not work with a PathLike argument #3041

regnarg opened this issue Jul 31, 2024 · 3 comments · Fixed by #3042
Labels
good first issue typing Adding static types to trio's interface

Comments

@regnarg
Copy link
Contributor

regnarg commented Jul 31, 2024

trio.socket.SocketType.bind no longer works with a PathLike (e.g. pathlib.Path) argument.

Simple test program:

import socket, trio.socket
from pathlib import Path

async def main():
    sock = trio.socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    await sock.bind(Path('/tmp/x.sock'))

trio.run(main)

Results in:

Traceback (most recent call last):
  File "/tmp/x.py", line 8, in <module>
    trio.run(main)
  File "/usr/lib/python3.12/site-packages/trio/_core/_run.py", line 2305, in run
    raise runner.main_task_outcome.error
  File "/tmp/x.py", line 6, in main
    await sock.bind(Path('/tmp/x.sock'))
  File "/usr/lib/python3.12/site-packages/trio/_socket.py", line 874, in bind
    address = await self._resolve_address_nocp(address, local=True)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/trio/_socket.py", line 922, in _resolve_address_nocp
    return await _resolve_address_nocp(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/trio/_socket.py", line 472, in _resolve_address_nocp
    assert isinstance(address, (str, bytes))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

This bugs seems to have been introduced in cf1f3c745, released in v0.23.0.

@CoolCat467
Copy link
Member

You can fix this by doing this:

import socket, trio.socket
from pathlib import Path

async def main():
    sock = trio.socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    await sock.bind(str(Path('/tmp/x.sock')))

trio.run(main)

@regnarg
Copy link
Contributor Author

regnarg commented Jul 31, 2024 via email

@jakkdl
Copy link
Member

jakkdl commented Aug 5, 2024

I don't think this passed, or passes, typing? The method is typed as

Address: TypeAlias = Union[
    str, bytes, Tuple[str, int], Tuple[str, int, int], Tuple[str, int, int, int]
]
async def bind(self, address: Address) -> None:
  ...

and was previously

async def bind(self, address: tuple[object, ...] | str | bytes) -> None:

But yeah you're totally right that the comment mentions "path-likes", and as such the typing should be updated to include Path and/or PathLike and the assertion changed. Thank you!

@jakkdl jakkdl added good first issue typing Adding static types to trio's interface labels Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue typing Adding static types to trio's interface
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants