Eryk Sun <eryk...@gmail.com> added the comment:

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)
    {
        long res;

        if (self->fd < 0)
            return err_closed();
        Py_BEGIN_ALLOW_THREADS
        _Py_BEGIN_SUPPRESS_IPH
        res = isatty(self->fd);
    #ifdef MS_WINDOWS
        if (res) {
            DWORD mode;
            HANDLE h = (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_IPH
        Py_END_ALLOW_THREADS
        return PyBool_FromLong(res);
    }

----------
stage:  -> needs patch
type:  -> behavior
versions: +Python 3.10, Python 3.9 -Python 3.5, Python 3.6, Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue28654>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to