diff --git a/newsfragments/3041.bugfix.rst b/newsfragments/3041.bugfix.rst new file mode 100644 index 0000000000..b21851e5b3 --- /dev/null +++ b/newsfragments/3041.bugfix.rst @@ -0,0 +1 @@ +Allow sockets to bind any ``os.PathLike`` object. diff --git a/src/trio/_socket.py b/src/trio/_socket.py index 0a3bd1cba1..69a4d773d4 100644 --- a/src/trio/_socket.py +++ b/src/trio/_socket.py @@ -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 diff --git a/src/trio/_tests/test_socket.py b/src/trio/_tests/test_socket.py index b98b3246e9..9397733c72 100644 --- a/src/trio/_tests/test_socket.py +++ b/src/trio/_tests/test_socket.py @@ -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 @@ -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) @@ -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")