From d018cbea0ee9cb4703ac78b901937aeb8212e810 Mon Sep 17 00:00:00 2001 From: Malcolm Smith Date: Wed, 29 Jul 2026 16:49:25 +0100 Subject: [PATCH 1/2] Make `os.get_terminal_size` check `isatty` before `ioctl` --- Lib/test/test_os/test_os.py | 7 +------ Modules/posixmodule.c | 7 +++++++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_os/test_os.py b/Lib/test/test_os/test_os.py index 3e5ad52c4ab130d..328a0dbeb99f8fa 100644 --- a/Lib/test/test_os/test_os.py +++ b/Lib/test/test_os/test_os.py @@ -3970,12 +3970,7 @@ def test_does_not_crash(self): try: size = os.get_terminal_size() except OSError as e: - known_errnos = [errno.EINVAL, errno.ENOTTY] - if sys.platform == "android": - # The Android testbed redirects the native stdout to a pipe, - # which returns a different error code. - known_errnos.append(errno.EACCES) - if sys.platform == "win32" or e.errno in known_errnos: + if sys.platform == "win32" or e.errno in (errno.EINVAL, errno.ENOTTY): # Under win32 a generic OSError can be thrown if the # handle cannot be retrieved self.skipTest("failed to query terminal size") diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index f754d0e18b5fb09..c34e3fc5eb600df 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -15971,6 +15971,13 @@ os_get_terminal_size_impl(PyObject *module, int fd) #ifdef TERMSIZE_USE_IOCTL { + // On Android, stdout is probably not connected, and calling TIOCGWINSZ + // on an invalid file descriptor causes a log message "avc: denied { + // ioctl }". Some common tools such as pytest call get_terminal_size + // very often, so check it's a TTY first to avoid cluttering the log. + if (!isatty(fd)) + return PyErr_SetFromErrno(PyExc_OSError); + struct winsize w; if (ioctl(fd, TIOCGWINSZ, &w)) return PyErr_SetFromErrno(PyExc_OSError); From b48044c7b0f76cc89b9d38f5d7ff1289ba6d7474 Mon Sep 17 00:00:00 2001 From: Malcolm Smith Date: Wed, 29 Jul 2026 17:16:43 +0100 Subject: [PATCH 2/2] Add news file --- .../next/Library/2026-07-29-16-53-50.gh-issue-154885.ptofmI.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-07-29-16-53-50.gh-issue-154885.ptofmI.rst diff --git a/Misc/NEWS.d/next/Library/2026-07-29-16-53-50.gh-issue-154885.ptofmI.rst b/Misc/NEWS.d/next/Library/2026-07-29-16-53-50.gh-issue-154885.ptofmI.rst new file mode 100644 index 000000000000000..7c3839ad1a59e4f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-29-16-53-50.gh-issue-154885.ptofmI.rst @@ -0,0 +1,2 @@ +:func:`os.get_terminal_size` now checks ``isatty`` before calling ``ioctl``, +which reduces log noise on Android.