[PATCH 1/5] encode_vint/encode_vuint: move fallback code out of except

0 views
Skip to first unread message

Rob Browning

unread,
Mar 14, 2026, 4:57:45 PM (10 days ago) Mar 14
to bup-...@googlegroups.com
Signed-off-by: Rob Browning <r...@defaultvalue.org>
Tested-by: Rob Browning <r...@defaultvalue.org>
---
lib/bup/vint.py | 44 ++++++++++++++++++++++----------------------
1 file changed, 22 insertions(+), 22 deletions(-)

diff --git a/lib/bup/vint.py b/lib/bup/vint.py
index 45a2c57c..5c4b92e9 100644
--- a/lib/bup/vint.py
+++ b/lib/bup/vint.py
@@ -20,20 +20,19 @@ def encode_vuint(x):
try:
return _helpers.vuint_encode(x)
except OverflowError:
- ret = b''
if x < 0:
- raise Exception("vuints must not be negative") from None
- assert x, "the C version should have picked this up"
-
- while True:
- seven_bits = x & 0x7f
- x >>= 7
- if x:
- ret += (0x80 | seven_bits).to_bytes(1, 'big')
- else:
- ret += seven_bits.to_bytes(1, 'big')
- break
- return ret
+ raise Exception(f'cannot encode negative vuint {x}') from None
+ assert x != 0, 'the C code did not handle zero'
+ ret = b''
+ while True:
+ seven_bits = x & 0x7f
+ x >>= 7
+ if x:
+ ret += (0x80 | seven_bits).to_bytes(1, 'big')
+ else:
+ ret += seven_bits.to_bytes(1, 'big')
+ break
+ return ret


def read_vuint(port):
@@ -69,15 +68,16 @@ def encode_vint(x):
try:
return _helpers.vint_encode(x)
except OverflowError:
- assert x != 0, "the C version should have picked this up"
- if x < 0:
- x = -x
- sign_and_six_bits = (x & 0x3f) | 0x40
- else:
- sign_and_six_bits = x & 0x3f
- x >>= 6
- assert x, "the C version should have picked this up"
- return (0x80 | sign_and_six_bits).to_bytes(1, 'big') + encode_vuint(x)
+ pass
+ assert x != 0, 'the C code did not handle zero'
+ if x < 0:
+ x = -x
+ sign_and_six_bits = (x & 0x3f) | 0x40
+ else:
+ sign_and_six_bits = x & 0x3f
+ x >>= 6
+ assert x != 0, f'the C code did not handle the small value {x}'
+ return (0x80 | sign_and_six_bits).to_bytes(1, 'big') + encode_vuint(x)


def read_vint(port):
--
2.47.3

Reply all
Reply to author
Forward
0 new messages