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
|
$(MAKE) check-$$PYTHON_VERSION
|
||||||
|
|
||||||
#: Run working tests from Python 2.6 or 2.7
|
#: 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
|
#: Run working tests from Python 3.2
|
||||||
check-3.2: check-bytecode
|
check-3.2: check-bytecode
|
||||||
@@ -50,11 +50,6 @@ check-bytecode:
|
|||||||
$(PYTHON) test_pythonlib.py --bytecode-2.5 --bytecode-2.6 --bytecode-2.7 \
|
$(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
|
--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 deparsing Python 2.5
|
||||||
check-bytecode-2.5:
|
check-bytecode-2.5:
|
||||||
$(PYTHON) test_pythonlib.py --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
|
# This part is modified for cross Python compatability
|
||||||
from uncompyle6.opcodes.opcode_3x import *
|
from uncompyle6.opcodes.opcode_3x import *
|
||||||
@@ -294,6 +296,7 @@ def _get_const_info(const_index, const_list):
|
|||||||
argval = const_index
|
argval = const_index
|
||||||
if const_list is not None:
|
if const_list is not None:
|
||||||
argval = const_list[const_index]
|
argval = const_list[const_index]
|
||||||
|
|
||||||
return argval, repr(argval)
|
return argval, repr(argval)
|
||||||
|
|
||||||
def _get_name_info(name_index, name_list):
|
def _get_name_info(name_index, name_list):
|
||||||
@@ -312,6 +315,10 @@ def _get_name_info(name_index, name_list):
|
|||||||
return argval, argrepr
|
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,
|
def _get_instructions_bytes(code, opnames, varnames=None, names=None, constants=None,
|
||||||
cells=None, linestarts=None, line_offset=0):
|
cells=None, linestarts=None, line_offset=0):
|
||||||
"""Iterate over the instructions in a bytecode string.
|
"""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)
|
n = len(code)
|
||||||
i = 0
|
i = 0
|
||||||
while i < n:
|
while i < n:
|
||||||
op = code[i]
|
op = code2num(code, i)
|
||||||
if isinstance(op, str):
|
|
||||||
op_num = ord(op)
|
|
||||||
else:
|
|
||||||
op_num = op
|
|
||||||
|
|
||||||
offset = i
|
offset = i
|
||||||
if linestarts is not None:
|
if linestarts is not None:
|
||||||
starts_line = linestarts.get(i, None)
|
starts_line = linestarts.get(i, None)
|
||||||
@@ -347,10 +349,7 @@ def _get_instructions_bytes(code, opnames, varnames=None, names=None, constants=
|
|||||||
argval = None
|
argval = None
|
||||||
argrepr = ''
|
argrepr = ''
|
||||||
if op >= HAVE_ARGUMENT:
|
if op >= HAVE_ARGUMENT:
|
||||||
if isinstance(code[i], str):
|
arg = code2num(code, i) + code2num(code, i+1)*256 + extended_arg
|
||||||
arg = op_num + ord(code[i+1])*256 + extended_arg
|
|
||||||
else:
|
|
||||||
arg = code[i] + code[i+1]*256 + extended_arg
|
|
||||||
extended_arg = 0
|
extended_arg = 0
|
||||||
i = i+2
|
i = i+2
|
||||||
if op == EXTENDED_ARG:
|
if op == EXTENDED_ARG:
|
||||||
@@ -375,9 +374,9 @@ def _get_instructions_bytes(code, opnames, varnames=None, names=None, constants=
|
|||||||
elif op in hasfree:
|
elif op in hasfree:
|
||||||
argval, argrepr = _get_name_info(arg, cells)
|
argval, argrepr = _get_name_info(arg, cells)
|
||||||
elif op in hasnargs:
|
elif op in hasnargs:
|
||||||
argrepr = ("%d positional, %d keyword pair, %d annotated" %
|
argrepr = ("%d positional, %d keyword pair" %
|
||||||
(code[i-2], code[i-1], code[i]))
|
(code2num(code, i-2), code2num(code, i-1)))
|
||||||
opname = opnames[op_num]
|
opname = opnames[op]
|
||||||
yield Instruction3(opname, op,
|
yield Instruction3(opname, op,
|
||||||
arg, argval, argrepr,
|
arg, argval, argrepr,
|
||||||
offset, starts_line, is_jump_target)
|
offset, starts_line, is_jump_target)
|
||||||
@@ -394,10 +393,10 @@ def findlabels(code):
|
|||||||
n = len(code)
|
n = len(code)
|
||||||
i = 0
|
i = 0
|
||||||
while i < n:
|
while i < n:
|
||||||
op = code[i]
|
op = code2num(code, i)
|
||||||
i = i+1
|
i = i+1
|
||||||
if op >= HAVE_ARGUMENT:
|
if op >= HAVE_ARGUMENT:
|
||||||
arg = code[i] + code[i+1]*256
|
arg = code2num(code, i) + code2num(code, i+1)*256
|
||||||
i = i+2
|
i = i+2
|
||||||
label = -1
|
label = -1
|
||||||
if op in hasjrel:
|
if op in hasjrel:
|
||||||
|
Reference in New Issue
Block a user