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-37267: Do not check for FILE_TYPE_CHAR in os.dup() on Windows #14051

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -3382,6 +3382,15 @@ def test_dup(self):
self.addCleanup(os.close, fd2)
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()")
def test_dup2(self):
fd = os.open(__file__, os.O_RDONLY)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
On Windows, :func:`os.dup` no longer creates an inheritable fd when handling
a character file.
17 changes: 5 additions & 12 deletions Python/fileutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1776,7 +1776,6 @@ _Py_dup(int fd)
{
#ifdef MS_WINDOWS
HANDLE handle;
DWORD ftype;
#endif

assert(PyGILState_Check());
Expand All @@ -1790,9 +1789,6 @@ _Py_dup(int fd)
return -1;
}

/* get the file type, ignore the error if it failed */
ftype = GetFileType(handle);

Py_BEGIN_ALLOW_THREADS
_Py_BEGIN_SUPPRESS_IPH
fd = dup(fd);
Expand All @@ -1803,14 +1799,11 @@ _Py_dup(int fd)
return -1;
}

/* Character files like console cannot be make non-inheritable */
if (ftype != FILE_TYPE_CHAR) {
if (_Py_set_inheritable(fd, 0, NULL) < 0) {
_Py_BEGIN_SUPPRESS_IPH
close(fd);
_Py_END_SUPPRESS_IPH
return -1;
}
if (_Py_set_inheritable(fd, 0, NULL) < 0) {
_Py_BEGIN_SUPPRESS_IPH
close(fd);
_Py_END_SUPPRESS_IPH
return -1;
}
#elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC)
Py_BEGIN_ALLOW_THREADS
Expand Down