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/PC/msvcrtmodule.c b/PC/msvcrtmodule.c
index b7ff20a..0591497 100644
--- a/PC/msvcrtmodule.c
+++ b/PC/msvcrtmodule.c
@@ -177,19 +177,11 @@ static long
msvcrt_open_osfhandle_impl(PyObject *module, void *handle, int flags)
/*[clinic end generated code: output=b2fb97c4b515e4e6 input=d5db190a307cf4bb]*/
{
- int fd;
-
if (PySys_Audit("msvcrt.open_osfhandle", "Ki", handle, flags) < 0) {
return -1;
}
- _Py_BEGIN_SUPPRESS_IPH
- fd = _open_osfhandle((intptr_t)handle, flags);
- _Py_END_SUPPRESS_IPH
- if (fd == -1)
- PyErr_SetFromErrno(PyExc_OSError);
-
- return fd;
+ return _Py_open_osfhandle(handle, flags);
}
/*[clinic input]
@@ -207,19 +199,11 @@ static void *
msvcrt_get_osfhandle_impl(PyObject *module, int fd)
/*[clinic end generated code: output=aca01dfe24637374 input=5fcfde9b17136aa2]*/
{
- intptr_t handle = -1;
-
if (PySys_Audit("msvcrt.get_osfhandle", "(i)", fd) < 0) {
return NULL;
}
- _Py_BEGIN_SUPPRESS_IPH
- handle = _get_osfhandle(fd);
- _Py_END_SUPPRESS_IPH
- if (handle == -1)
- PyErr_SetFromErrno(PyExc_OSError);
-
- return (HANDLE)handle;
+ return _Py_get_osfhandle(fd);
}
/* Console I/O */