You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-04 01:09:52 +08:00
Python2 comptability in using Python 3 disassembly
Also fixes ablility to run bytecode 3.5 tests from 2.x now For Python 2 reading python3 bytstrings, we need to make sure we confer the character to a number.
This commit is contained in:
@@ -20,7 +20,7 @@ check:
|
||||
$(MAKE) check-$$PYTHON_VERSION
|
||||
|
||||
#: Run working tests from Python 2.6 or 2.7
|
||||
check-2.6 check-2.7: check-bytecode-sans-3.5 check-2.7-ok
|
||||
check-2.6 check-2.7: check-bytecode check-2.7-ok
|
||||
|
||||
#: Run working tests from Python 3.2
|
||||
check-3.2: check-bytecode
|
||||
@@ -50,11 +50,6 @@ check-bytecode:
|
||||
$(PYTHON) test_pythonlib.py --bytecode-2.5 --bytecode-2.6 --bytecode-2.7 \
|
||||
--bytecode-3.2 --bytecode-3.3 --bytecode-3.4 --bytecode-3.5
|
||||
|
||||
#: Check deparsing bytecode only
|
||||
check-bytecode-sans-3.5:
|
||||
$(PYTHON) test_pythonlib.py --bytecode-2.5 --bytecode-2.6 --bytecode-2.7 \
|
||||
--bytecode-3.2 --bytecode-3.3 --bytecode-3.4
|
||||
|
||||
#: Check deparsing Python 2.5
|
||||
check-bytecode-2.5:
|
||||
$(PYTHON) test_pythonlib.py --bytecode-2.5
|
||||
|
@@ -1,5 +1,7 @@
|
||||
# This is taken from the python 3.x dis module
|
||||
"""Disassembler of Python byte code into mnemonics."""
|
||||
"""Disassembler of Python byte code into mnemonics.
|
||||
Extracted from Python 3 dis module but generalized to
|
||||
allow running on Python 2.
|
||||
"""
|
||||
|
||||
# This part is modified for cross Python compatability
|
||||
from uncompyle6.opcodes.opcode_3x import *
|
||||
@@ -294,6 +296,7 @@ def _get_const_info(const_index, const_list):
|
||||
argval = const_index
|
||||
if const_list is not None:
|
||||
argval = const_list[const_index]
|
||||
|
||||
return argval, repr(argval)
|
||||
|
||||
def _get_name_info(name_index, name_list):
|
||||
@@ -312,6 +315,10 @@ def _get_name_info(name_index, name_list):
|
||||
return argval, argrepr
|
||||
|
||||
|
||||
def code2num(code, i):
|
||||
op = code[i]
|
||||
return ord(op) if isinstance(op, str) else op
|
||||
|
||||
def _get_instructions_bytes(code, opnames, varnames=None, names=None, constants=None,
|
||||
cells=None, linestarts=None, line_offset=0):
|
||||
"""Iterate over the instructions in a bytecode string.
|
||||
@@ -330,12 +337,7 @@ def _get_instructions_bytes(code, opnames, varnames=None, names=None, constants=
|
||||
n = len(code)
|
||||
i = 0
|
||||
while i < n:
|
||||
op = code[i]
|
||||
if isinstance(op, str):
|
||||
op_num = ord(op)
|
||||
else:
|
||||
op_num = op
|
||||
|
||||
op = code2num(code, i)
|
||||
offset = i
|
||||
if linestarts is not None:
|
||||
starts_line = linestarts.get(i, None)
|
||||
@@ -347,10 +349,7 @@ def _get_instructions_bytes(code, opnames, varnames=None, names=None, constants=
|
||||
argval = None
|
||||
argrepr = ''
|
||||
if op >= HAVE_ARGUMENT:
|
||||
if isinstance(code[i], str):
|
||||
arg = op_num + ord(code[i+1])*256 + extended_arg
|
||||
else:
|
||||
arg = code[i] + code[i+1]*256 + extended_arg
|
||||
arg = code2num(code, i) + code2num(code, i+1)*256 + extended_arg
|
||||
extended_arg = 0
|
||||
i = i+2
|
||||
if op == EXTENDED_ARG:
|
||||
@@ -375,9 +374,9 @@ def _get_instructions_bytes(code, opnames, varnames=None, names=None, constants=
|
||||
elif op in hasfree:
|
||||
argval, argrepr = _get_name_info(arg, cells)
|
||||
elif op in hasnargs:
|
||||
argrepr = ("%d positional, %d keyword pair, %d annotated" %
|
||||
(code[i-2], code[i-1], code[i]))
|
||||
opname = opnames[op_num]
|
||||
argrepr = ("%d positional, %d keyword pair" %
|
||||
(code2num(code, i-2), code2num(code, i-1)))
|
||||
opname = opnames[op]
|
||||
yield Instruction3(opname, op,
|
||||
arg, argval, argrepr,
|
||||
offset, starts_line, is_jump_target)
|
||||
@@ -394,10 +393,10 @@ def findlabels(code):
|
||||
n = len(code)
|
||||
i = 0
|
||||
while i < n:
|
||||
op = code[i]
|
||||
op = code2num(code, i)
|
||||
i = i+1
|
||||
if op >= HAVE_ARGUMENT:
|
||||
arg = code[i] + code[i+1]*256
|
||||
arg = code2num(code, i) + code2num(code, i+1)*256
|
||||
i = i+2
|
||||
label = -1
|
||||
if op in hasjrel:
|
||||
|
Reference in New Issue
Block a user