Skip to content

Commit

Permalink
Fix trio.socket.SocketType.bind to work with PathLike arguments again (
Browse files Browse the repository at this point in the history
…#3042)

* Fix trio.socket.SocketType.bind to work with PathLike arguments again

fixes #3041

* Test passing various types to trio.socket.SocketType.{bind,connect}

Tests #3041

* Add a newsfragment

* Appease pre-commit

---------

Co-authored-by: EXPLOSION <git@helvetica.moe>
  • Loading branch information
regnarg and A5rocks authored Aug 21, 2024
1 parent af02e8b commit 57c95a5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions newsfragments/3041.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow sockets to bind any ``os.PathLike`` object.
2 changes: 1 addition & 1 deletion src/trio/_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ async def _resolve_address_nocp(
)
elif hasattr(_stdlib_socket, "AF_UNIX") and family == _stdlib_socket.AF_UNIX:
# unwrap path-likes
assert isinstance(address, (str, bytes))
assert isinstance(address, (str, bytes, os.PathLike))
return os.fspath(address)
else:
return address
Expand Down
10 changes: 7 additions & 3 deletions src/trio/_tests/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import socket as stdlib_socket
import sys
import tempfile
from pathlib import Path
from socket import AddressFamily, SocketKind
from typing import TYPE_CHECKING, Any, Callable, List, Tuple, Union

Expand Down Expand Up @@ -1077,7 +1078,7 @@ async def test_unix_domain_socket() -> None:
# Bind has a special branch to use a thread, since it has to do filesystem
# traversal. Maybe connect should too? Not sure.

async def check_AF_UNIX(path: str | bytes) -> None:
async def check_AF_UNIX(path: str | bytes | os.PathLike[str]) -> None:
with tsocket.socket(family=tsocket.AF_UNIX) as lsock:
await lsock.bind(path)
lsock.listen(10)
Expand All @@ -1091,8 +1092,11 @@ async def check_AF_UNIX(path: str | bytes) -> None:
# Can't use tmpdir fixture, because we can exceed the maximum AF_UNIX path
# length on macOS.
with tempfile.TemporaryDirectory() as tmpdir:
path = f"{tmpdir}/sock"
await check_AF_UNIX(path)
# Test passing various supported types as path
# Must use different filenames to prevent "address already in use"
await check_AF_UNIX(f"{tmpdir}/sock")
await check_AF_UNIX(Path(f"{tmpdir}/sock1"))
await check_AF_UNIX(os.fsencode(f"{tmpdir}/sock2"))

try:
cookie = os.urandom(20).hex().encode("ascii")
Expand Down

0 comments on commit 57c95a5

Please sign in to comment.