Skip to content

Commit 6d72dc8

Browse files
mballanceCopilot
andcommitted
Fix circular recursion in _accel fallback encode/decode_varints
The pure-Python fallback functions in _accel/__init__.py were calling back into ucis.ncdb.varint.encode_varints/decode_varints. Since varint.py successfully imports from _accel (no exception raised), _HAVE_ACCEL is True and varint's bulk functions delegate back to _accel, creating infinite mutual recursion (RecursionError in CI). Fix: inline the LEB128 encode/decode logic directly in the _accel fallback functions instead of re-importing from varint.py. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 695f7ed commit 6d72dc8

1 file changed

Lines changed: 30 additions & 4 deletions

File tree

src/ucis/ncdb/_accel/__init__.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,21 @@ def encode_varints(values) -> bytes:
6363
else:
6464
def encode_varints(values) -> bytes:
6565
"""Encode a sequence of non-negative ints as LEB128 (pure Python)."""
66-
from ucis.ncdb.varint import encode_varint
67-
return b"".join(encode_varint(v) for v in values)
66+
parts = []
67+
for value in values:
68+
if value < 0:
69+
raise ValueError(f"varint requires non-negative integer, got {value}")
70+
result = []
71+
while True:
72+
byte = value & 0x7F
73+
value >>= 7
74+
if value != 0:
75+
byte |= 0x80
76+
result.append(byte)
77+
if value == 0:
78+
break
79+
parts.append(bytes(result))
80+
return b"".join(parts)
6881

6982
# ── decode_varints ─────────────────────────────────────────────────────────
7083

@@ -89,8 +102,21 @@ def decode_varints(data: bytes, count: int, offset: int = 0):
89102
90103
Returns (list[int], new_offset).
91104
"""
92-
from ucis.ncdb.varint import decode_varints as _py_decode
93-
return _py_decode(data, count, offset)
105+
values = []
106+
for _ in range(count):
107+
result = 0
108+
shift = 0
109+
while True:
110+
if offset >= len(data):
111+
raise ValueError("Buffer too short for varint")
112+
byte = data[offset]
113+
offset += 1
114+
result |= (byte & 0x7F) << shift
115+
shift += 7
116+
if not (byte & 0x80):
117+
break
118+
values.append(result)
119+
return values, offset
94120

95121
# ── add_uint32_arrays ──────────────────────────────────────────────────────
96122

0 commit comments

Comments
 (0)