From ffc7a350f714073c5a12804aca6f75f3a54d5ed5 Mon Sep 17 00:00:00 2001 From: lewoudar Date: Sun, 9 Feb 2020 22:22:57 +0100 Subject: [PATCH 1/3] Update function open_unix_socket to check the type of filename argument Update related test file and docstring --- trio/_highlevel_open_unix_stream.py | 5 +++-- trio/tests/test_highlevel_open_unix_stream.py | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/trio/_highlevel_open_unix_stream.py b/trio/_highlevel_open_unix_stream.py index 5992c4f73e..456ae351c1 100644 --- a/trio/_highlevel_open_unix_stream.py +++ b/trio/_highlevel_open_unix_stream.py @@ -36,12 +36,13 @@ async def open_unix_socket(filename,): Raises: OSError: If the socket file could not be connected to. RuntimeError: If AF_UNIX sockets are not supported. + TypeError: if filename is not str or bytes. """ if not has_unix: raise RuntimeError("Unix sockets are not supported on this platform") - if filename is None: - raise ValueError("Filename cannot be None") + if not isinstance(filename, (str, bytes)): + raise TypeError("Filename must be str or bytes") # much more simplified logic vs tcp sockets - one socket type and only one # possible location to connect to diff --git a/trio/tests/test_highlevel_open_unix_stream.py b/trio/tests/test_highlevel_open_unix_stream.py index c66028b14f..872a43dd6d 100644 --- a/trio/tests/test_highlevel_open_unix_stream.py +++ b/trio/tests/test_highlevel_open_unix_stream.py @@ -30,6 +30,12 @@ def close(self): assert c.closed +@pytest.mark.parametrize('filename', [4, 4.5]) +async def test_open_with_bad_filename_type(filename): + with pytest.raises(TypeError): + await open_unix_socket(filename) + + async def test_open_bad_socket(): # mktemp is marked as insecure, but that's okay, we don't want the file to # exist From 75cd12a6c7b37ec118428426f24db4c78dbbef36 Mon Sep 17 00:00:00 2001 From: lewoudar Date: Tue, 11 Feb 2020 04:26:39 +0100 Subject: [PATCH 2/3] Add trio.Path to possible filename types --- trio/_highlevel_open_unix_stream.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/trio/_highlevel_open_unix_stream.py b/trio/_highlevel_open_unix_stream.py index 456ae351c1..7a424729d0 100644 --- a/trio/_highlevel_open_unix_stream.py +++ b/trio/_highlevel_open_unix_stream.py @@ -41,8 +41,8 @@ async def open_unix_socket(filename,): if not has_unix: raise RuntimeError("Unix sockets are not supported on this platform") - if not isinstance(filename, (str, bytes)): - raise TypeError("Filename must be str or bytes") + if not isinstance(filename, (str, trio.Path, bytes)): + raise TypeError("Filename must be str, trio.Path or bytes") # much more simplified logic vs tcp sockets - one socket type and only one # possible location to connect to From e8659acad6251b6a79e5db1fb5194796948fcb15 Mon Sep 17 00:00:00 2001 From: lewoudar Date: Tue, 11 Feb 2020 21:25:34 +0100 Subject: [PATCH 3/3] Change implementation to check filename type Remove TypeError in docstring --- trio/_highlevel_open_unix_stream.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/trio/_highlevel_open_unix_stream.py b/trio/_highlevel_open_unix_stream.py index 7a424729d0..59141ebc38 100644 --- a/trio/_highlevel_open_unix_stream.py +++ b/trio/_highlevel_open_unix_stream.py @@ -36,18 +36,14 @@ async def open_unix_socket(filename,): Raises: OSError: If the socket file could not be connected to. RuntimeError: If AF_UNIX sockets are not supported. - TypeError: if filename is not str or bytes. """ if not has_unix: raise RuntimeError("Unix sockets are not supported on this platform") - if not isinstance(filename, (str, trio.Path, bytes)): - raise TypeError("Filename must be str, trio.Path or bytes") - # much more simplified logic vs tcp sockets - one socket type and only one # possible location to connect to sock = socket(AF_UNIX, SOCK_STREAM) with close_on_error(sock): - await sock.connect(filename) + await sock.connect(trio._util.fspath(filename)) return trio.SocketStream(sock)