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

Update function open_unix_socket to check the type of filename argument #1399

Merged
merged 3 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions trio/_highlevel_open_unix_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should mention this in the documentation. It's a general rule that pretty much any Python function can raise TypeError if you give it the wrong argument types, and mentioning it explicitly here distracts from more relevant information.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok for TypeError I removed it

"""
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, trio.Path, bytes)):
raise TypeError("Filename must be str, trio.Path or bytes")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't quite correct... really the criterion is "is the object a valid path". And starting in Python 3.6, the proper way to check that is to use the fspath protocol. So if we want to do this check, then the right way to do it would be something like:

filename = os.fspath(filename)

This automatically handles all "path-like" objects, including new ones defined in third-party libraries that we've never heard of. And if the user passes in something inappropriate, it gives a good error message:

>>> os.fspath(4)                                                                      
TypeError: expected str, bytes or os.PathLike object, not int

Annoyingly, though, we still support Python 3.5 (though not for long, see #1396), so we can't just use os.fspath. Instead, use our compatibility wrapper: trio._util.fspath

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the information, I didn't know the function os.fspath. To be honest, when I made the change, I told myself that a pathlib.Path object would also be valid but I was "comforted" by the fact that the tests only took into account trio.Path 😛
I changed the check


# much more simplified logic vs tcp sockets - one socket type and only one
# possible location to connect to
Expand Down
6 changes: 6 additions & 0 deletions trio/tests/test_highlevel_open_unix_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down