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

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

merged 3 commits into from
Feb 12, 2020

Conversation

lewoudar
Copy link
Contributor

Hi,
this PR fixes an edge case where a user can pass a number when calling open_unix_socket. In this case he will get a TypeError. There are two possibilities:

  • We can just let socket function raises the error or
  • We can intercept the error and print a nicer message, which is what I did

And in all cases, we need to update the docstring :)

@@ -36,12 +36,13 @@ def close_on_error(obj):
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 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

Remove TypeError in docstring
@pquentin
Copy link
Member

Cycling the pull request to fix the Travis checks.

@pquentin pquentin closed this Feb 12, 2020
@pquentin pquentin reopened this Feb 12, 2020
@pquentin pquentin merged commit 84694d9 into python-trio:master Feb 12, 2020
@pquentin
Copy link
Member

Thanks!

@lewoudar
Copy link
Contributor Author

You are welcome!

@lewoudar lewoudar deleted the open-unix-socket-fix branch February 12, 2020 20:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants