]> arthur.barton.de Git - bup.git/commitdiff
Replace atoi with explicit checks; rm unused atof
authorRob Browning <rlb@defaultvalue.org>
Tue, 22 Dec 2020 17:17:45 +0000 (11:17 -0600)
committerRob Browning <rlb@defaultvalue.org>
Thu, 24 Dec 2020 01:27:40 +0000 (19:27 -0600)
Replace atoi with explicit checks, making the intention a bit clearer
at each call site, and avoiding trouble we've had in the past with the
default to 0 for any unrecognized input.

This does make us stricter about the values (and the environment),
i.e. BUP_TTY_WIDTH=random-noise will now cause a crash.

Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
lib/bup/client.py
lib/bup/helpers.py
lib/bup/ssh.py
lib/cmd/bup
lib/cmd/random-cmd.py

index f8f5807d87cfd6bf5ab270b53536acd237c87ab3..b37b71d2fff4d4df48a0631e243f96ab729c3c7d 100644 (file)
@@ -10,7 +10,7 @@ from bup import git, ssh, vfs
 from bup.compat import environ, range, reraise
 from bup.helpers import (Conn, atomically_replaced_file, chunkyreader, debug1,
                          debug2, linereader, lines_until_sentinel,
-                         mkdirp, progress, qprogress, DemuxConn, atoi)
+                         mkdirp, progress, qprogress, DemuxConn)
 from bup.io import path_msg
 from bup.vint import read_bvec, read_vuint, write_bvec
 
@@ -101,7 +101,8 @@ class Client:
                     reraise(ClientError('connect: %s' % e))
             elif self.protocol == b'bup':
                 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-                self.sock.connect((self.host, atoi(self.port) or 1982))
+                self.sock.connect((self.host,
+                                   1982 if self.port is None else int(self.port)))
                 self.sockw = self.sock.makefile('wb')
                 self.conn = DemuxConn(self.sock.fileno(), self.sockw)
         self._available_commands = self._get_available_commands()
index 2b0b6ed2c6ddeee83c686b422961beb68d375826..aa691eb69ccd198bfc8560f9f0199c7a6621b970 100644 (file)
@@ -19,6 +19,9 @@ from bup.io import byte_stream, path_msg
 from bup.options import _tty_width as tty_width
 
 
+buglvl = int(os.environ.get('BUP_DEBUG', 0))
+
+
 class Nonlocal:
     """Helper to deal with Python scoping issues"""
     pass
@@ -37,26 +40,6 @@ def last(iterable):
         pass
     return result
 
-
-def atoi(s):
-    """Convert s (ascii bytes) to an integer. Return 0 if s is not a number."""
-    try:
-        return int(s or b'0')
-    except ValueError:
-        return 0
-
-
-def atof(s):
-    """Convert s (ascii bytes) to a float. Return 0 if s is not a number."""
-    try:
-        return float(s or b'0')
-    except ValueError:
-        return 0
-
-
-buglvl = atoi(os.environ.get('BUP_DEBUG', 0))
-
-
 try:
     _fdatasync = os.fdatasync
 except AttributeError:
@@ -165,8 +148,8 @@ def debug2(s):
         log(s)
 
 
-istty1 = os.isatty(1) or (atoi(os.environ.get('BUP_FORCE_TTY')) & 1)
-istty2 = os.isatty(2) or (atoi(os.environ.get('BUP_FORCE_TTY')) & 2)
+istty1 = os.isatty(1) or (int(os.environ.get('BUP_FORCE_TTY', 0)) & 1)
+istty2 = os.isatty(2) or (int(os.environ.get('BUP_FORCE_TTY', 0)) & 2)
 _last_progress = ''
 def progress(s):
     """Calls log() if stderr is a TTY.  Does nothing otherwise."""
index 7f178a69dc2ca0101563e283878b0d3e326ceec3..2d539e8a32e0084e911d39daff7d8c219c2245ef 100644 (file)
@@ -14,11 +14,11 @@ def connect(rhost, port, subcmd, stderr=None):
     if rhost is None or rhost == b'-':
         argv = [path.exe(), subcmd]
     else:
-        buglvl = helpers.atoi(environ.get(b'BUP_DEBUG'))
-        force_tty = helpers.atoi(environ.get(b'BUP_FORCE_TTY'))
+        buglvl = int(environ.get(b'BUP_DEBUG', 0))
+        force_tty = int(environ.get(b'BUP_FORCE_TTY', 0))
         tty_width = environ.get(b'BUP_TTY_WIDTH', None)
         if tty_width is not None:
-            tty_width = b'BUP_TTY_WIDTH=%d' % helpers.atoi(tty_width)
+            tty_width = b'BUP_TTY_WIDTH=%d' % int(tty_width)
         else:
             tty_width = b''
         cmd = b"""
index 03d98c54440aec457cdb5a12ba78a1e9b3ddbd3d..ee029aa74595bf9f163d3c5e29dcd4049d548fb7 100755 (executable)
@@ -37,7 +37,7 @@ from bup.compat import environ, fsdecode
 from bup.io import path_msg
 from bup import compat, path, helpers
 from bup.compat import add_ex_tb, add_ex_ctx, argv_bytes, wrap_main
-from bup.helpers import atoi, columnate, debug1, log, merge_dict, tty_width
+from bup.helpers import columnate, debug1, log, merge_dict, tty_width
 from bup.io import byte_stream, path_msg
 from bup.options import _tty_width
 
@@ -145,7 +145,7 @@ subcmd[0] = subpath(subcmd_name)
 if not os.path.exists(subcmd[0]):
     usage('error: unknown command "%s"' % path_msg(subcmd_name))
 
-already_fixed = atoi(environ.get(b'BUP_FORCE_TTY'))
+already_fixed = int(environ.get(b'BUP_FORCE_TTY', 0))
 if subcmd_name in [b'mux', b'ftp', b'help']:
     already_fixed = True
 fix_stdout = not already_fixed and os.isatty(1)
index 1dd1ff3f720976d7a984c62908fb6f9f681df9df..62889a2ffc3c4901211cfdd2e0ed41d06c98fcdf 100755 (executable)
@@ -20,7 +20,7 @@ import os, sys
 sys.path[:0] = [os.path.dirname(os.path.realpath(__file__)) + '/..']
 
 from bup import compat, options, _helpers
-from bup.helpers import atoi, handle_ctrl_c, log, parse_num
+from bup.helpers import handle_ctrl_c, log, parse_num
 
 
 optspec = """
@@ -41,7 +41,7 @@ total = parse_num(extra[0])
 handle_ctrl_c()
 
 if opt.force or (not os.isatty(1) and
-                 not atoi(os.environ.get('BUP_FORCE_TTY')) & 1):
+                 not int(os.environ.get('BUP_FORCE_TTY', 0)) & 1):
     _helpers.write_random(sys.stdout.fileno(), total, opt.seed,
                           opt.verbose and 1 or 0)
 else: