Release the GIL while performing isatty() system calls on arbitrary
file descriptors. In particular, this affects os.isatty(),
os.device_encoding() and io.TextIOWrapper. By extension,
io.open() in text mode is also affected.
(cherry picked from commit
06148b1870fceb1a21738761b8e1ac3bf654319b)
Co-authored-by: Vincent Michel <vxgmichel@gmail.com>
--- /dev/null
+Release the GIL while performing ``isatty`` system calls on arbitrary file
+descriptors. In particular, this affects :func:`os.isatty`,
+:func:`os.device_encoding` and :class:`io.TextIOWrapper`. By extension,
+:func:`io.open` in text mode is also affected.
/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
{
int return_value;
+ Py_BEGIN_ALLOW_THREADS
_Py_BEGIN_SUPPRESS_IPH
return_value = isatty(fd);
_Py_END_SUPPRESS_IPH
+ Py_END_ALLOW_THREADS
return return_value;
}
UINT cp;
#endif
int valid;
+ Py_BEGIN_ALLOW_THREADS
_Py_BEGIN_SUPPRESS_IPH
valid = isatty(fd);
_Py_END_SUPPRESS_IPH
+ Py_END_ALLOW_THREADS
if (!valid)
Py_RETURN_NONE;
_Py_BEGIN_SUPPRESS_IPH
#ifdef MS_WINDOWS
- if (count > 32767 && isatty(fd)) {
+ if (count > 32767) {
/* Issue #11395: the Windows console returns an error (12: not
enough space error) on writing into stdout if stdout mode is
binary and the length is greater than 66,000 bytes (or less,
depending on heap usage). */
- count = 32767;
+ if (gil_held) {
+ Py_BEGIN_ALLOW_THREADS
+ if (isatty(fd)) {
+ count = 32767;
+ }
+ Py_END_ALLOW_THREADS
+ } else {
+ if (isatty(fd)) {
+ count = 32767;
+ }
+ }
}
#endif
if (count > _PY_WRITE_MAX) {