Skip to content

Commit 5be6660

Browse files
ZackerySpytzzooba
authored andcommitted
bpo-37549: os.dup() fails for standard streams on Windows 7 (GH-15389)
1 parent 8f080b0 commit 5be6660

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

Lib/test/test_os.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3476,6 +3476,11 @@ def test_dup(self):
34763476
self.addCleanup(os.close, fd2)
34773477
self.assertEqual(os.get_inheritable(fd2), False)
34783478

3479+
def test_dup_standard_stream(self):
3480+
fd = os.dup(1)
3481+
self.addCleanup(os.close, fd)
3482+
self.assertGreater(fd, 0)
3483+
34793484
@unittest.skipUnless(sys.platform == 'win32', 'win32-specific test')
34803485
def test_dup_nul(self):
34813486
# os.dup() was creating inheritable fds for character files.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:func:`os.dup` no longer fails for standard streams on Windows 7.

Python/fileutils.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1134,11 +1134,18 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
11341134
flags = HANDLE_FLAG_INHERIT;
11351135
else
11361136
flags = 0;
1137-
if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {
1137+
1138+
/* This check can be removed once support for Windows 7 ends. */
1139+
#define CONSOLE_PSEUDOHANDLE(handle) (((ULONG_PTR)(handle) & 0x3) == 0x3 && \
1140+
GetFileType(handle) == FILE_TYPE_CHAR)
1141+
1142+
if (!CONSOLE_PSEUDOHANDLE(handle) &&
1143+
!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {
11381144
if (raise)
11391145
PyErr_SetFromWindowsErr(0);
11401146
return -1;
11411147
}
1148+
#undef CONSOLE_PSEUDOHANDLE
11421149
return 0;
11431150

11441151
#else

0 commit comments

Comments
 (0)