Skip to content

Commit

Permalink
bpo-30555: Fix WindowsConsoleIO fails in the presence of fd redirecti…
Browse files Browse the repository at this point in the history
…on (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.
  • Loading branch information
segevfiner authored Apr 23, 2021
1 parent 6b59e66 commit 5e437fb
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 125 deletions.
10 changes: 9 additions & 1 deletion Include/cpython/fileutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,12 @@ PyAPI_FUNC(int) _Py_dup(int fd);
PyAPI_FUNC(int) _Py_get_blocking(int fd);

PyAPI_FUNC(int) _Py_set_blocking(int fd, int blocking);
#endif /* !MS_WINDOWS */
#else /* MS_WINDOWS */
PyAPI_FUNC(void*) _Py_get_osfhandle_noraise(int fd);

PyAPI_FUNC(void*) _Py_get_osfhandle(int fd);

PyAPI_FUNC(int) _Py_open_osfhandle_noraise(void *handle, int flags);

PyAPI_FUNC(int) _Py_open_osfhandle(void *handle, int flags);
#endif /* MS_WINDOWS */
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix ``WindowsConsoleIO`` errors in the presence of fd redirection. Patch by
Segev Finer.
13 changes: 5 additions & 8 deletions Modules/_io/clinic/winconsoleio.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ PyDoc_STRVAR(_io__WindowsConsoleIO_close__doc__,
"close($self, /)\n"
"--\n"
"\n"
"Close the handle.\n"
"Close the console object.\n"
"\n"
"A closed handle cannot be used for further I/O operations. close() may be\n"
"called more than once without error.");
"A closed console object cannot be used for further I/O operations.\n"
"close() may be called more than once without error.");

#define _IO__WINDOWSCONSOLEIO_CLOSE_METHODDEF \
{"close", (PyCFunction)_io__WindowsConsoleIO_close, METH_NOARGS, _io__WindowsConsoleIO_close__doc__},
Expand Down Expand Up @@ -110,10 +110,7 @@ PyDoc_STRVAR(_io__WindowsConsoleIO_fileno__doc__,
"fileno($self, /)\n"
"--\n"
"\n"
"Return the underlying file descriptor (an integer).\n"
"\n"
"fileno is only set when a file descriptor is used to open\n"
"one of the standard streams.");
"Return the underlying file descriptor (an integer).");

#define _IO__WINDOWSCONSOLEIO_FILENO_METHODDEF \
{"fileno", (PyCFunction)_io__WindowsConsoleIO_fileno, METH_NOARGS, _io__WindowsConsoleIO_fileno__doc__},
Expand Down Expand Up @@ -381,4 +378,4 @@ _io__WindowsConsoleIO_isatty(winconsoleio *self, PyObject *Py_UNUSED(ignored))
#ifndef _IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF
#define _IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF
#endif /* !defined(_IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF) */
/*[clinic end generated code: output=a28b3120fa53b256 input=a9049054013a1b77]*/
/*[clinic end generated code: output=48080572ffee22f5 input=a9049054013a1b77]*/
Loading

0 comments on commit 5e437fb

Please sign in to comment.