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.
This commit is contained in:
Segev Finer 2021-04-24 01:00:27 +03:00 committed by GitHub
parent 6b59e662fa
commit 5e437fb872
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 144 additions and 125 deletions

View file

@ -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;