https://github.com/python/cpython/commit/cfd8e2ac49339330040459504d4acf374c38a5cb
commit: cfd8e2ac49339330040459504d4acf374c38a5cb
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: freakboy3742 <[email protected]>
date: 2026-07-30T02:39:42Z
summary:

[3.15] Make `os.get_terminal_size` check `isatty` before calling `ioctl` 
(GH-154885) (#154903)

Calling ioctl on stdout raises warnings on Android. Ensure we have a TTY
before doing terminal size calls.
(cherry picked from commit 51ae3c0ee585e2bc56d0a58c619eefa1b4b4f929)

Co-authored-by: Malcolm Smith <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-07-29-16-53-50.gh-issue-154885.ptofmI.rst
M Lib/test/test_os/test_os.py
M Modules/posixmodule.c

diff --git a/Lib/test/test_os/test_os.py b/Lib/test/test_os/test_os.py
index c6d20447086ae45..2c2b856086dabf2 100644
--- a/Lib/test/test_os/test_os.py
+++ b/Lib/test/test_os/test_os.py
@@ -3966,12 +3966,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/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.
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index e72c2258faa2ce4..01195018b2e4f66 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -15937,6 +15937,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);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to