]> arthur.barton.de Git - bup.git/commitdiff
vint: remove unneded loop guards and improve checks
authorRob Browning <rlb@defaultvalue.org>
Sat, 26 Jan 2019 21:47:08 +0000 (15:47 -0600)
committerRob Browning <rlb@defaultvalue.org>
Sun, 3 Mar 2019 23:44:19 +0000 (17:44 -0600)
Don't bother to check the read/write loop guards; rely on checks in
the condiitonal arms.  Check read results for "not x" rather than "x
!= ''".

lib/bup/vint.py

index cd729ce82c27a4737d4cdf029962c19d2df65e95..e0e33804c395437f769c992ca7cbac72b0c8ca2c 100644 (file)
@@ -16,27 +16,32 @@ def write_vuint(port, x):
     elif x == 0:
         port.write('\0')
     else:
-        while x:
+        while True:
             seven_bits = x & 0x7f
             x >>= 7
             if x:
                 port.write(chr(0x80 | seven_bits))
             else:
                 port.write(chr(seven_bits))
+                break
 
 
 def read_vuint(port):
     c = port.read(1)
-    if c == '':
-        raise EOFError('encountered EOF while reading vuint');
+    if not c:
+        raise EOFError('encountered EOF while reading vuint')
+    if ord(c) == 0:
+        return 0
     result = 0
     offset = 0
-    while c:
+    while True:
         b = ord(c)
         if b & 0x80:
             result |= ((b & 0x7f) << offset)
             offset += 7
             c = port.read(1)
+            if not c:
+                raise EOFError('encountered EOF while reading vuint')
         else:
             result |= (b << offset)
             break
@@ -64,8 +69,8 @@ def write_vint(port, x):
 
 def read_vint(port):
     c = port.read(1)
-    if c == '':
-        raise EOFError('encountered EOF while reading vint');
+    if not c:
+        raise EOFError('encountered EOF while reading vint')
     negative = False
     result = 0
     offset = 0
@@ -82,12 +87,14 @@ def read_vint(port):
             return -result
         else:
             return result
-    while c:
+    while True:
         b = ord(c)
         if b & 0x80:
             result |= ((b & 0x7f) << offset)
             offset += 7
             c = port.read(1)
+            if not c:
+                raise EOFError('encountered EOF while reading vint')
         else:
             result |= (b << offset)
             break