You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
sys.stdout.isatty() returns True even if redirected to NUL #72840
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2016-11-10.08:18:38.364>labels= ['3.10', 'type-bug', 'expert-IO', 'OS-windows', '3.9']
title='sys.stdout.isatty() returns True even if redirected to NUL'updated_at=<Date2021-02-25.14:05:07.685>user='https://github.com/kunom'
Windows doesn't have terminal devices, so the C runtime's isatty() function just checks for a character device, which includes NUL among others.
This can lead to hanging the REPL, albeit with a contrived example such as redirecting stdin to COM3:
C:\>python < COM3
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55)
[MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> ^C
Ctrl+C doesn't work because the main thread is blocked (we'd have to call CancelSynchronousIo in the C signal handler). I killed the process using the default Ctrl+Break handler. "^C" was printed by the cmd shell's Ctrl+Break handler.
I don't think the CRT's check for a character device is generally useful. It could be replaced with a check that specifically looks for a console handle (e.g. by calling GetConsoleMode), which is what someone calling isatty() generally wants to know.
Here's an example of how to change the isatty() method in Modules/_io/fileio.c to check for a console in Windows:
_io_FileIO_isatty_impl(fileio*self)
{
longres;
if (self->fd<0)
returnerr_closed();
Py_BEGIN_ALLOW_THREADS_Py_BEGIN_SUPPRESS_IPHres=isatty(self->fd);
#ifdefMS_WINDOWSif (res) {
DWORDmode;
HANDLEh= (HANDLE)_get_osfhandle(self->fd);
// It's a console if GetConsoleMode succeeds or if the last error// isn't ERROR_INVALID_HANDLE. e.g., if 'con' is opened with 'w'// mode, the error is ERROR_ACCESS_DENIED.res=GetConsoleMode(h, &mode) ||GetLastError() !=ERROR_INVALID_HANDLE;
}
#endif_Py_END_SUPPRESS_IPHPy_END_ALLOW_THREADSreturnPyBool_FromLong(res);
}
Activity
kunom commentedon Nov 10, 2016
[Python 3.5.2 on Windows]
NUL is the Windows equivalent of /dev/nul, so the result should probably be False.
Under Cygwin, the result is as expected:
eryksun commentedon Nov 10, 2016
Windows doesn't have terminal devices, so the C runtime's isatty() function just checks for a character device, which includes NUL among others.
This can lead to hanging the REPL, albeit with a contrived example such as redirecting stdin to COM3:
C:\>python < COM3 Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> ^CCtrl+C doesn't work because the main thread is blocked (we'd have to call CancelSynchronousIo in the C signal handler). I killed the process using the default Ctrl+Break handler. "^C" was printed by the cmd shell's Ctrl+Break handler.
I don't think the CRT's check for a character device is generally useful. It could be replaced with a check that specifically looks for a console handle (e.g. by calling GetConsoleMode), which is what someone calling isatty() generally wants to know.
kunom commentedon Nov 10, 2016
http://stackoverflow.com/questions/3648711/detect-nul-file-descriptor-isatty-is-bogus, although a bit convoluted, might also help. It mentions GetConsoleMode() for stdin and GetConsoleScreenBufferInfo() for stdout.
socketpair commentedon Nov 10, 2016
> /dev/nulis a bug, it should be/dev/nulldoes not it ?kunom commentedon Nov 11, 2016
> /dev/nullinstead of> /dev/nul(my bad). The output remains the same.eryksun commentedon Feb 25, 2021
Here's an example of how to change the isatty() method in Modules/_io/fileio.c to check for a console in Windows: