Skip to content

Commit 28fca0c

Browse files
ZackerySpytzvstinner
authored andcommitted
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.
1 parent 66d47da commit 28fca0c

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

Lib/test/test_os.py

+9
Original file line numberDiff line numberDiff line change
@@ -3382,6 +3382,15 @@ def test_dup(self):
33823382
self.addCleanup(os.close, fd2)
33833383
self.assertEqual(os.get_inheritable(fd2), False)
33843384

3385+
@unittest.skipUnless(sys.platform == 'win32', 'win32-specific test')
3386+
def test_dup_nul(self):
3387+
# os.dup() was creating inheritable fds for character files.
3388+
fd1 = os.open('NUL', os.O_RDONLY)
3389+
self.addCleanup(os.close, fd1)
3390+
fd2 = os.dup(fd1)
3391+
self.addCleanup(os.close, fd2)
3392+
self.assertFalse(os.get_inheritable(fd2))
3393+
33853394
@unittest.skipUnless(hasattr(os, 'dup2'), "need os.dup2()")
33863395
def test_dup2(self):
33873396
fd = os.open(__file__, os.O_RDONLY)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
On Windows, :func:`os.dup` no longer creates an inheritable fd when handling
2+
a character file.

Python/fileutils.c

+5-12
Original file line numberDiff line numberDiff line change
@@ -1776,7 +1776,6 @@ _Py_dup(int fd)
17761776
{
17771777
#ifdef MS_WINDOWS
17781778
HANDLE handle;
1779-
DWORD ftype;
17801779
#endif
17811780

17821781
assert(PyGILState_Check());
@@ -1790,9 +1789,6 @@ _Py_dup(int fd)
17901789
return -1;
17911790
}
17921791

1793-
/* get the file type, ignore the error if it failed */
1794-
ftype = GetFileType(handle);
1795-
17961792
Py_BEGIN_ALLOW_THREADS
17971793
_Py_BEGIN_SUPPRESS_IPH
17981794
fd = dup(fd);
@@ -1803,14 +1799,11 @@ _Py_dup(int fd)
18031799
return -1;
18041800
}
18051801

1806-
/* Character files like console cannot be make non-inheritable */
1807-
if (ftype != FILE_TYPE_CHAR) {
1808-
if (_Py_set_inheritable(fd, 0, NULL) < 0) {
1809-
_Py_BEGIN_SUPPRESS_IPH
1810-
close(fd);
1811-
_Py_END_SUPPRESS_IPH
1812-
return -1;
1813-
}
1802+
if (_Py_set_inheritable(fd, 0, NULL) < 0) {
1803+
_Py_BEGIN_SUPPRESS_IPH
1804+
close(fd);
1805+
_Py_END_SUPPRESS_IPH
1806+
return -1;
18141807
}
18151808
#elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC)
18161809
Py_BEGIN_ALLOW_THREADS

0 commit comments

Comments
 (0)