This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: shutil.get_terminal_size() returns 0 when run in a pty
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: FFY00, The Compiler, giampaolo.rodola, jack1142, lukasz.langa, tarek
Priority: normal Keywords: patch

Created on 2020-10-27 16:32 by The Compiler, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 29046 merged FFY00, 2021-10-19 00:32
Messages (5)
msg379773 - (view) Author: Florian Bruhin (The Compiler) * Date: 2020-10-27 16:32
When shutil.get_terminal_size() is used in a PTY, the os.get_terminal_size() call can return (0, 0).

With the pty script from

$ python3 python3 -c 'import shutil; print(shutil.get_terminal_size())'        
os.terminal_size(columns=0, lines=0)

While the following cases *are* handled by returning the fallback:

- COLUMNS or lines being non-existent or 0
- os.get_terminal_size raising

The case of os.get_terminal_size() succeeding but returning (0, 0) should probably be handled the same way as well.

This e.g. affects mypy when run with pre-commit:
msg380224 - (view) Author: Florian Bruhin (The Compiler) * Date: 2020-11-02 14:23
Just found another workaround for this in the wild, as part of the "rich" library:
msg404329 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-10-19 18:42
New changeset 236e301b8ad9b78ee880baf12e98a826113dc59b by Filipe Laíns in branch 'main':
bpo-42174: fallback to sane values if the columns or lines are 0 in get_terminal_size (GH-29046)
msg404331 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-10-19 18:48
We decided not to backport this to 3.10 nor 3.9 due to the subtle nature of the change.
msg404332 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-10-19 18:48
Thanks for the fix, Filipe! ✨ 🍰 ✨
Date User Action Args
2022-04-11 14:59:37adminsetgithub: 86340
2021-10-19 18:48:53lukasz.langasetstatus: open -> closed
resolution: fixed
messages: + msg404332

stage: patch review -> resolved
2021-10-19 18:48:05lukasz.langasetmessages: + msg404331
versions: + Python 3.11, - Python 3.9
2021-10-19 18:42:17lukasz.langasetnosy: + lukasz.langa
messages: + msg404329
2021-10-19 00:32:03FFY00setkeywords: + patch
stage: patch review
pull_requests: + pull_request27317
2021-10-15 21:20:21FFY00setnosy: + FFY00
2021-10-10 01:10:32jack1142setnosy: + jack1142
2020-11-02 14:23:22The Compilersetmessages: + msg380224
2020-10-27 16:32:31The Compilercreate