Skip to content

Make os.get_terminal_size check isatty before calling ioctl - #154885

Merged
freakboy3742 merged 2 commits into
python:mainfrom
mhsmith:get-terminal-size-isatty
Jul 30, 2026
Merged

Make os.get_terminal_size check isatty before calling ioctl#154885
freakboy3742 merged 2 commits into
python:mainfrom
mhsmith:get-terminal-size-isatty

Conversation

@mhsmith

@mhsmith mhsmith commented Jul 29, 2026

Copy link
Copy Markdown
Member

On Android, stdout is probably not connected, and calling ioctl(TIOCGWINSZ) on an invalid file descriptor causes a log message "avc: denied { ioctl }", and returns EACCES. This still meets the specification of get_terminal_size, which only says it throws an OSError.

However, the log noise can be a problem. You can see it in this run of the CPython test suite, mostly in tests that involve argparse, such as test_ast. Some common tools such as pytest are even worse affected, as they call get_terminal_size very often.

So this PR adds an isatty check before caling ioctl. This should be harmless on other platforms, so I haven't made it Android-specific.

This isn't a bugfix, so it shouldn't be backported.

@mhsmith mhsmith changed the title Make os.get_terminal_size check isatty before ioctl Make os.get_terminal_size check isatty before calling ioctl Jul 29, 2026
@mhsmith
mhsmith marked this pull request as ready for review July 29, 2026 16:17
@mhsmith

mhsmith commented Jul 29, 2026

Copy link
Copy Markdown
Member Author

Confirmed that the relevant test is skipped on Android:

0:00:00 [1/1] test_os.test_os
test_does_not_crash (test.test_os.test_os.TermsizeTests.test_does_not_crash)
Check if get_terminal_size() returns a meaningful value. ... skipped 'failed to query terminal size'
test_stty_match (test.test_os.test_os.TermsizeTests.test_stty_match)
Check if stty returns the same results ... skipped 'requires subprocess support'
test_windows_fd (test.test_os.test_os.TermsizeTests.test_windows_fd)
Check if get_terminal_size() returns a meaningful value in Windows ... skipped 'Windows specific test'
 
----------------------------------------------------------------------
Ran 3 tests in 0.000s
 
OK (skipped=3)

And still passes on macOS:

0:00:00 load avg: 1.90 mem: 23.8 MiB [1/1] test_os.test_os
test_does_not_crash (test.test_os.test_os.TermsizeTests.test_does_not_crash)
Check if get_terminal_size() returns a meaningful value. ... ok
test_stty_match (test.test_os.test_os.TermsizeTests.test_stty_match)
Check if stty returns the same results ... ok
test_windows_fd (test.test_os.test_os.TermsizeTests.test_windows_fd)
Check if get_terminal_size() returns a meaningful value in Windows ... skipped 'Windows specific test'

----------------------------------------------------------------------
Ran 3 tests in 0.011s

OK (skipped=1)
% ./cross-build/build/python.exe -c 'import os; print(os.get_terminal_size())'
os.terminal_size(columns=193, lines=63

@mhsmith
mhsmith requested a review from freakboy3742 July 29, 2026 16:24
@freakboy3742 freakboy3742 added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jul 30, 2026

@freakboy3742 freakboy3742 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me 👍

Agreed that this doesn't meet the requirements for a backport to stable releases; but I've back ported to 3.15 because it will be an appreciable quality-of-life improvement, and 3.15 isn't final yet.

@freakboy3742
freakboy3742 enabled auto-merge (squash) July 30, 2026 02:04
@freakboy3742
freakboy3742 merged commit 51ae3c0 into python:main Jul 30, 2026
61 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @mhsmith for the PR, and @freakboy3742 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Jul 30, 2026

Copy link
Copy Markdown

GH-154903 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jul 30, 2026
freakboy3742 pushed a commit that referenced this pull request Jul 30, 2026
…tl` (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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants