diff options
author | Segev Finer <segev208@gmail.com> | 2021-04-23 22:00:27 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-23 22:00:27 (GMT) |
commit | 5e437fb872279960992c9a07f1a4c051b4948c53 (patch) | |
tree | da3e3ad584eba24da9cfe63c4acd1a01e55d4676 /PC | |
parent | 6b59e662fa39a356d6eb03d349b140477857f4b1 (diff) | |
download | cpython-5e437fb872279960992c9a07f1a4c051b4948c53.zip cpython-5e437fb872279960992c9a07f1a4c051b4948c53.tar.gz cpython-5e437fb872279960992c9a07f1a4c051b4948c53.tar.bz2 |
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.
Diffstat (limited to 'PC')
-rw-r--r-- | PC/_testconsole.c | 9 | ||||
-rw-r--r-- | PC/msvcrtmodule.c | 20 |
2 files changed, 8 insertions, 21 deletions
diff --git a/PC/_testconsole.c b/PC/_testconsole.c index b62f21c..db84f73 100644 --- a/PC/_testconsole.c +++ b/PC/_testconsole.c @@ -13,10 +13,10 @@ #include <fcntl.h> /* The full definition is in iomodule. We reproduce - enough here to get the handle, which is all we want. */ + enough here to get the fd, which is all we want. */ typedef struct { PyObject_HEAD - HANDLE handle; + int fd; } winconsoleio; @@ -67,7 +67,10 @@ _testconsole_write_input_impl(PyObject *module, PyObject *file, prec->Event.KeyEvent.uChar.UnicodeChar = *p; } - HANDLE hInput = ((winconsoleio*)file)->handle; + HANDLE hInput = _Py_get_osfhandle(((winconsoleio*)file)->fd); + if (hInput == INVALID_HANDLE_VALUE) + goto error; + DWORD total = 0; while (total < size) { DWORD wrote; 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 */ |