diff options
author | Fedja Beader <fedja@protonmail.ch> | 2023-12-12 21:25:02 +0100 |
---|---|---|
committer | Fedja Beader <fedja@protonmail.ch> | 2023-12-12 21:25:02 +0100 |
commit | 635a136ec06c00efadc46a9d364f15d771f4036e (patch) | |
tree | 493b4cbad24432521363a90a72635e24151baa75 /attoconf/help.py | |
parent | bd57b489d0dacccbdd7a675cafdaff5e72ba7215 (diff) | |
download | attobuild-635a136ec06c00efadc46a9d364f15d771f4036e.tar.gz attobuild-635a136ec06c00efadc46a9d364f15d771f4036e.tar.bz2 attobuild-635a136ec06c00efadc46a9d364f15d771f4036e.tar.xz attobuild-635a136ec06c00efadc46a9d364f15d771f4036e.zip |
Update for Python3
File "attoconf/help.py",
line 113, in detect_terminal_width
fd = getattr(fd, 'fileno', lambda: -1)()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
io.UnsupportedOperation: fileno
Diffstat (limited to 'attoconf/help.py')
-rw-r--r-- | attoconf/help.py | 27 |
1 files changed, 10 insertions, 17 deletions
diff --git a/attoconf/help.py b/attoconf/help.py index 777becb..a2379a6 100644 --- a/attoconf/help.py +++ b/attoconf/help.py @@ -109,23 +109,16 @@ class HelpSection(object): def detect_terminal_width(fd, DEFAULT_WIDTH=float('inf')): ''' Detect the width of a terminal. ''' - if not isinstance(fd, int): - fd = getattr(fd, 'fileno', lambda: -1)() - if fd == -1: - return DEFAULT_WIDTH - - import fcntl - import termios - try: - buf = fcntl.ioctl(fd, termios.TIOCGWINSZ, b'xx' * 4) - except IOError as e: - import errno - if e.errno != errno.ENOTTY: - raise - return DEFAULT_WIDTH - import struct - ws_row, ws_col, ws_xpixel, ws_ypixel = struct.unpack('HHHH', buf) - return ws_col + import os + import shutil + + if isinstance(fd, int): + try: + return os.get_terminal_size(fd) + except OSError(...): + return DEFAULT_WIDTH + else: + return shutil.get_terminal_size(fallback=(DEFAULT_WIDTH, 24)).columns class Help(object): |