Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

[issue13609] Add "os.get_terminal_size()" function

 

 

First page Previous page 1 2 3 Next page Last page  View All Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

Dec 15, 2011, 5:01 PM

Post #1 of 72 (102 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function

New submission from Denilson Figueiredo de Sá <denilsonsa [at] gmail>:

Please add a function called "os.get_terminal_size()" that should return a tuple "(width, height)".

The built-in "argparse" module would directly benefit from this function, as it would wrap the help text correctly. I'm pretty sure many users would also benefit from it.

Once this function gets implemented, issue #13041 can be trivially fixed. There are some suggestions about how to implement this feature in issue #8408.

[extra feature:] After this function gets implemented, it might be a good idea to implement some kind of API to detect size changes without requiring probing. (or, at least, the documentation should give some directions about how to achieve it)

----------
components: Library (Lib)
messages: 149586
nosy: denilsonsa
priority: normal
severity: normal
status: open
title: Add "os.get_terminal_size()" function
type: enhancement

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Dec 15, 2011, 10:53 PM

Post #2 of 72 (97 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Antoine Pitrou <pitrou [at] free> added the comment:

Well, do you want to propose a patch yourself?
See http://docs.python.org/devguide/ for how to contribute a patch.

----------
nosy: +pitrou
stage: -> needs patch
versions: +Python 3.3

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Dec 16, 2011, 12:14 AM

Post #3 of 72 (97 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Zbyszek Szmek <zbyszek [at] in> added the comment:

