summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2014-10-02 18:09:05 +0200
committerVictor Stinner <victor.stinner@gmail.com>2014-10-02 18:09:05 +0200
commitac90e8d0c5d2619afb445a68f9d9540124ef5c10 (patch)
tree4f2c0461b27c76368a700092c4f4ce65954b394a
parent7df260f1b738fbd4713ec0e29f46834392e45e8c (diff)
downloadtrollius-ac90e8d0c5d2619afb445a68f9d9540124ef5c10.tar.gz
Fix unix_events for Python 3.5
Use os.set_inheritable() if available. Change the test to decide if the fcntl module must be imported.
-rw-r--r--trollius/unix_events.py27
1 files changed, 17 insertions, 10 deletions
diff --git a/trollius/unix_events.py b/trollius/unix_events.py
index aed0777..2d833d6 100644
--- a/trollius/unix_events.py
+++ b/trollius/unix_events.py
@@ -9,6 +9,8 @@ import stat
import subprocess
import sys
import threading
+if not hasattr(os, 'set_blocking') or not hasattr(os, 'set_inheritable'):
+ import fcntl
from . import base_events
@@ -287,11 +289,10 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop):
if hasattr(os, 'set_blocking'):
+ # Python 3.5 and newer
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
@@ -569,14 +570,18 @@ class _UnixWritePipeTransport(transports._FlowControlMixin,
self._loop = None
-def _set_cloexec_flag(fd, cloexec):
- cloexec_flag = getattr(fcntl, 'FD_CLOEXEC', 1)
+if hasattr(os, 'set_inheritable'):
+ # Python 3.4 and newer
+ _set_inheritable = os.set_inheritable
+else:
+ def _set_inheritable(fd, inheritable):
+ cloexec_flag = getattr(fcntl, 'FD_CLOEXEC', 1)
- old = fcntl.fcntl(fd, fcntl.F_GETFD)
- if cloexec:
- fcntl.fcntl(fd, fcntl.F_SETFD, old | cloexec_flag)
- else:
- fcntl.fcntl(fd, fcntl.F_SETFD, old & ~cloexec_flag)
+ 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):
@@ -590,7 +595,9 @@ class _UnixSubprocessTransport(base_subprocess.BaseSubprocessTransport):
# other end). Notably this is needed on AIX, and works
# just fine on other platforms.
stdin, stdin_w = self._loop._socketpair()
- _set_cloexec_flag(stdin_w.fileno(), True)
+
+ # Mark the write end of the stdin pipe as non-inheritable
+ _set_inheritable(stdin_w.fileno(), False)
self._proc = subprocess.Popen(
args, shell=shell, stdin=stdin, stdout=stdout, stderr=stderr,
universal_newlines=False, bufsize=bufsize, **kwargs)