Skip to content

Commit 51ae3c0

Browse files
authored
Make os.get_terminal_size check isatty before calling ioctl (#154885)
Calling ioctl on stdout raises warnings on Android. Ensure we have a TTY before doing terminal size calls.
1 parent 49f9667 commit 51ae3c0

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
@@ -3970,12 +3970,7 @@ def test_does_not_crash(self):
39703970
try:
39713971
size = os.get_terminal_size()
39723972
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:
3973+
if sys.platform == "win32" or e.errno in (errno.EINVAL, errno.ENOTTY):
39793974
# Under win32 a generic OSError can be thrown if the
39803975
# handle cannot be retrieved
39813976
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
@@ -15971,6 +15971,13 @@ os_get_terminal_size_impl(PyObject *module, int fd)
1597115971

1597215972
#ifdef TERMSIZE_USE_IOCTL
1597315973
{
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+
1597415981
struct winsize w;
1597515982
if (ioctl(fd, TIOCGWINSZ, &w))
1597615983
return PyErr_SetFromErrno(PyExc_OSError);

0 commit comments

Comments
 (0)