Skip to content

Commit edb7204

Browse files
gh-43414: os.get_terminal_size() now uses the actual file descriptor on Windows instead of mapping to standard handles (#93203)
1 parent 3908479 commit edb7204

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

Lib/test/test_os.py

+13
Original file line numberDiff line numberDiff line change
@@ -3665,6 +3665,19 @@ def test_stty_match(self):
36653665
raise
36663666
self.assertEqual(expected, actual)
36673667

3668+
@unittest.skipUnless(sys.platform == 'win32', 'Windows specific test')
3669+
def test_windows_fd(self):
3670+
"""Check if get_terminal_size() returns a meaningful value in Windows"""
3671+
try:
3672+
conout = open('conout$', 'w')
3673+
except OSError:
3674+
self.skipTest('failed to open conout$')
3675+
with conout:
3676+
size = os.get_terminal_size(conout.fileno())
3677+
3678+
self.assertGreaterEqual(size.columns, 0)
3679+
self.assertGreaterEqual(size.lines, 0)
3680+
36683681

36693682
@unittest.skipUnless(hasattr(os, 'memfd_create'), 'requires os.memfd_create')
36703683
@support.requires_linux_version(3, 17)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`os.get_terminal_size` now attempts to read the size from any provided
2+
handle, rather than only supporting file descriptors 0, 1 and 2.

Modules/posixmodule.c

+2-15
Original file line numberDiff line numberDiff line change
@@ -13247,24 +13247,11 @@ os_get_terminal_size_impl(PyObject *module, int fd)
1324713247

1324813248
#ifdef TERMSIZE_USE_CONIO
1324913249
{
13250-
DWORD nhandle;
1325113250
HANDLE handle;
1325213251
CONSOLE_SCREEN_BUFFER_INFO csbi;
13253-
switch (fd) {
13254-
case 0: nhandle = STD_INPUT_HANDLE;
13255-
break;
13256-
case 1: nhandle = STD_OUTPUT_HANDLE;
13257-
break;
13258-
case 2: nhandle = STD_ERROR_HANDLE;
13259-
break;
13260-
default:
13261-
return PyErr_Format(PyExc_ValueError, "bad file descriptor");
13262-
}
13263-
handle = GetStdHandle(nhandle);
13264-
if (handle == NULL)
13265-
return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
13252+
handle = _Py_get_osfhandle(fd);
1326613253
if (handle == INVALID_HANDLE_VALUE)
13267-
return PyErr_SetFromWindowsErr(0);
13254+
return NULL;
1326813255

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

0 commit comments

Comments
 (0)