Skip to content

sys.stdout.isatty() returns True even if redirected to NUL #72840

Open

Description

@kunom
mannequin
BPO 28654
Nosy @pfmoore, @tjguk, @socketpair, @zware, @eryksun, @zooba, @kunom

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 = None
closed_at = None
created_at = <Date 2016-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 = <Date 2021-02-25.14:05:07.685>
user = 'https://github.com/kunom'

bugs.python.org fields:

activity = <Date 2021-02-25.14:05:07.685>
actor = 'eryksun'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Windows', 'IO']
creation = <Date 2016-11-10.08:18:38.364>
creator = 'kmeyer'
dependencies = []
files = []
hgrepos = []
issue_num = 28654
keywords = []
message_count = 6.0
messages = ['280494', '280498', '280504', '280508', '280564', '387673']
nosy_count = 7.0
nosy_names = ['paul.moore', 'tim.golden', 'socketpair', 'zach.ware', 'eryksun', 'steve.dower', 'kmeyer']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'needs patch'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue28654'
versions = ['Python 3.9', 'Python 3.10']

Activity

  1. kunom commented on Nov 10, 2016

    kunommannequin
    MannequinAuthor

    [Python 3.5.2 on Windows]

    py -c "import sys; print(sys.stdout.isatty(), file=sys.stderr)" > NUL
    True

    NUL is the Windows equivalent of /dev/nul, so the result should probably be False.

    Under Cygwin, the result is as expected:

    $ python3 -c "import sys; print(sys.stdout.isatty(), file=sys.stderr)" > /dev/nul
    False
  2. eryksun commented on Nov 10, 2016

    @eryksun
    Contributor

    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.

  3. kunom commented on Nov 10, 2016

    kunommannequin
    MannequinAuthor
  4. socketpair commented on Nov 10, 2016

    socketpairmannequin
    Mannequin
    1. I think, that is not a bug.
    2. It seems, that for Cygwin, > /dev/nul is a bug, it should be /dev/null does not it ?
  5. kunom commented on Nov 11, 2016

    kunommannequin
    MannequinAuthor
    1. Yes, it should be > /dev/null instead of > /dev/nul (my bad). The output remains the same.
  6. eryksun commented on Feb 25, 2021

    @eryksun
    Contributor

    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);
        }
  7. added
    3.9 (EOL)
    3.10 (EOL)
    type-bug
    and removed
    3.7 (EOL)
    on Feb 25, 2021
  8. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.10 (EOL)3.9 (EOL)OS-windowstopic-IOtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions