]> arthur.barton.de Git - bup.git/commitdiff
tty_width: only import fcntl and termios once
authorRob Browning <rlb@defaultvalue.org>
Thu, 6 Apr 2017 06:34:39 +0000 (01:34 -0500)
committerRob Browning <rlb@defaultvalue.org>
Thu, 6 Apr 2017 06:34:39 +0000 (01:34 -0500)
Signed-off-by: Rob Browning <rlb@defaultvalue.org>
lib/bup/options.py

index d73aac2eb126484aa57c1a31887e32a96338e0d9..3256ac96d585515260dc7498f32229daef4dfd14 100644 (file)
@@ -62,6 +62,16 @@ space. The text on that line will be output after an empty line.
 """
 import sys, os, textwrap, getopt, re, struct
 
+try:
+    import fcntl
+except ImportError:
+    fcntl = None
+
+try:
+    import termios
+except ImportError:
+    termios = None
+
 
 def _invert(v, invert):
     if invert:
@@ -123,15 +133,18 @@ def _atoi(v):
         return 0
 
 
-def _tty_width():
-    s = struct.pack("HHHH", 0, 0, 0, 0)
-    try:
-        import fcntl, termios
-        s = fcntl.ioctl(sys.stderr.fileno(), termios.TIOCGWINSZ, s)
-    except (IOError, ImportError):
-        return _atoi(os.environ.get('WIDTH')) or 70
-    (ysize,xsize,ypix,xpix) = struct.unpack('HHHH', s)
-    return xsize or 70
+if not fcntl and termios:
+    def _tty_width():
+        return 70
+else:
+    def _tty_width():
+        s = struct.pack("HHHH", 0, 0, 0, 0)
+        try:
+            s = fcntl.ioctl(sys.stderr.fileno(), termios.TIOCGWINSZ, s)
+        except IOError:
+            return 70
+        ysize, xsize, ypix, xpix = struct.unpack('HHHH', s)
+        return xsize or 70
 
 
 class Options: