Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shutil.get_terminal_size() returns 0 when run in a pty #86340

Closed
The-Compiler mannequin opened this issue Oct 27, 2020 · 5 comments
Closed

shutil.get_terminal_size() returns 0 when run in a pty #86340

The-Compiler mannequin opened this issue Oct 27, 2020 · 5 comments
Labels
3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@The-Compiler
Copy link
Mannequin

The-Compiler mannequin commented Oct 27, 2020

BPO 42174
Nosy @giampaolo, @tarekziade, @ambv, @The-Compiler, @jack1142, @FFY00
PRs
  • bpo-42174: fallback to sane values if the columns or lines are 0 in get_terminal_size #29046
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-10-19.18:48:53.526>
    created_at = <Date 2020-10-27.16:32:31.273>
    labels = ['type-bug', 'library', '3.11']
    title = 'shutil.get_terminal_size() returns 0 when run in a pty'
    updated_at = <Date 2021-10-19.18:48:53.526>
    user = 'https://github.com/The-Compiler'

    bugs.python.org fields:

    activity = <Date 2021-10-19.18:48:53.526>
    actor = 'lukasz.langa'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-10-19.18:48:53.526>
    closer = 'lukasz.langa'
    components = ['Library (Lib)']
    creation = <Date 2020-10-27.16:32:31.273>
    creator = 'The Compiler'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 42174
    keywords = ['patch']
    message_count = 5.0
    messages = ['379773', '380224', '404329', '404331', '404332']
    nosy_count = 6.0
    nosy_names = ['giampaolo.rodola', 'tarek', 'lukasz.langa', 'The Compiler', 'jack1142', 'FFY00']
    pr_nums = ['29046']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue42174'
    versions = ['Python 3.11']

    @The-Compiler
    Copy link
    Mannequin Author

    The-Compiler mannequin commented Oct 27, 2020

    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 python/mypy#8144 (comment):

    $ python3 t.py 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:
    https://github.com/pre-commit/mirrors-mypy/issues/29

    @The-Compiler The-Compiler mannequin added 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Oct 27, 2020
    @The-Compiler
    Copy link
    Mannequin Author

    The-Compiler mannequin commented Nov 2, 2020

    Just found another workaround for this in the wild, as part of the "rich" library:

    https://github.com/willmcgugan/rich/blob/v9.1.0/rich/console.py#L669-L672

    @ambv
    Copy link
    Contributor

    ambv commented Oct 19, 2021

    New changeset 236e301 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)
    236e301

    @ambv
    Copy link
    Contributor

    ambv commented Oct 19, 2021

    We decided not to backport this to 3.10 nor 3.9 due to the subtle nature of the change.

    @ambv ambv added 3.11 only security fixes and removed 3.9 only security fixes labels Oct 19, 2021
    @ambv
    Copy link
    Contributor

    ambv commented Oct 19, 2021

    Thanks for the fix, Filipe! ✨ 🍰 ✨

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant