There was an error while loading. Please reload this page.
os.get_terminal_size
isatty
ioctl
1 parent 49f9667 commit 51ae3c0Copy full SHA for 51ae3c0
3 files changed
Lib/test/test_os/test_os.py
@@ -3970,12 +3970,7 @@ def test_does_not_crash(self):
3970
try:
3971
size = os.get_terminal_size()
3972
except OSError as e:
3973
- known_errnos = [errno.EINVAL, errno.ENOTTY]
3974
- if sys.platform == "android":
3975
- # The Android testbed redirects the native stdout to a pipe,
3976
- # which returns a different error code.
3977
- known_errnos.append(errno.EACCES)
3978
- if sys.platform == "win32" or e.errno in known_errnos:
+ if sys.platform == "win32" or e.errno in (errno.EINVAL, errno.ENOTTY):
3979
# Under win32 a generic OSError can be thrown if the
3980
# handle cannot be retrieved
3981
self.skipTest("failed to query terminal size")
Misc/NEWS.d/next/Library/2026-07-29-16-53-50.gh-issue-154885.ptofmI.rst
@@ -0,0 +1,2 @@
1
+:func:`os.get_terminal_size` now checks ``isatty`` before calling ``ioctl``,
2
+which reduces log noise on Android.
Modules/posixmodule.c
@@ -15971,6 +15971,13 @@ os_get_terminal_size_impl(PyObject *module, int fd)
15971
15972
#ifdef TERMSIZE_USE_IOCTL
15973
{
15974
+ // On Android, stdout is probably not connected, and calling TIOCGWINSZ
15975
+ // on an invalid file descriptor causes a log message "avc: denied {
15976
+ // ioctl }". Some common tools such as pytest call get_terminal_size
15977
+ // very often, so check it's a TTY first to avoid cluttering the log.
15978
+ if (!isatty(fd))
15979
+ return PyErr_SetFromErrno(PyExc_OSError);
15980
+
15981
struct winsize w;
15982
if (ioctl(fd, TIOCGWINSZ, &w))
15983
return PyErr_SetFromErrno(PyExc_OSError);
0 commit comments