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

@ -1370,13 +1370,10 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
*/
if (fileno != -1 && fileno != 0) {
/* Ensure that fileno is within the CRT's valid range */
_Py_BEGIN_SUPPRESS_IPH
fh = (HANDLE)_get_osfhandle(fileno);
_Py_END_SUPPRESS_IPH
if (fh==(HANDLE)-1) {
PyErr_SetFromErrno(PyExc_OSError);
fh = _Py_get_osfhandle(fileno);
if (fh == INVALID_HANDLE_VALUE)
return NULL;
}
/* Win9x appears to need us seeked to zero */
lseek(fileno, 0, SEEK_SET);
}