[New-bugs-announce] [issue43414] os.get_terminal_size() should use file descriptors in Windows

Eryk Sun report at bugs.python.org
Fri Mar 5 20:58:09 EST 2021


New submission from Eryk Sun <eryksun at gmail.com>:

Currently os.get_terminal_size() is hard coded to use the process standard handles in Windows. The function, however, is intended for arbitrary file descriptors, so should not be limited as follows:

    >>> f = open('conout$', 'w')
    >>> os.get_terminal_size(f.fileno())
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: bad file descriptor

This is a simple fix. Rewrite it to use _get_osfhandle(). For example:

#ifdef TERMSIZE_USE_CONIO
    {
        HANDLE handle;
        CONSOLE_SCREEN_BUFFER_INFO csbi;

        _Py_BEGIN_SUPPRESS_IPH
        handle = (HANDLE)_get_osfhandle(fd);
        _Py_END_SUPPRESS_IPH
        if (handle == INVALID_HANDLE_VALUE)
            return PyErr_SetFromErrno(PyExc_OSError);

        if (!GetConsoleScreenBufferInfo(handle, &csbi))
            return PyErr_SetFromWindowsErr(0);

        columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
        lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
    }
#endif /* TERMSIZE_USE_CONIO */

----------
components: Extension Modules, Windows
keywords: easy (C)
messages: 388187
nosy: eryksun, paul.moore, steve.dower, tim.golden, zach.ware
priority: normal
severity: normal
stage: needs patch
status: open
title: os.get_terminal_size() should use file descriptors in Windows
type: behavior
versions: Python 3.10, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43414>
_______________________________________


More information about the New-bugs-announce mailing list