]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Make `os.get_terminal_size` check `isatty` before calling `ioctl` (#154885)
authorMalcolm Smith <smith@chaquo.com>
Thu, 30 Jul 2026 02:04:46 +0000 (03:04 +0100)
committerGitHub <noreply@github.com>
Thu, 30 Jul 2026 02:04:46 +0000 (02:04 +0000)
Calling ioctl on stdout raises warnings on Android. Ensure we have a TTY
before doing terminal size calls.

Lib/test/test_os/test_os.py
Misc/NEWS.d/next/Library/2026-07-29-16-53-50.gh-issue-154885.ptofmI.rst [new file with mode: 0644]
Modules/posixmodule.c

index 3e5ad52c4ab130d58c962a92087e1bc3334df41e..328a0dbeb99f8fabb2652bd7461df798651fcde8 100644 (file)
@@ -3970,12 +3970,7 @@ class TermsizeTests(unittest.TestCase):
         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 (file)
index 0000000..7c3839a
--- /dev/null
@@ -0,0 +1,2 @@
+:func:`os.get_terminal_size` now checks ``isatty`` before calling ``ioctl``,
+which reduces log noise on Android.
index f754d0e18b5fb09c8a9a12f6c8f7eb0a966f26e4..c34e3fc5eb600df7f79c6ea90613cac16a4afd08 100644 (file)
@@ -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);