The patch is already almost there (in #13041). I'll post an updated version here in a moment.

----------
nosy: +zbysz

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Dec 16, 2011, 3:28 AM

Post #4 of 72 (97 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Denilson Figueiredo de Sá <denilsonsa [at] gmail> added the comment:

Zbyszek, I just looked at [1] and I disagree that the environment variable should have higher precedence. In fact, I believe it should have lower precedence, and should be used as a fallback.

[1]: http://bugs.python.org/file23241/patch1.1.diff

Reason? Imagine a program is launched, and thus it has COLUMNS set to some value. While the program is running, the user resizes the terminal. I believe (though I have not tested) that the envvar will keep the old value, and this function would return wrong dimensions.

In my opinion, the system-specific code (termios/windll) should be tried first, and then the envvars (as fallback). If all else fails, maybe it would be better to return (None, None) and let the caller of this function handle that.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Dec 16, 2011, 4:07 AM

Post #5 of 72 (97 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Giampaolo Rodola' <g.rodola [at] gmail> added the comment:

http://bugs.python.org/file23241/patch1.1.diff
This looks like something which would fit better into shutil module rather than os.
Also, the Windows implementation should not rely on ctypes.

----------
nosy: +giampaolo.rodola

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Dec 16, 2011, 4:16 AM

Post #6 of 72 (97 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Giampaolo Rodola' <g.rodola [at] gmail> added the comment:

Plus, you should provide also heigth, not only width, possibly as a namedtuple:

>>> import shutil
>>> shutil.get_terminal_size()
size(width=80, heigth=170)
>>>

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Dec 16, 2011, 5:41 AM

Post #7 of 72 (97 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Zbyszek Szmek <zbyszek [at] in> added the comment:

One reply to three comments:

[To myself]:
> I'll post an updated version here in a moment.
Hm, it's not so simple. The implementation is simple,
but the configure defines are harder than I thought.
But I'm getting there :)

> Zbyszek, I just looked at [1] and I disagree that the environment variable should have higher precedence. In fact, I believe it should have lower precedence, and should be used as a fallback.

I disagree. $COLUMNS will usually _not_ be set:

$ echo $COLUMNS
119

$ ./python -c 'import os; print("COLUMNS" in os.environ)'
False

As I wrote before, the shell only sets COLUMNS for use in the shell, without exporting. Giving a precedence to the environment variable (if it is set by the user) allows to do something like this:
$ COLUMNS=80 python program.py
... do whatever is to be dome for 80 columns ...
and the normal case (with dynamic checking) will also work correctly.

Anyway, I think that two functions should be provided: a "raw" one, which does the ioctl, and the "normal" one, which queries $COLUMNS and the the "raw" function. If different behaviour is wanted, than just
the "raw" one can be called.

> Also, the Windows implementation should not rely on ctypes.
Of course. For windows I have:
#include <conio.h>
...
GetConsoleScreenBufferInfo(handle, &csbi)
...

> This looks like something which would fit better into shutil module rather than os.
This is problematic. The docstring for shutil says:
Utility functions for copying and archiving files and directory trees.
So it doesn't seem to fit at all.

OTOH, there's termios and tty, but they are UNIX only. Module curses is also UNIX only, and slightly different topic, because get_terminal_width should be independent of curses.

> Plus, you should provide also heigth, not only width, possibly as a namedtuple.
Agreed.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Dec 16, 2011, 7:50 AM

Post #8 of 72 (97 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Giampaolo Rodola' <g.rodola [at] gmail> added the comment:

> The docstring for shutil says: Utility functions for copying and
> archiving files and directory trees. So it doesn't seem to fit at all.

Well... shutil should stand for "shell utilities" and it already contains stuff which is not strictly related to file/directory direct operations (see shutil.disk_usage and upcoming shutil.which).

But maybe you're right... I don't know...
My point is that a function which attempts different fallbacks (ask OS -> ask os.environ -> return (None, None) / raise exception) sounds more like an utility function rather than something belonging to os module, where you tipically see 1-to-1 interfaces for the underlying system functions.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Dec 16, 2011, 8:09 AM

Post #9 of 72 (97 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Zbyszek Szmek <zbyszek [at] in> added the comment:

This is proof-of-concept implementation.

This adds two modules: termsize (python) and _termsize (C). The first one contains the get_terminal_size user-facing function and namedtuple definition. The second on contains the function query_terminal_size which does the real job.

Two simple tests are added. I'm not sure if it is feasible to test with different terminal sizes: it certainly _could_ be done, e.g. by launching an xterm with a specified geometry, but this would be much
more complicated than this implementation, so probably not worth it.

This was only tested on 64-bit linux, seems to work.

I'm not sure how to the configure tests should be done: right now the presence of <sys/ioctl.h> is checked, and it is used. If not available,
<conio.h> is checked, and used. Otherwise NotImplementedError is thrown.

Would be nice to test on a mac and other systems, but I don't have one available at the moment unfortunately.

I think that the python part (termsize.py) should be either merged with os.py (or whatever home we find for this function), or rewritten in C in _termsizemodule.c and only imported into os. But since it hasn't yet been decided where this should go, keeping it separate is easier for now.

----------
keywords: +patch
Added file: http://bugs.python.org/file23981/termsize.diff

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Dec 16, 2011, 8:45 AM

Post #10 of 72 (88 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Amaury Forgeot d'Arc <amauryfa [at] gmail> added the comment:

Don't forget the Windows platform... here is an implementation: https://bitbucket.org/hpk42/py/src/980c8d526463/py/_io/terminalwriter.py#cl-284
But it would be better written in C, of course.

----------
nosy: +amaury.forgeotdarc

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Dec 16, 2011, 9:32 AM

Post #11 of 72 (88 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Antoine Pitrou <pitrou [at] free> added the comment:

Ok, a couple of general comments:
- there is no point having a separate module for a single function; I think the os module (and posixmodule.c for the C side) is a reasonable place where to put this
- C code should be indented with 4 spaces increments and no tabs (see PEP 7)
- constants in C code should be uppercase
- C code should be C89-compliant and therefore we don't use named struct initializers (such as ".m_size = 0")

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Dec 16, 2011, 5:41 PM

Post #12 of 72 (88 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Zbyszek Szmek <zbyszek [at] in> added the comment:

Here's updated version: termsize.diff.1

> Ok, a couple of general comments:
> - there is no point having a separate module for a single function; I > think the os module (and posixmodule.c for the C side) is a
> reasonable place where to put this
Done. (But posixmodule.c is so enourmous... I feel bad making it even longer.)

> - C code should be indented with 4 spaces increments and no tabs (see > PEP 7)
> - constants in C code should be uppercase
> - C code should be C89-compliant and therefore we don't use named
> struct initializers (such as ".m_size = 0")
All done, I hope.

This version was tested on linux/amd64 and win32 (XP).

----------
Added file: http://bugs.python.org/file23983/termsize.diff.1

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Dec 28, 2011, 5:08 AM

Post #13 of 72 (82 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Zbyszek Szmek <zbyszek [at] in> added the comment:

Seems to work also on kfreebsd/debian (with eglibc).

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Dec 28, 2011, 5:28 AM

Post #14 of 72 (82 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Changes by Antoine Pitrou <pitrou [at] free>:


----------
stage: needs patch -> patch review

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Dec 28, 2011, 1:28 PM

Post #15 of 72 (82 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Changes by Arfrever Frehtes Taifersar Arahesis <Arfrever.FTA [at] GMail>:


----------
nosy: +Arfrever

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Dec 30, 2011, 12:16 PM

Post #16 of 72 (76 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Antoine Pitrou <pitrou [at] free> added the comment:

(review posted on http://bugs.python.org/review/13609/show )

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Dec 31, 2011, 6:01 AM

Post #17 of 72 (76 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Zbyszek Szmek <zbyszek [at] in> added the comment:

Thanks for the review!

New version is attached. The code is actually slightly shorter, but
there are more docs.

Doc/library/os.rst | 52 +++++++++++++++++++
Doc/whatsnew/3.3.rst | 5 +
Lib/os.py | 43 +++++++++++++++
Lib/test/test_termsize.py | 31 +++++++++++
Misc/NEWS | 3 +
Modules/posixmodule.c | 125 ++++++++++++++++++++++++++++++++++++++++++++++
configure | 2
configure.in | 2
pyconfig.h.in | 3 +
9 files changed, 264 insertions(+), 2 deletions(-)

>The patch also lacks some documentation update in Doc/library/os.rst.
Added as a subsection under "File Descriptor Operations" section.
I also added an entry to Misc/NEWS and Doc/whatsnew/3.3.rst.

> Lib/os.py:833: def get_terminal_size(columns=80, rows=25):
> The arguments seem ignored in the function body, so I think they should simply
> be removed.
The implementation was borked and didn't actually do what the
docstring said. It should be fixed now.

I want to retain the default values, because get_terminal_size()
should "do the right thing" in case stdout is not a terminal,
without the user needing to wrap it in a try..except block
or some other test. Putting the default values as parameters
makes it clear what will be returned in case query fails, and
makes it easy to override those defaults.

To make it clearer that those are the fallback values, I renamed the
parameter to 'fallback=(c, r)', which should be understandable even
without reading the docstring. Having it as a single parameter might
also make it easier to add something like 'minimum=(c, r)' in the
future.

> I wonder if you shouldn't try to explicitly pass sys.stdout.fileno() here, or
> perhaps sys.__stdout__.fileno() since the former could be overriden.
OK, changed to sys.__stdout__.fileno().

I think that sys.__stdout__ is better, because when stdout is overridden,
it is usually with something that is not a terminal. Changing the output
terminal is probably pretty rare.

> Actually, perhaps get_terminal_size() should have an optional file descriptor
> argument.
Querying anything other than stdout should be pretty rare. If
necessary, query_terminal_size() can be called explicitly. Also,
the variables $COLUMNS and $ROWS are usually meant to refer to stdout,
so get_terminal_size() is about stdout and ROWS and COLUMNS, and the
other function allows full control.

> Lib/test/test_termsize.py:1: import unittest
> This file seems to lack an ``if __name__ == '__main__'`` like other test files
OK.

> Besides, why not simply put the tests in test_os.py?
The tests were nicely self-contained. I wanted to be able to do:
./python Lib/test/test_termsize.py
and
./python Lib/test/test_termsize.py | cat
but I guess it can be easily moved to test_os.py if necessary.

> Lib/test/test_termsize.py:7: def set_environ(**variables):
> There's already EnvironmentVarGuard in test.support.
OK.

> Lib/test/test_termsize.py:7: def set_environ(**variables):
> There's already EnvironmentVarGuard in test.support.
> Lib/test/test_termsize.py:25: self.assertTrue(0 < size.columns)
> Better to use assertGreater here, failure messages will be more informative.
> Lib/test/test_termsize.py:33: self.assertTrue(size.columns == 777)
> And better to use assertEqual here.
OK.

> Typo here ("imlmented").
OK.

> Modules/posixmodule.c:10571: return PyErr_Format(PyExc_ValueError)
> I don't think there's any point in changing the error here. Just let
> a normal OSError be raised.
OK, s/ValueError/OSError/ here and the the windows case below too.

> Modules/posixmodule.c:10573: return PyErr_SetFromErrno(PyExc_IOError);
> For the record, in 3.3, PyExc_IOError is an alias of PyExc_OSError.
OK, s/IOError/OSError/ for clarity.

> Modules/posixmodule.c:10599: return PyErr_Format(PyExc_IOError, "error %i",
> (int) GetLastError());
> Just use PyErr_SetFromWindowsErr(GetLastError()).
OK.

> Modules/posixmodule.c:11086: {"query_terminal_size", query_terminal_size,
> METH_VARARGS, termsize__doc__},
> I don't think there's any point in making this helper function public, so I'd
> rename it _query_terminal_size or something.
The idea is that query_terminal_size() can be used to do the
low-level query ignoring $COLUMNS and $ROWS and returing the real
error if something goes wrong. If is documented, so I think that
it can be "public".

I've also improved the docstrings slightly.

----------
Added file: http://bugs.python.org/file24117/termsize.diff.2

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jan 2, 2012, 5:00 AM

Post #18 of 72 (76 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Antoine Pitrou <pitrou [at] free> added the comment:

Thanks for the updated patch.
I think I would like other people's opinions about the dual-function approach.

----------
nosy: +loewis, neologix, rosslagerwall

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jan 2, 2012, 5:55 AM

Post #19 of 72 (77 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Giampaolo Rodola' <g.rodola [at] gmail> added the comment:

What I would do:

- build the namedtuple in Python rather than in C
- I don't particularly like CamelCased names for nametuples (I would use "size" instead of "TerminalSize")
- on UNIX, the ioctl() stuff can be done in python rather than in C
- use C only on Windows to deal with GetStdHandle/GetConsoleScreenBufferInfo; function name should be accessed as nt._get_terminal_size similarly to what we did for shutil.disk_usage in issue12442.
- do not raise NotImplementedError; if the required underlying functions are not available os.get_terminal_size should not exists in the first place (same as we did for shutil.disk_usage / issue12442)


Also, I'm not sure I like fallback=(80, 25) as default, followed by:

try:
size = query_terminal_size(sys.__stdout__.fileno())
except OSError:
size = TerminalSize(fallback)

That means we're never going to get an exception.
Instead I would:
- provide fallback as None
- let OSError propate unless fallback is not None

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jan 2, 2012, 8:46 AM

Post #20 of 72 (77 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Ross Lagerwall <rosslagerwall [at] gmail> added the comment:

Nice patch :-)

I think the two function approach works well.

Since you have already checked that termsize is not NULL, Py_DECREF can be used instead of Py_CLEAR.

Would it not be better to use sys.__stdout__ instead of 1 in the documentation and to use STDOUT_FILENO instead of 1 in the code?

A brief google suggested that Solaris requires sys/termios.h for TIOCGWINSZ. Perhaps also only define TERMSIZE_USE_IOCTL if TIOCGWINSZ is defined.
Like so:
#if defined(HAVE_SYS_IOCTL_H)
#include <sys/ioctl.h>
#if defined(TIOCGWINSZ)
#define TERMSIZE_USE_IOCTL
#else
#define TERMSIZE_USE_NOTIMPLEMENTED
#endif
#elif defined(HAVE_CONIO_H)
#include <windows.h>
#include <conio.h>
#define TERMSIZE_USE_CONIO
#else
#define TERMSIZE_USE_NOTIMPLEMENTED
#endif

(didn't check the windows parts)

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jan 2, 2012, 10:57 AM

Post #21 of 72 (76 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Zbyszek Szmek <zbyszek [at] in> added the comment:

Thanks for the reviews!

> - build the namedtuple in Python rather than in C
I started this way, but if two functions are used, it is nicer to have them both
return the same type. If it was defined in the Python part, there would be
loop in the sense that os would import the function from _posixmodule, and
_posixmodule would import the namedtuple from os. The code for the tuple is fairly simple,
mostly docs, and saving a few lines just doesn't seem to be worth the complication of doing
it other way around.

> - I don't particularly like CamelCased names for nametuples (I would use "size" instead of "TerminalSize")
I was following the style in _posixmodule: there is WaitidResultType and SchedParamType.
"size" seems to generic, maybe something like "terminal_size" would be better (SchedParamType
is exported as sched_param).

Will change to "terminal_size".

- on UNIX, the ioctl() stuff can be done in python rather than in C
It can, but it requires extracting the bytes by hand, doing it in C is cleaner.
The C implementation is only 5 lines (in addition to the code necessary for windows)
and again it seems simpler this way. Also the configuration is kept in one
place in the #ifdefs at the top, not in two different files.

> - use C only on Windows to deal with GetStdHandle/GetConsoleScreenBufferInfo; function name should be
> accessed as nt._get_terminal_size similarly to what we did for shutil.disk_usage in issue12442.
> - do not raise NotImplementedError; if the required underlying functions are not available
> os.get_terminal_size should not exists in the first place (same as we did for shutil.disk_usage / issue12442)

I think that there is a difference in "importance" -- shutil.disk_usage cannot be
faked, or ignored, and if it is unavailable, then some work-around must be implemented.
OTOH, the terminal size is an embellishment, and if a wrong terminal size is used, the output
might wrap around or be a bit narrow (the common case), but like in argparse.FancyFormatter,
just continuing is probably reasonable. So to save the users of the function the trouble
to wrap it in some try..except, let's cater for the common case.

This same argument goes for defining the function only if an implementation is available:
argparse would need something like:
try:
os.get_terminal_size
except NameError:
def get_terminal_size():
return (80, 25)
which just doesn't seem to _gain_ anything.

> Also, I'm not sure I like fallback=(80, 25) as default, followed by:
> [...]
> That means we're never going to get an exception.
> Instead I would:
> - provide fallback as None
> - let OSError propate unless fallback is not None
Again, if the user forgets to add the fallback, and only tests in a terminal, she
would get an unnecessary surprise when running the thing in a pipe. So the fallback
would have to be almost always provided... and it will almost always be (80, 25).
So let's just provide it upfront, and let the user call the low-level function if
they want full control.

-----------------------------------------------------------------------------------

> I think the two function approach works well.
:)

> Since you have already checked that termsize is not NULL, Py_DECREF can be used instead of Py_CLEAR.
OK.

> Would it not be better to use sys.__stdout__ instead of 1 in the documentation and to use STDOUT_FILENO
> instead of 1 in the code?
OK.

> A brief google suggested that Solaris requires sys/termios.h for TIOCGWINSZ. Perhaps also only define
> TERMSIZE_USE_IOCTL if TIOCGWINSZ is defined.
Hm, I don't have solaris available, so I won't be able to check easily, but
maybe sys/termios.h should be imported if TIOCGWINSZ is not available. Would
be nice to provide the implementation if possible.

> Like so:
Something like:

> #if defined(HAVE_SYS_IOCTL_H)
> #include <sys/ioctl.h>
> #if defined(TIOCGWINSZ)
> #define TERMSIZE_USE_IOCTL
> #else

#if defined(HAVE_SYS_TERMIOS_H)
#include <sys/termios.h>
#if defined(TIOCGWINSZ)
#define TERMSIZE_USE_IOCTL
#else
#define TERMSIZE_USE_NOTIMPLEMENTED

> #endif
> #elif defined(HAVE_CONIO_H)
> #include <windows.h>
> #include <conio.h>
> #define TERMSIZE_USE_CONIO
> #else
> #define TERMSIZE_USE_NOTIMPLEMENTED
> #endif

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jan 2, 2012, 11:49 AM

Post #22 of 72 (75 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Ross Lagerwall <rosslagerwall [at] gmail> added the comment:

I'll try & investigate Solaris a bit...

Also, what should be the default terminal size?

Gnome-terminal and xterm seem to default to 80x24, not 80x25.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jan 2, 2012, 2:43 PM

Post #23 of 72 (77 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Zbyszek Szmek <zbyszek [at] in> added the comment:

Updated patch termsize.diff.3

- s/TerminalSize/terminal_size/ in Python code
- change the fallback to (80, 24) (seems to be the commonest default)
- s/Py_CLEAR/Py_DECREF/
- use STDOUT_FILENO
- add more hrefs in docs
- include <termios.h> is available (untested fix for solaris compatibility)
- rename TerminalSize as terminal_size in Python code

I tested this iteration only on linux and windows, but it is not substantially changed, so should still work on *bsd. (I previously
tested on debian/kfreebsd and dragonflybsd and it seemed functional).

----------
Added file: http://bugs.python.org/file24127/termsize.diff.3

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jan 5, 2012, 12:55 PM

Post #24 of 72 (77 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Martin v. Löwis <martin [at] v> added the comment:

I haven't read much of this issue, but I strongly dislike the use of named tuples. Either we want people to use named fields, then we should use a regular class (possibly with slots), or we want to define the result as two values, then there should be a plain tuple result.

named tuples should only be used as a compatibility mechanism, when the first design used tuples, and it was later found that additional values need to be returned which would change the number of values (or the original design was considered bad because it returned too many positional values to properly keep track).

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jan 5, 2012, 1:24 PM

Post #25 of 72 (77 views)
Permalink
[issue13609] Add "os.get_terminal_size()" function [In reply to]

Zbyszek Szmek <zbyszek [at] in> added the comment:

> I haven't read much of this issue, but I strongly dislike the use of
> named tuples.
I don't really have a very strong opinion, but (cols, rows) does seem a lot like a tuple -- it really is just a pair of values without other function or state. Still I would much prefer to say
get_terminal_size().columns
than
get_terminal_size()[0]
So a bare tuple seems like the worst choice.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

First page Previous page 1 2 3 Next page Last page  View All Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.