Skip to content

Commit

Permalink
bpo-37549: os.dup() fails for standard streams on Windows 7 (pythonGH…
Browse files Browse the repository at this point in the history
  • Loading branch information
ZackerySpytz authored and zooba committed Aug 23, 2019
1 parent 8f080b0 commit 5be6660
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -3476,6 +3476,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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:func:`os.dup` no longer fails for standard streams on Windows 7.
9 changes: 8 additions & 1 deletion Python/fileutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1134,11 +1134,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
Expand Down

0 comments on commit 5be6660

Please sign in to comment.