Commit 28fca0c4 authored by Zackery Spytz's avatar Zackery Spytz Committed by Victor Stinner

bpo-37267: Do not check for FILE_TYPE_CHAR in os.dup() on Windows (GH-14051)

On Windows, os.dup() no longer creates an inheritable fd when handling a
character file.
parent 66d47da8
...@@ -3382,6 +3382,15 @@ class FDInheritanceTests(unittest.TestCase): ...@@ -3382,6 +3382,15 @@ class FDInheritanceTests(unittest.TestCase):
self.addCleanup(os.close, fd2) self.addCleanup(os.close, fd2)
self.assertEqual(os.get_inheritable(fd2), False) self.assertEqual(os.get_inheritable(fd2), False)
@unittest.skipUnless(sys.platform == 'win32', 'win32-specific test')
def test_dup_nul(self):
# os.dup() was creating inheritable fds for character files.
fd1 = os.open('NUL', os.O_RDONLY)
self.addCleanup(os.close, fd1)
fd2 = os.dup(fd1)
self.addCleanup(os.close, fd2)
self.assertFalse(os.get_inheritable(fd2))
@unittest.skipUnless(hasattr(os, 'dup2'), "need os.dup2()") @unittest.skipUnless(hasattr(os, 'dup2'), "need os.dup2()")
def test_dup2(self): def test_dup2(self):
fd = os.open(__file__, os.O_RDONLY) fd = os.open(__file__, os.O_RDONLY)
......
On Windows, :func:`os.dup` no longer creates an inheritable fd when handling
a character file.
...@@ -1776,7 +1776,6 @@ _Py_dup(int fd) ...@@ -1776,7 +1776,6 @@ _Py_dup(int fd)
{ {
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
HANDLE handle; HANDLE handle;
DWORD ftype;
#endif #endif
assert(PyGILState_Check()); assert(PyGILState_Check());
...@@ -1790,9 +1789,6 @@ _Py_dup(int fd) ...@@ -1790,9 +1789,6 @@ _Py_dup(int fd)
return -1; return -1;
} }
/* get the file type, ignore the error if it failed */
ftype = GetFileType(handle);
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
_Py_BEGIN_SUPPRESS_IPH _Py_BEGIN_SUPPRESS_IPH
fd = dup(fd); fd = dup(fd);
...@@ -1803,14 +1799,11 @@ _Py_dup(int fd) ...@@ -1803,14 +1799,11 @@ _Py_dup(int fd)
return -1; return -1;
} }
/* Character files like console cannot be make non-inheritable */ if (_Py_set_inheritable(fd, 0, NULL) < 0) {
if (ftype != FILE_TYPE_CHAR) { _Py_BEGIN_SUPPRESS_IPH
if (_Py_set_inheritable(fd, 0, NULL) < 0) { close(fd);
_Py_BEGIN_SUPPRESS_IPH _Py_END_SUPPRESS_IPH
close(fd); return -1;
_Py_END_SUPPRESS_IPH
return -1;
}
} }
#elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC) #elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC)
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment