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

bpo-32166: Drop Python 3.4 code from asyncio #4612

Merged
merged 12 commits into from
Nov 29, 2017
54 changes: 10 additions & 44 deletions Lib/asyncio/unix_events.py
Original file line number Diff line number Diff line change
@@ -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.

@@ -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)
@@ -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)
@@ -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.
@@ -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)
@@ -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
@@ -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
@@ -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
@@ -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):
@@ -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
Contributor Author

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?

Moved the question from #4633
@vstinner please review

Copy link
Member

Choose a reason for hiding this comment

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

os.set_inheritable(stdin_w.fileno(), False) is useless on Python 3.4 and newer. Remove the call with its comment.

Copy link
Member

Choose a reason for hiding this comment

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

If you don't trust me, please trust the tests ;-)

self.assertEqual(s1.get_inheritable(), False)
self.assertEqual(s2.get_inheritable(), False)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks.
The call is removed.

self._proc = subprocess.Popen(
args, shell=shell, stdin=stdin, stdout=stdout, stderr=stderr,
universal_newlines=False, bufsize=bufsize, **kwargs)
@@ -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):
4 changes: 2 additions & 2 deletions Lib/test/test_asyncio/test_unix_events.py
Original file line number Diff line number Diff line change
@@ -394,7 +394,7 @@ def setUp(self):
self.pipe = mock.Mock(spec_set=io.RawIOBase)
self.pipe.fileno.return_value = 5

blocking_patcher = mock.patch('asyncio.unix_events._set_nonblocking')
blocking_patcher = mock.patch('os.set_blocking')
blocking_patcher.start()
self.addCleanup(blocking_patcher.stop)

@@ -544,7 +544,7 @@ def setUp(self):
self.pipe = mock.Mock(spec_set=io.RawIOBase)
self.pipe.fileno.return_value = 5

blocking_patcher = mock.patch('asyncio.unix_events._set_nonblocking')
blocking_patcher = mock.patch('os.set_blocking')
blocking_patcher.start()
self.addCleanup(blocking_patcher.stop)