Commit 1e8c7677 authored by Yury Selivanov's avatar Yury Selivanov

Issue #28704: Fix create_unix_server to support Path-like objects

parent d0965de0
...@@ -39,6 +39,13 @@ def _sighandler_noop(signum, frame): ...@@ -39,6 +39,13 @@ def _sighandler_noop(signum, frame):
pass pass
try:
_fspath = os.fspath
except AttributeError:
# Python 3.5 or earlier
_fspath = lambda path: path
class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop): class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop):
"""Unix event loop. """Unix event loop.
...@@ -256,6 +263,7 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop): ...@@ -256,6 +263,7 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop):
raise ValueError( raise ValueError(
'path and sock can not be specified at the same time') 'path and sock can not be specified at the same time')
path = _fspath(path)
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
# Check for abstract socket. `str` and `bytes` paths are supported. # Check for abstract socket. `str` and `bytes` paths are supported.
......
...@@ -4,6 +4,7 @@ import collections ...@@ -4,6 +4,7 @@ import collections
import errno import errno
import io import io
import os import os
import pathlib
import signal import signal
import socket import socket
import stat import stat
...@@ -251,6 +252,15 @@ class SelectorEventLoopUnixSocketTests(test_utils.TestCase): ...@@ -251,6 +252,15 @@ class SelectorEventLoopUnixSocketTests(test_utils.TestCase):
srv.close() srv.close()
self.loop.run_until_complete(srv.wait_closed()) self.loop.run_until_complete(srv.wait_closed())
@unittest.skipUnless(hasattr(os, 'fspath'), 'no os.fspath')
def test_create_unix_server_pathlib(self):
with test_utils.unix_socket_path() as path:
path = pathlib.Path(path)
srv_coro = self.loop.create_unix_server(lambda: None, path)
srv = self.loop.run_until_complete(srv_coro)
srv.close()
self.loop.run_until_complete(srv.wait_closed())
def test_create_unix_server_existing_path_nonsock(self): def test_create_unix_server_existing_path_nonsock(self):
with tempfile.NamedTemporaryFile() as file: with tempfile.NamedTemporaryFile() as file:
coro = self.loop.create_unix_server(lambda: None, file.name) coro = self.loop.create_unix_server(lambda: None, file.name)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment