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.

classification
标题: argparse: terminal width is not detected properly
类型: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
状态: closed Resolution: fixed
Dependencies: 13609 后续:
分配给: 抄送列表: berker.peksag, bethard, denilsonsa, flox, jmehnle, paul.j3, zbysz
优先级: normal 关键字: patch

Created on 2011-09-24 22:07 by zbysz, last changed 2022-04-11 14:57 by admin. This issue is now closed.

文件
文件名 上传时间 Description 编辑
issue13041.patch zbysz, 2012-02-22 15:36 use shutil.get_terminal_size() review
Pull Requests
URL Status Linked Edit
PR 8459 merged berker.peksag, 2018-07-25 09:11
Messages (12)
msg144507 - (view) Author: Zbyszek Jędrzejewski-Szmek (zbysz) * 日期: 2011-09-24 22:07
COLUMNS is a shell variable (updated whenever the window size
changes), that usually isn't exported to programs. Therefore checking
for COLUMNS in sys.environ will not work in the majority of cases. The
proper check is to use the TIOCGWINSZ ioctl on stdout.
    
Why COLUMNS is not exported? Because it can change during the lifetime
of a program. Therefore it is better to use the dynamic ioctl.
msg144521 - (view) Author: Zbyszek Jędrzejewski-Szmek (zbysz) * 日期: 2011-09-25 10:01
I see that adding a separate module was proposed in issue #8408, which was rejected/closed. I don't have the rights to reopen, so I'll continue here.

#8408 was proposing a new module, which seems a bit overkill, since the implementation for unix and windows is about 20 lines.

I'm attaching a second version of the patch which works on windows (tested with python3.2.2 on XP). Thanks to techtonik for pointing to a windows imlementation.
msg144523 - (view) Author: Denilson Figueiredo de Sá (denilsonsa) 日期: 2011-09-25 14:18
> #8408 was proposing a new module, which seems a bit overkill

If a module seems overkill, then maybe add this useful function to os module. Don't leave it private to argparse module. Maybe something along these lines:

    >>> import os
    >>> print(os.get_terminal_size())
    (80, 25)

Why do I believe a module could be better? Because I'd also like some way to detect when the terminal size has changed (without probing it all the time).
msg149531 - (view) Author: Steven Bethard (bethard) * (Python committer) 日期: 2011-12-15 11:08
I'd feel more comfortable with the argparse fix if it were simply calling "os.get_terminal_size()". I recommend that you:

* Create a new issue called, say "add os.get_terminal_size()" proposing just the single method.

* Add that issue to the Dependencies of this issue.

Once that is fixed, then the argparse fix should be simple.
msg149587 - (view) Author: Denilson Figueiredo de Sá (denilsonsa) 日期: 2011-12-16 01:05
Issue #13609 created, but I don't have permission to edit the dependencies.
msg150728 - (view) Author: Zbyszek Jędrzejewski-Szmek (zbysz) * 日期: 2012-01-06 13:04
New version to use after #13609 is implemented: patch2.diff
msg153961 - (view) Author: Zbyszek Jędrzejewski-Szmek (zbysz) * 日期: 2012-02-22 15:36
OK, I guess that this could now be closed, since 13609 has been commited.
(It is currently reopened, but the proposed tweaks wouldn't influence
the usage in argparse, even if accepted).

I'm attaching a patch which updates the tests to the new $COLUMNS logic.
Previously, unsetting COLUMNS would fix the width on 80, now setting
COLUMNS=80 is the proper way to do this.
msg223222 - (view) Author: Mark Lawrence (BreamoreBoy) * 日期: 2014-07-16 15:29
@Paul the attached patch is extremely simple and follows the work on #13609.  Is it okay with you if the patch was committed?
msg223298 - (view) Author: paul j3 (paul.j3) * (Python triager) 日期: 2014-07-16 23:30
The latest patch, using _shutil.get_terminal_size(), looks fine.  

It lets environ['COLUMNS'] have priority over the end user's terminal width, as demonstrated by the change to test_argparse.  test_argparse doesn't test changing the actual terminal size, but I imagine that would be a pain to implement.
msg223809 - (view) Author: paul j3 (paul.j3) * (Python triager) 日期: 2014-07-24 05:24
For now the user could add this to his module:

    import os, shutil
    os.environ['COLUMNS'] = str(shutil.get_terminal_size().columns)
msg315274 - (view) Author: Julian Mehnle (jmehnle) 日期: 2018-04-13 23:58
What's holding up the merging of this patch?
msg322365 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) 日期: 2018-07-25 15:23
New changeset 74102c9a5f2327c4fc47feefa072854a53551d1f by Berker Peksag in branch 'master':
bpo-13041: Use shutil.get_terminal_size() in argparse.HelpFormatter (GH-8459)
https://github.com/python/cpython/commit/74102c9a5f2327c4fc47feefa072854a53551d1f
历史
日期 用户 动作 参数
2022-04-11 14:57:21admin修改github: 57250
2018-07-25 15:24:20berker.peksag修改状态: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: + Python 3.8, - Python 3.5
2018-07-25 15:23:47berker.peksag修改消息: + msg322365
2018-07-25 09:11:28berker.peksag修改pull_requests: + pull_request7983
2018-04-14 00:09:54BreamoreBoy修改抄送: - BreamoreBoy
2018-04-13 23:58:03jmehnle修改抄送: + jmehnle
消息: + msg315274
2014-07-24 05:24:13paul.j3修改消息: + msg223809
2014-07-16 23:30:18paul.j3修改消息: + msg223298
2014-07-16 20:15:50berker.peksag修改抄送: + berker.peksag

type: behavior -> enhancement
versions: + Python 3.5, - Python 3.3
2014-07-16 15:29:44BreamoreBoy修改抄送: + paul.j3, BreamoreBoy
消息: + msg223222
2012-02-22 15:36:48zbysz修改文件: - patch2.diff
2012-02-22 15:36:42zbysz修改文件: - patch1.1.diff
2012-02-22 15:36:33zbysz修改文件: - patch1.diff
2012-02-22 15:36:05zbysz修改文件: + issue13041.patch

消息: + msg153961
2012-01-06 13:04:38zbysz修改文件: + patch2.diff

消息: + msg150728
2011-12-16 12:12:27giampaolo.rodola修改dependencies: + Add "os.get_terminal_size()" function
versions: - Python 2.7, Python 3.2
2011-12-16 01:05:43denilsonsa修改消息: + msg149587
2011-12-15 11:08:45bethard修改消息: + msg149531
versions: + Python 2.7, Python 3.2
2011-10-27 21:55:35flox修改抄送: + flox
stage: patch review

versions: - Python 3.4
2011-09-25 14:18:42denilsonsa修改消息: + msg144523
2011-09-25 10:02:00zbysz修改文件: + patch1.1.diff

消息: + msg144521
2011-09-25 02:40:21denilsonsa修改抄送: + denilsonsa
2011-09-24 22:13:22ezio.melotti修改抄送: + bethard
2011-09-24 22:12:15ezio.melotti链接issue13042 superseder
2011-09-24 22:07:18zbysz创建