Skip to content

Commit

Permalink
Do not delete EXTENDED_ARG instructions that have no arg (#24)
Browse files Browse the repository at this point in the history
* Fix for missing / mismatched jump labels in ConcreteBytecode.to_bytecode

* Add test that fails without fix and passes with fix

* Fix style in bytecode/tests/test_concrete.py

* increment to v0.5.1

* update version to 0.5.1
  • Loading branch information
localhuman authored and MatthieuDartiailh committed Mar 19, 2018
1 parent a5d01f7 commit 63f1ec9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bytecode/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.5'
__version__ = '0.5.1'

__all__ = ['Label', 'Instr', 'SetLineno', 'Bytecode',
'ConcreteInstr', 'ConcreteBytecode',
Expand Down
6 changes: 4 additions & 2 deletions bytecode/concrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,10 @@ def from_code(code, *, extended_arg=False):
extended_arg = (extended_arg << 8) + instr.arg
else:
extended_arg = instr.arg
del instructions[index]
continue

if extended_arg > 0:
del instructions[index]
continue

if extended_arg is not None:
if _WORDCODE:
Expand Down
48 changes: 48 additions & 0 deletions bytecode/tests/test_concrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,54 @@ def test_label2(self):
self.assertListEqual(concrete.names, ['test', 'x'])
self.assertListEqual(concrete.varnames, [])

def test_label3(self):
"""
When you delete ``extended_arg`` that have a value of 0, the following
will fail when calling ``Bytecode.to_concrete_bytecode()`` because
it cant find a label to correspond to the jump target
"""
code = get_code("""
def func(x):
if x == 1:
return x +1
elif x == 2:
return x + 1
elif x == 3:
return x + 1
elif x == 4:
return x + 1
elif x == 5:
return x + 1
elif x == 6:
return x + 1
elif x == 7:
return x + 1
elif x == 8:
return x + 1
elif x == 9:
return x + 1
elif x == 10:
return x + 1
elif x == 11:
return x + 1
elif x == 12:
return x + 1
elif x == 13:
return x + 1
elif x == 14:
return x + 1
elif x == 15:
return x + 1
elif x == 16:
return x + 1
elif x == 17:
return x + 1
return -1
""", function=True)
code = Bytecode.from_code(code)
concrete = code.to_concrete_bytecode()
self.assertIsInstance(concrete, ConcreteBytecode)

def test_setlineno(self):
# x = 7
# y = 8
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# - git commit -a -m "post-release"
# - git push

VERSION = '0.5'
VERSION = '0.5.1'

DESCRIPTION = 'Python module to generate and modify bytecode'
CLASSIFIERS = [
Expand Down

0 comments on commit 63f1ec9

Please sign in to comment.