This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eryksun
Recipients eryksun, matejcik, paul.moore, steve.dower, tim.golden, zach.ware
Date 2021-07-29.19:15:41
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1627586141.37.0.683340419086.issue44762@roundup.psfhosted.org>
In-reply-to
Content
> We could also provide a better check in WindowsConsoleIO.isatty, 

For isatty(), my concern is a false positive if the file is the "NUL" device, which should be an io.FileIO object. I suppose checking the file type in io._WindowsConsoleIO.isatty() could be useful in case the file descriptor gets reassigned to a non-console file (e.g. via os.close or os.dup2). 

I'd prefer to implement the check in a common _Py_isatty() function that supports the optional errno values EBADF and ENOTTY [1]. For example:

    int
    _Py_isatty(int fd)
    {
        DWORD mode;
        HANDLE handle = _Py_get_osfhandle_noraise(fd);

        if (handle == INVALID_HANDLE_VALUE) {
            errno = EBADF;
            return 0;
        }

        switch (GetFileType(handle)) {
        case FILE_TYPE_CHAR:
            break;
        case FILE_TYPE_DISK:
        case FILE_TYPE_PIPE:
            errno = ENOTTY;
            return 0;
        default:
            errno = EBADF;
            return 0;
        }
        
        if (!GetConsoleMode(handle, &mode) && 
              GetLastError() != ERROR_ACCESS_DENIED) {
            errno = ENOTTY;
            return 0;
        }
        
        return 1;
    }

---

[1] https://pubs.opengroup.org/onlinepubs/9699919799/functions/isatty.html
History
Date User Action Args
2021-07-29 19:15:41eryksunsetrecipients: + eryksun, paul.moore, tim.golden, matejcik, zach.ware, steve.dower
2021-07-29 19:15:41eryksunsetmessageid: <1627586141.37.0.683340419086.issue44762@roundup.psfhosted.org>
2021-07-29 19:15:41eryksunlinkissue44762 messages
2021-07-29 19:15:41eryksuncreate