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.

classification
Title: os.get_terminal_size() should use file descriptors in Windows
Type: behavior Stage: needs patch
Components: Extension Modules, Windows Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords: easy (C)

Created on 2021-03-06 01:58 by eryksun, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg388187 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-03-06 01:58
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 */
History
Date User Action Args
2022-04-11 14:59:42adminsetgithub: 87580
2021-03-06 01:58:09eryksuncreate