Is shutil.get_terminal_size useless?

Steve D'Aprano steve+python at pearwood.info
Sat Jan 28 06:00:57 EST 2017


On Sat, 28 Jan 2017 07:39 pm, Peter Otten wrote:

> One potential advantage of shutil.get_terminal_size() is that you can
> affect it with an environment variable:
> 
> $ python3 test_gts.py | cat
> shutil: os.terminal_size(columns=999, lines=999)
> os: os.terminal_size(columns=72, lines=48)
> 
> $ COLUMNS=123 python3 test_gts.py | cat
> shutil: os.terminal_size(columns=123, lines=999)
> os: os.terminal_size(columns=72, lines=48)

Unless your terminal *actually is* 123 columns wide, I don't see that as an
advantage. I see that as "Hey look, we can fool shutil into returning
absolute garbage instead of the terminal size!"

I can already read environment variables using os.getenv() and os.environ,
so this gives me no new functionality.


> I have the line
> 
> export LINES COLUMNS
> 
> in my .bashrc, so by default I see the physical size:
> 
> $ export COLUMNS LINES
> $ python3 test_gts.py | cat
> shutil: os.terminal_size(columns=72, lines=48)
> os: os.terminal_size(columns=72, lines=48)

But what happens if you resize the terminal while the process is running?
Again, the os.get_terminal_size() correctly returns the updated size, while
shutil.get_terminal_size() doesn't.



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.



More information about the Python-list mailing list