Skip to content

Commit f2c045b

Browse files
Merge pull request #132 from MatthieuDartiailh/encode_varint_fixes
Fix encoding of 0 in varint
2 parents 1e9ebd3 + 1d9ea28 commit f2c045b

3 files changed

Lines changed: 8 additions & 2 deletions

File tree

doc/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ChangeLog
66

77
Bugfixes:
88

9+
- Fixes encoding of 0 as a varint PR #132
910
- Correct spelling of "INTRINSIC" in several places; this affected
1011
some ops in Python 3.12. PR #131
1112

src/bytecode/concrete.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,9 +818,10 @@ def _encode_varint(value: int, set_begin_marker: bool = False) -> Iterator[int]:
818818
while value:
819819
temp.append(value & 63 | (64 if temp else 0))
820820
value >>= 6
821+
temp = temp or [0]
821822
if set_begin_marker:
822823
temp[-1] |= 128
823-
return reversed(temp or [0])
824+
return reversed(temp)
824825

825826
def _assemble_exception_table(self) -> bytes:
826827
table = bytearray()

tests/test_concrete.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,6 @@ def test_cellvar_freevar(self):
529529
],
530530
)
531531

532-
# XXX adjust test for 3.12 in which load_classderef does not exist anymore
533532
def test_load_classderef(self):
534533
i_name = (
535534
"LOAD_FROM_DICT_OR_DEREF"
@@ -682,6 +681,11 @@ def test_copy(self):
682681
)
683682
self.assertInstructionListEqual(concrete, concrete.copy())
684683

684+
def test_encode_varint(self):
685+
self.assertListEqual(list(ConcreteBytecode._encode_varint(0)), [0])
686+
self.assertListEqual(list(ConcreteBytecode._encode_varint(0, True)), [128])
687+
self.assertListEqual(list(ConcreteBytecode._encode_varint(64, False)), [65, 0])
688+
685689

686690
class ConcreteFromCodeTests(TestCase):
687691
def test_extended_arg(self):

0 commit comments

Comments
 (0)