bpo-30555: Fix WindowsConsoleIO fails in the presence of fd redirection (GH-1927)
This works by not caching the handle and instead getting the handle from
the file descriptor each time, so that if the actual handle changes by
fd redirection closing/opening the console handle beneath our feet, we
will keep working correctly.
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index e754db7..ecd210e 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -10098,18 +10098,16 @@ os_pipe_impl(PyObject *module)
attr.bInheritHandle = FALSE;
Py_BEGIN_ALLOW_THREADS
- _Py_BEGIN_SUPPRESS_IPH
ok = CreatePipe(&read, &write, &attr, 0);
if (ok) {
- fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
- fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
+ fds[0] = _Py_open_osfhandle_noraise(read, _O_RDONLY);
+ fds[1] = _Py_open_osfhandle_noraise(write, _O_WRONLY);
if (fds[0] == -1 || fds[1] == -1) {
CloseHandle(read);
CloseHandle(write);
ok = 0;
}
}
- _Py_END_SUPPRESS_IPH
Py_END_ALLOW_THREADS
if (!ok)