Skip to content
Closed
Changes from all 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
54 changes: 10 additions & 44 deletions Lib/asyncio/unix_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ def _sighandler_noop(signum, frame):
pass


try:
_fspath = os.fspath
except AttributeError:
# Python 3.5 or earlier
_fspath = lambda path: path


class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop):
"""Unix event loop.

Expand Down Expand Up @@ -74,7 +67,7 @@ def add_signal_handler(self, sig, callback, *args):
Raise RuntimeError if there is a problem setting up the handler.
"""
if (coroutines.iscoroutine(callback)
or coroutines.iscoroutinefunction(callback)):
or coroutines.iscoroutinefunction(callback)):
raise TypeError("coroutines cannot be used "
"with add_signal_handler()")
self._check_signal(sig)
Expand Down Expand Up @@ -226,7 +219,7 @@ def create_unix_connection(self, protocol_factory, path=None, *,
raise ValueError(
'path and sock can not be specified at the same time')

path = _fspath(path)
path = os.fspath(path)
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0)
try:
sock.setblocking(False)
Expand Down Expand Up @@ -260,7 +253,7 @@ def create_unix_server(self, protocol_factory, path=None, *,
raise ValueError(
'path and sock can not be specified at the same time')

path = _fspath(path)
path = os.fspath(path)
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

# Check for abstract socket. `str` and `bytes` paths are supported.
Expand All @@ -272,7 +265,8 @@ def create_unix_server(self, protocol_factory, path=None, *,
pass
except OSError as err:
# Directory may have permissions only to create socket.
logger.error('Unable to check or remove stale UNIX socket %r: %r', path, err)
logger.error('Unable to check or remove stale UNIX socket '
'%r: %r', path, err)

try:
sock.bind(path)
Expand Down Expand Up @@ -306,18 +300,6 @@ def create_unix_server(self, protocol_factory, path=None, *,
return server


if hasattr(os, 'set_blocking'):
def _set_nonblocking(fd):
os.set_blocking(fd, False)
else:
import fcntl

def _set_nonblocking(fd):
flags = fcntl.fcntl(fd, fcntl.F_GETFL)
flags = flags | os.O_NONBLOCK
fcntl.fcntl(fd, fcntl.F_SETFL, flags)


class _UnixReadPipeTransport(transports.ReadTransport):

max_size = 256 * 1024 # max bytes we read in one event loop iteration
Expand All @@ -340,7 +322,7 @@ def __init__(self, loop, pipe, protocol, waiter=None, extra=None):
self._protocol = None
raise ValueError("Pipe transport is for pipes/sockets only.")

_set_nonblocking(self._fileno)
os.set_blocking(self._fileno, False)

self._loop.call_soon(self._protocol.connection_made, self)
# only start reading when connection_made() has been called
Expand Down Expand Up @@ -469,7 +451,7 @@ def __init__(self, loop, pipe, protocol, waiter=None, extra=None):
raise ValueError("Pipe transport is only for "
"pipes, sockets and character devices")

_set_nonblocking(self._fileno)
os.set_blocking(self._fileno, False)
self._loop.call_soon(self._protocol.connection_made, self)

# On AIX, the reader trick (to be notified when the read end of the
Expand Down Expand Up @@ -648,22 +630,6 @@ def _call_connection_lost(self, exc):
self._loop = None


if hasattr(os, 'set_inheritable'):
# Python 3.4 and newer
_set_inheritable = os.set_inheritable
else:
import fcntl

def _set_inheritable(fd, inheritable):
cloexec_flag = getattr(fcntl, 'FD_CLOEXEC', 1)

old = fcntl.fcntl(fd, fcntl.F_GETFD)
if not inheritable:
fcntl.fcntl(fd, fcntl.F_SETFD, old | cloexec_flag)
else:
fcntl.fcntl(fd, fcntl.F_SETFD, old & ~cloexec_flag)


class _UnixSubprocessTransport(base_subprocess.BaseSubprocessTransport):

def _start(self, args, shell, stdin, stdout, stderr, bufsize, **kwargs):
Expand All @@ -680,7 +646,7 @@ def _start(self, args, shell, stdin, stdout, stderr, bufsize, **kwargs):
# needed by close_fds=False on Python 3.3 and older
# (Python 3.4 implements the PEP 446, socketpair returns
# non-inheritable sockets)
_set_inheritable(stdin_w.fileno(), False)
os.set_inheritable(stdin_w.fileno(), False)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does not the above comment make this unneeded? @vstinner, what are your thoughts?

self._proc = subprocess.Popen(
args, shell=shell, stdin=stdin, stdout=stdout, stderr=stderr,
universal_newlines=False, bufsize=bufsize, **kwargs)
Expand Down Expand Up @@ -1035,8 +1001,8 @@ def set_event_loop(self, loop):

super().set_event_loop(loop)

if self._watcher is not None and \
isinstance(threading.current_thread(), threading._MainThread):
if (self._watcher is not None and
isinstance(threading.current_thread(), threading._MainThread)):
self._watcher.attach_loop(loop)

def get_child_watcher(self):
Expand Down