From: Ben Darnell Date: Fri, 26 Jun 2020 21:28:04 +0000 (-0400) Subject: platform: Remove tornado.platform.auto.set_close_exec X-Git-Tag: v6.1.0b1~18^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e050be1859c476e2158df14aefcec4036cd9c1e0;p=thirdparty%2Ftornado.git platform: Remove tornado.platform.auto.set_close_exec This function is obsolete: Since python 3.4, file descriptors created by python are non-inheritable by default (and in the event you create a file descriptor another way, a standard function os.set_inheritable is available). The windows implementation of this function was also apparently broken, but this went unnoticed because the default behavior on windows is for file descriptors to be non-inheritable. Fixes #2867 --- diff --git a/tornado/autoreload.py b/tornado/autoreload.py index af6c3d286..3299a3b34 100644 --- a/tornado/autoreload.py +++ b/tornado/autoreload.py @@ -158,8 +158,7 @@ def add_reload_hook(fn: Callable[[], None]) -> None: Note that for open file and socket handles it is generally preferable to set the ``FD_CLOEXEC`` flag (using `fcntl` or - ``tornado.platform.auto.set_close_exec``) instead - of using a reload hook to close them. + `os.set_inheritable`) instead of using a reload hook to close them. """ _reload_hooks.append(fn) diff --git a/tornado/netutil.py b/tornado/netutil.py index 6951b0f00..ee7825826 100644 --- a/tornado/netutil.py +++ b/tornado/netutil.py @@ -25,7 +25,6 @@ import stat from tornado.concurrent import dummy_executor, run_on_executor from tornado.ioloop import IOLoop -from tornado.platform.auto import set_close_exec from tornado.util import Configurable, errno_from_exception from typing import List, Callable, Any, Type, Dict, Union, Tuple, Awaitable, Optional @@ -130,7 +129,6 @@ def bind_sockets( if errno_from_exception(e) == errno.EAFNOSUPPORT: continue raise - set_close_exec(sock.fileno()) if os.name != "nt": try: sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) @@ -203,7 +201,6 @@ if hasattr(socket, "AF_UNIX"): `bind_sockets`) """ sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - set_close_exec(sock.fileno()) try: sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) except socket.error as e: @@ -276,7 +273,6 @@ def add_accept_handler( # but it was closed while still in the accept queue. # (observed on FreeBSD). continue - set_close_exec(connection.fileno()) callback(connection, address) def remove_handler() -> None: diff --git a/tornado/platform/auto.py b/tornado/platform/auto.py deleted file mode 100644 index 4f1b6ac39..000000000 --- a/tornado/platform/auto.py +++ /dev/null @@ -1,32 +0,0 @@ -# -# Copyright 2011 Facebook -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -"""Implementation of platform-specific functionality. - -For each function or class described in `tornado.platform.interface`, -the appropriate platform-specific implementation exists in this module. -Most code that needs access to this functionality should do e.g.:: - - from tornado.platform.auto import set_close_exec -""" - -import os - -if os.name == "nt": - from tornado.platform.windows import set_close_exec -else: - from tornado.platform.posix import set_close_exec - -__all__ = ["set_close_exec"] diff --git a/tornado/platform/auto.pyi b/tornado/platform/auto.pyi deleted file mode 100644 index a1c97228a..000000000 --- a/tornado/platform/auto.pyi +++ /dev/null @@ -1,4 +0,0 @@ -# auto.py is full of patterns mypy doesn't like, so for type checking -# purposes we replace it with interface.py. - -from .interface import * diff --git a/tornado/platform/interface.py b/tornado/platform/interface.py deleted file mode 100644 index 11afdb186..000000000 --- a/tornado/platform/interface.py +++ /dev/null @@ -1,26 +0,0 @@ -# -# Copyright 2011 Facebook -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -"""Interfaces for platform-specific functionality. - -This module exists primarily for documentation purposes and as base classes -for other tornado.platform modules. Most code should import the appropriate -implementation from `tornado.platform.auto`. -""" - - -def set_close_exec(fd: int) -> None: - """Sets the close-on-exec bit (``FD_CLOEXEC``)for a file descriptor.""" - raise NotImplementedError() diff --git a/tornado/platform/posix.py b/tornado/platform/posix.py index 0ab27ca85..e4699a6fc 100644 --- a/tornado/platform/posix.py +++ b/tornado/platform/posix.py @@ -19,11 +19,6 @@ import fcntl import os -def set_close_exec(fd: int) -> None: - flags = fcntl.fcntl(fd, fcntl.F_GETFD) - fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC) - - def _set_nonblocking(fd: int) -> None: flags = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK) diff --git a/tornado/platform/windows.py b/tornado/platform/windows.py deleted file mode 100644 index ddbde20cc..000000000 --- a/tornado/platform/windows.py +++ /dev/null @@ -1,22 +0,0 @@ -# NOTE: win32 support is currently experimental, and not recommended -# for production use. - -import ctypes -import ctypes.wintypes - -# See: http://msdn.microsoft.com/en-us/library/ms724935(VS.85).aspx -SetHandleInformation = ctypes.windll.kernel32.SetHandleInformation # type: ignore -SetHandleInformation.argtypes = ( - ctypes.wintypes.HANDLE, - ctypes.wintypes.DWORD, - ctypes.wintypes.DWORD, -) -SetHandleInformation.restype = ctypes.wintypes.BOOL - -HANDLE_FLAG_INHERIT = 0x00000001 - - -def set_close_exec(fd: int) -> None: - success = SetHandleInformation(fd, HANDLE_FLAG_INHERIT, 0) - if not success: - raise ctypes.WinError() # type: ignore diff --git a/tornado/process.py b/tornado/process.py index 90f6bdd0d..26428feb7 100644 --- a/tornado/process.py +++ b/tornado/process.py @@ -34,10 +34,9 @@ from tornado.concurrent import ( from tornado import ioloop from tornado.iostream import PipeIOStream from tornado.log import gen_log -from tornado.platform.auto import set_close_exec import typing -from typing import Tuple, Optional, Any, Callable +from typing import Optional, Any, Callable if typing.TYPE_CHECKING: from typing import List # noqa: F401 @@ -77,13 +76,6 @@ def _reseed_random() -> None: random.seed(seed) -def _pipe_cloexec() -> Tuple[int, int]: - r, w = os.pipe() - set_close_exec(r) - set_close_exec(w) - return r, w - - _task_id = None @@ -227,19 +219,19 @@ class Subprocess(object): pipe_fds = [] # type: List[int] to_close = [] # type: List[int] if kwargs.get("stdin") is Subprocess.STREAM: - in_r, in_w = _pipe_cloexec() + in_r, in_w = os.pipe() kwargs["stdin"] = in_r pipe_fds.extend((in_r, in_w)) to_close.append(in_r) self.stdin = PipeIOStream(in_w) if kwargs.get("stdout") is Subprocess.STREAM: - out_r, out_w = _pipe_cloexec() + out_r, out_w = os.pipe() kwargs["stdout"] = out_w pipe_fds.extend((out_r, out_w)) to_close.append(out_w) self.stdout = PipeIOStream(out_r) if kwargs.get("stderr") is Subprocess.STREAM: - err_r, err_w = _pipe_cloexec() + err_r, err_w = os.pipe() kwargs["stderr"] = err_w pipe_fds.extend((err_r, err_w)) to_close.append(err_w) diff --git a/tornado/tcpclient.py b/tornado/tcpclient.py index 45d721db6..e2d682ea6 100644 --- a/tornado/tcpclient.py +++ b/tornado/tcpclient.py @@ -27,7 +27,6 @@ from tornado.ioloop import IOLoop from tornado.iostream import IOStream from tornado import gen from tornado.netutil import Resolver -from tornado.platform.auto import set_close_exec from tornado.gen import TimeoutError from typing import Any, Union, Dict, Tuple, List, Callable, Iterator, Optional, Set @@ -311,7 +310,6 @@ class TCPClient(object): # - 127.0.0.1 for IPv4 # - ::1 for IPv6 socket_obj = socket.socket(af) - set_close_exec(socket_obj.fileno()) if source_port_bind or source_ip_bind: # If the user requires binding also to a specific IP/port. try: diff --git a/tornado/test/runtests.py b/tornado/test/runtests.py index 9156be701..43cdf4d91 100644 --- a/tornado/test/runtests.py +++ b/tornado/test/runtests.py @@ -50,7 +50,6 @@ TEST_MODULES = [ "tornado.test.util_test", "tornado.test.web_test", "tornado.test.websocket_test", - "tornado.test.windows_test", "tornado.test.wsgi_test", ] diff --git a/tornado/test/windows_test.py b/tornado/test/windows_test.py deleted file mode 100644 index 6fe73270f..000000000 --- a/tornado/test/windows_test.py +++ /dev/null @@ -1,24 +0,0 @@ -import functools -import os -import socket -import unittest - -from tornado.platform.auto import set_close_exec - -skipIfNonWindows = unittest.skipIf(os.name != "nt", "non-windows platform") - - -@skipIfNonWindows -class WindowsTest(unittest.TestCase): - def test_set_close_exec(self): - # set_close_exec works with sockets. - s = socket.socket() - self.addCleanup(s.close) - set_close_exec(s.fileno()) - - # But it doesn't work with pipes. - r, w = os.pipe() - self.addCleanup(functools.partial(os.close, r)) - self.addCleanup(functools.partial(os.close, w)) - with self.assertRaises(WindowsError): - set_close_exec(r)