Skip to content

Commit cfd8e2a

Browse files
[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 51ae3c0) Co-authored-by: Malcolm Smith <smith@chaquo.com>
1 parent dd2714d commit cfd8e2a

3 files changed

Lines changed: 10 additions & 6 deletions

File tree

Lib/test/test_os/test_os.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3966,12 +3966,7 @@ def test_does_not_crash(self):
39663966
try:
39673967
size = os.get_terminal_size()
39683968
except OSError as e:
3969-
known_errnos = [errno.EINVAL, errno.ENOTTY]
3970-
if sys.platform == "android":
3971-
# The Android testbed redirects the native stdout to a pipe,
3972-
# which returns a different error code.
3973-
known_errnos.append(errno.EACCES)
3974-
if sys.platform == "win32" or e.errno in known_errnos:
3969+
if sys.platform == "win32" or e.errno in (errno.EINVAL, errno.ENOTTY):
39753970
# Under win32 a generic OSError can be thrown if the
39763971
# handle cannot be retrieved
39773972
self.skipTest("failed to query terminal size")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15937,6 +15937,13 @@ os_get_terminal_size_impl(PyObject *module, int fd)
1593715937

1593815938
#ifdef TERMSIZE_USE_IOCTL
1593915939
{
15940+
// On Android, stdout is probably not connected, and calling TIOCGWINSZ
15941+
// on an invalid file descriptor causes a log message "avc: denied {
15942+
// ioctl }". Some common tools such as pytest call get_terminal_size
15943+
// very often, so check it's a TTY first to avoid cluttering the log.
15944+
if (!isatty(fd))
15945+
return PyErr_SetFromErrno(PyExc_OSError);
15946+
1594015947
struct winsize w;
1594115948
if (ioctl(fd, TIOCGWINSZ, &w))
1594215949
return PyErr_SetFromErrno(PyExc_OSError);

0 commit comments

Comments
 (0)