[Python-checkins] bpo-37549: os.dup() fails for standard streams on Windows 7 (GH-15389)

Miss Islington (bot) webhook-mailer at python.org
Fri Aug 23 15:01:44 EDT 2019


https://github.com/python/cpython/commit/f8dc3e85ab01e1b0345738670dcb3993da7ec436
commit: f8dc3e85ab01e1b0345738670dcb3993da7ec436
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-08-23T12:01:38-07:00
summary:

bpo-37549: os.dup() fails for standard streams on Windows 7 (GH-15389)

(cherry picked from commit 5be666010e4df65dc4d831435cc92340ea369f94)

Co-authored-by: Zackery Spytz <zspytz at gmail.com>

files:
A Misc/NEWS.d/next/Windows/2019-08-22-09-04-44.bpo-37549.TpKI3M.rst
M Lib/test/test_os.py
M Python/fileutils.c

diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index 8032da053067..df4bad7a8cf7 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -3237,6 +3237,11 @@ def test_dup(self):
         self.addCleanup(os.close, fd2)
         self.assertEqual(os.get_inheritable(fd2), False)
 
+    def test_dup_standard_stream(self):
+        fd = os.dup(1)
+        self.addCleanup(os.close, fd)
+        self.assertGreater(fd, 0)
+
     @unittest.skipUnless(sys.platform == 'win32', 'win32-specific test')
     def test_dup_nul(self):
         # os.dup() was creating inheritable fds for character files.
diff --git a/Misc/NEWS.d/next/Windows/2019-08-22-09-04-44.bpo-37549.TpKI3M.rst b/Misc/NEWS.d/next/Windows/2019-08-22-09-04-44.bpo-37549.TpKI3M.rst
new file mode 100644
index 000000000000..5345da80781d
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2019-08-22-09-04-44.bpo-37549.TpKI3M.rst
@@ -0,0 +1 @@
+:func:`os.dup` no longer fails for standard streams on Windows 7.
diff --git a/Python/fileutils.c b/Python/fileutils.c
index 868fbf910312..b2c634db23bf 100644
--- a/Python/fileutils.c
+++ b/Python/fileutils.c
@@ -1037,11 +1037,18 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
         flags = HANDLE_FLAG_INHERIT;
     else
         flags = 0;
-    if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {
+
+    /* This check can be removed once support for Windows 7 ends. */
+#define CONSOLE_PSEUDOHANDLE(handle) (((ULONG_PTR)(handle) & 0x3) == 0x3 && \
+        GetFileType(handle) == FILE_TYPE_CHAR)
+
+    if (!CONSOLE_PSEUDOHANDLE(handle) &&
+        !SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {
         if (raise)
             PyErr_SetFromWindowsErr(0);
         return -1;
     }
+#undef CONSOLE_PSEUDOHANDLE
     return 0;
 
 #else



More information about the Python-checkins mailing list