Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Commit fff05d4

Browse files
committed
Fix create_unix_server to support Path-like objects (PEP 519)
1 parent 7e9a4d5 commit fff05d4

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

asyncio/unix_events.py

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ def _sighandler_noop(signum, frame):
3939
pass
4040

4141

42+
try:
43+
_fspath = os.fspath
44+
except AttributeError:
45+
# Python 3.5 or earlier
46+
_fspath = lambda path: path
47+
48+
4249
class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop):
4350
"""Unix event loop.
4451
@@ -256,6 +263,7 @@ def create_unix_server(self, protocol_factory, path=None, *,
256263
raise ValueError(
257264
'path and sock can not be specified at the same time')
258265

266+
path = _fspath(path)
259267
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
260268

261269
# Check for abstract socket. `str` and `bytes` paths are supported.

tests/test_unix_events.py

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import errno
55
import io
66
import os
7+
import pathlib
78
import signal
89
import socket
910
import stat
@@ -251,6 +252,15 @@ def test_create_unix_server_existing_path_sock(self):
251252
srv.close()
252253
self.loop.run_until_complete(srv.wait_closed())
253254

255+
@unittest.skipUnless(hasattr(os, 'fspath'), 'no os.fspath')
256+
def test_create_unix_server_pathlib(self):
257+
with test_utils.unix_socket_path() as path:
258+
path = pathlib.Path(path)
259+
srv_coro = self.loop.create_unix_server(lambda: None, path)
260+
srv = self.loop.run_until_complete(srv_coro)
261+
srv.close()
262+
self.loop.run_until_complete(srv.wait_closed())
263+
254264
def test_create_unix_server_existing_path_nonsock(self):
255265
with tempfile.NamedTemporaryFile() as file:
256266
coro = self.loop.create_unix_server(lambda: None, file.name)

0 commit comments

Comments
 (0)