summaryrefslogtreecommitdiffstats
path: root/PC/_testconsole.c
diff options
context:
space:
mode:
authorSegev Finer <segev208@gmail.com>2021-04-23 22:00:27 (GMT)
committerGitHub <noreply@github.com>2021-04-23 22:00:27 (GMT)
commit5e437fb872279960992c9a07f1a4c051b4948c53 (patch)
treeda3e3ad584eba24da9cfe63c4acd1a01e55d4676 /PC/_testconsole.c
parent6b59e662fa39a356d6eb03d349b140477857f4b1 (diff)
downloadcpython-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/_testconsole.c')
-rw-r--r--PC/_testconsole.c9
1 files changed, 6 insertions, 3 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;