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 */
|