Make work on 2.4

This commit is contained in:
rocky
2016-11-23 08:26:12 -05:00
parent 590231741d
commit 7133540c23
13 changed files with 120 additions and 34 deletions

View File

@@ -37,7 +37,7 @@ check-3.0 check-3.1 check-3.2 check-3.5 check-3.6:
$(MAKE) -C test $@ $(MAKE) -C test $@
#:Tests for Python 2.6 (doesn't have pytest) #:Tests for Python 2.6 (doesn't have pytest)
check-2.5 check-2.6: check-2.4 check-2.5 check-2.6:
$(MAKE) -C test $@ $(MAKE) -C test $@
#:PyPy 2.6.1 or PyPy 5.0.1 #:PyPy 2.6.1 or PyPy 5.0.1

View File

@@ -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.5 check-2.6 check-2.7: check-bytecode-2 check-bytecode-3 check-bytecode-1 check-2.7-ok check-2.4 check-2.5 check-2.6 check-2.7: check-bytecode-2 check-bytecode-3 check-bytecode-1 check-2.7-ok
#: Run working tests from Python 3.1 #: Run working tests from Python 3.1
check-3.0: check-bytecode check-3.0: check-bytecode

View File

@@ -21,13 +21,25 @@ def uncompyle(
# store final output stream for case of error # store final output stream for case of error
real_out = out or sys.stdout real_out = out or sys.stdout
co_pypy_str = 'PyPy ' if is_pypy else '' if is_pypy:
run_pypy_str = 'PyPy ' if IS_PYPY else '' co_pypy_str = 'PyPy '
else:
co_pypy_str = ''
if IS_PYPY:
run_pypy_str = 'PyPy '
else:
run_pypy_str = ''
if magic_int:
m = str(magic_int)
else:
m = ""
real_out.write('# uncompyle6 version %s\n' real_out.write('# uncompyle6 version %s\n'
'# %sPython bytecode %s%s\n# Decompiled from: %sPython %s\n' % '# %sPython bytecode %s%s\n# Decompiled from: %sPython %s\n' %
(VERSION, co_pypy_str, bytecode_version, (VERSION, co_pypy_str, bytecode_version,
" (%d)" % magic_int if magic_int else "", " (%s)" % m, run_pypy_str,
run_pypy_str, '\n# '.join(sys.version.split('\n')))) '\n# '.join(sys.version.split('\n'))))
if co.co_filename: if co.co_filename:
real_out.write('# Embedded file name: %s\n' % co.co_filename) real_out.write('# Embedded file name: %s\n' % co.co_filename)
if timestamp: if timestamp:

View File

@@ -78,7 +78,10 @@ class PythonParser(GenericASTBuilder):
err_token = instructions[index] err_token = instructions[index]
print("Instruction context:") print("Instruction context:")
for i in range(start, finish): for i in range(start, finish):
indent = ' ' if i != index else '-> ' if i != index:
indent = ' '
else:
indent = '-> '
print("%s%s" % (indent, instructions[i])) print("%s%s" % (indent, instructions[i]))
raise ParserError(err_token, err_token.offset) raise ParserError(err_token, err_token.offset)

View File

@@ -453,7 +453,10 @@ class Python3Parser(PythonParser):
"""Python 3.3 added a an addtional LOAD_CONST before MAKE_FUNCTION and """Python 3.3 added a an addtional LOAD_CONST before MAKE_FUNCTION and
this has an effect on many rules. this has an effect on many rules.
""" """
new_rule = rule % (('LOAD_CONST ') * (1 if self.version >= 3.3 else 0)) if self.version >= 3.3:
new_rule = rule % (('LOAD_CONST ') * 1)
else:
new_rule = rule % (('LOAD_CONST ') * 0)
self.add_unique_rule(new_rule, opname, attr, customize) self.add_unique_rule(new_rule, opname, attr, customize)
def add_custom_rules(self, tokens, customize): def add_custom_rules(self, tokens, customize):

View File

@@ -83,7 +83,9 @@ class Scanner2(scan.Scanner):
cause specific rules for the specific number of arguments they take. cause specific rules for the specific number of arguments they take.
""" """
show_asm = self.show_asm if not show_asm else show_asm if not show_asm:
show_asm = self.show_asm
# show_asm = 'after' # show_asm = 'after'
if show_asm in ('both', 'before'): if show_asm in ('both', 'before'):
from xdis.bytecode import Bytecode from xdis.bytecode import Bytecode
@@ -908,8 +910,10 @@ class Scanner2(scan.Scanner):
# FIXME: rocky: I think we need something like this... # FIXME: rocky: I think we need something like this...
if offset not in set(self.ignore_if) or self.version == 2.7: if offset not in set(self.ignore_if) or self.version == 2.7:
source = (self.setup_loops[label] if label in self.setup_loops:
if label in self.setup_loops else offset) source = self.setup_loops[label]
else:
source = offset
targets[label] = targets.get(label, []) + [source] targets[label] = targets.get(label, []) + [source]
pass pass

View File

@@ -86,7 +86,9 @@ class Scanner26(scan.Scanner2):
cause specific rules for the specific number of arguments they take. cause specific rules for the specific number of arguments they take.
""" """
show_asm = self.show_asm if not show_asm else show_asm if not show_asm:
show_asm = self.show_asm
# show_asm = 'after' # show_asm = 'after'
if show_asm in ('both', 'before'): if show_asm in ('both', 'before'):
from xdis.bytecode import Bytecode from xdis.bytecode import Bytecode

View File

@@ -146,7 +146,9 @@ class Scanner3(Scanner):
cause specific rules for the specific number of arguments they take. cause specific rules for the specific number of arguments they take.
""" """
show_asm = self.show_asm if not show_asm else show_asm if not show_asm:
show_asm = self.show_asm
# show_asm = 'after' # show_asm = 'after'
if show_asm in ('both', 'before'): if show_asm in ('both', 'before'):
bytecode = Bytecode(co, self.opc) bytecode = Bytecode(co, self.opc)

View File

@@ -56,12 +56,18 @@ class Token:
return self.format(line_prefix='') return self.format(line_prefix='')
def format(self, line_prefix=''): def format(self, line_prefix=''):
prefix = ('\n%s%4d ' % (line_prefix, self.linestart) if self.linestart:
if self.linestart else (' ' * (6 + len(line_prefix)))) prefix = '\n%s%4d ' % (line_prefix, self.linestart)
else:
prefix = ' ' * (6 + len(line_prefix))
offset_opname = '%6s %-17s' % (self.offset, self.type) offset_opname = '%6s %-17s' % (self.offset, self.type)
if not self.has_arg: if not self.has_arg:
return "%s%s" % (prefix, offset_opname) return "%s%s" % (prefix, offset_opname)
argstr = "%6d " % self.attr if isinstance(self.attr, int) else (' '*7)
if isinstance(self.attr, int):
argstr = "%6d " % self.attr
else:
argstr = ' '*7
if self.pattr: if self.pattr:
pattr = self.pattr pattr = self.pattr
if self.opc: if self.opc:

View File

@@ -732,7 +732,10 @@ class FragmentsWalker(pysource.SourceWalker, object):
def n_genexpr(self, node): def n_genexpr(self, node):
start = len(self.f.getvalue()) start = len(self.f.getvalue())
self.write('(') self.write('(')
code_index = -6 if self.version > 3.2 else -5 if self.version > 3.2:
code_index = -6
else:
code_index = -5
self.comprehension_walk(node, iter_index=3, code_index=code_index) self.comprehension_walk(node, iter_index=3, code_index=code_index)
self.write(')') self.write(')')
self.set_pos_info(node, start, len(self.f.getvalue())) self.set_pos_info(node, start, len(self.f.getvalue()))
@@ -886,7 +889,10 @@ class FragmentsWalker(pysource.SourceWalker, object):
subclass = n.attr subclass = n.attr
break break
pass pass
subclass_info = node if node == 'classdefdeco2' else node[0] if node == 'classdefdeco2':
subclass_info = node
else:
subclass_info = node[0]
elif buildclass[1][0] == 'load_closure': elif buildclass[1][0] == 'load_closure':
# Python 3 with closures not functions # Python 3 with closures not functions
load_closure = buildclass[1] load_closure = buildclass[1]
@@ -910,7 +916,10 @@ class FragmentsWalker(pysource.SourceWalker, object):
subclass = buildclass[1][0].attr subclass = buildclass[1][0].attr
subclass_info = node[0] subclass_info = node[0]
else: else:
buildclass = node if (node == 'classdefdeco2') else node[0] if node == 'classdefdeco2':
buildclass = node
else:
buildclass = node[0]
build_list = buildclass[1][0] build_list = buildclass[1][0]
if hasattr(buildclass[-3][0], 'attr'): if hasattr(buildclass[-3][0], 'attr'):
subclass = buildclass[-3][0].attr subclass = buildclass[-3][0].attr

View File

@@ -151,7 +151,10 @@ def make_function3_annotate(self, node, isLambda, nested=1,
self.write(': %s' % value) self.write(': %s' % value)
suffix = ', ' suffix = ', '
suffix = ', ' if i > 0 else '' if i > 0:
suffix = ', '
else:
suffix = ''
for n in node: for n in node:
if n == 'pos_arg': if n == 'pos_arg':
self.write(suffix) self.write(suffix)
@@ -301,7 +304,10 @@ def make_function2(self, node, isLambda, nested=1, codeNode=None):
self.ERROR = p self.ERROR = p
return return
kw_pairs = args_node.attr[1] if self.version >= 3.0 else 0 if self.version >= 3.0:
kw_pairs = args_node.attr[1]
else:
kw_pairs = 0
indent = self.indent indent = self.indent
# build parameters # build parameters
@@ -438,7 +444,10 @@ def make_function3(self, node, isLambda, nested=1, codeNode=None):
self.ERROR = p self.ERROR = p
return return
kw_pairs = args_node.attr[1] if self.version >= 3.0 else 0 if self.version >= 3.0:
kw_pairs = args_node.attr[1]
else:
kw_pairs = 0
indent = self.indent indent = self.indent
# build parameters # build parameters
@@ -480,7 +489,10 @@ def make_function3(self, node, isLambda, nested=1, codeNode=None):
i = len(paramnames) - len(defparams) i = len(paramnames) - len(defparams)
self.write(", ".join(paramnames[:i])) self.write(", ".join(paramnames[:i]))
suffix = ', ' if i > 0 else '' if i > 0:
suffix = ', '
else:
suffix = ''
for n in node: for n in node:
if n == 'pos_arg': if n == 'pos_arg':
self.write(suffix) self.write(suffix)

View File

@@ -600,7 +600,10 @@ class SourceWalker(GenericASTTraversal, object):
code = node[-3] code = node[-3]
self.indentMore() self.indentMore()
annotate_last = -4 if self.version == 3.1 else -5 if self.version == 3.1:
annotate_last = -4
else:
annotate_last = -5
# FIXME: handle and pass full annotate args # FIXME: handle and pass full annotate args
make_function3_annotate(self, node, isLambda=False, make_function3_annotate(self, node, isLambda=False,
@@ -1114,7 +1117,10 @@ class SourceWalker(GenericASTTraversal, object):
return return
n = node[-1] n = node[-1]
elif node[-1] == 'del_stmt': elif node[-1] == 'del_stmt':
n = node[-3] if node[-2] == 'JUMP_BACK' else node[-2] if node[-2] == 'JUMP_BACK':
n = node[-3]
else:
n = node[-2]
assert n == 'list_iter' assert n == 'list_iter'
@@ -1132,7 +1138,10 @@ class SourceWalker(GenericASTTraversal, object):
list_iter = node[-1] list_iter = node[-1]
else: else:
expr = n[1] expr = n[1]
list_iter = node[-3] if node[-2] == 'JUMP_BACK' else node[-2] if node[-2] == 'JUMP_BACK':
list_iter = node[-3]
else:
list_iter = node[-2]
assert expr == 'expr' assert expr == 'expr'
assert list_iter == 'list_iter' assert list_iter == 'list_iter'
@@ -1174,7 +1183,10 @@ class SourceWalker(GenericASTTraversal, object):
self.write( '[ ') self.write( '[ ')
expr = n[0] expr = n[0]
list_iter = node[-2] if self.is_pypy and node[-1] == 'JUMP_BACK' else node[-1] if self.is_pypy and node[-1] == 'JUMP_BACK':
list_iter = node[-2]
else:
list_iter = node[-1]
assert expr == 'expr' assert expr == 'expr'
assert list_iter == 'list_iter' assert list_iter == 'list_iter'
@@ -1248,7 +1260,10 @@ class SourceWalker(GenericASTTraversal, object):
self.write(' for ') self.write(' for ')
self.preorder(ast[iter_index-1]) self.preorder(ast[iter_index-1])
self.write(' in ') self.write(' in ')
iter_expr = node[2] if node[2] == 'expr' else node[-3] if node[2] == 'expr':
iter_expr = node[2]
else:
iter_expr = node[-3]
assert iter_expr == 'expr' assert iter_expr == 'expr'
self.preorder(iter_expr) self.preorder(iter_expr)
self.preorder(ast[iter_index]) self.preorder(ast[iter_index])
@@ -1256,7 +1271,10 @@ class SourceWalker(GenericASTTraversal, object):
def n_genexpr(self, node): def n_genexpr(self, node):
self.write('(') self.write('(')
code_index = -6 if self.version > 3.2 else -5 if self.version > 3.2:
code_index = -6
else:
code_index = -5
self.comprehension_walk(node, iter_index=3, code_index=code_index) self.comprehension_walk(node, iter_index=3, code_index=code_index)
self.write(')') self.write(')')
self.prune() self.prune()
@@ -1490,7 +1508,10 @@ class SourceWalker(GenericASTTraversal, object):
break break
pass pass
pass pass
subclass_info = node if node == 'classdefdeco2' else node[0] if node == 'classdefdeco2':
subclass_info = node
else:
subclass_info = node[0]
elif buildclass[1][0] == 'load_closure': elif buildclass[1][0] == 'load_closure':
# Python 3 with closures not functions # Python 3 with closures not functions
load_closure = buildclass[1] load_closure = buildclass[1]
@@ -1514,7 +1535,10 @@ class SourceWalker(GenericASTTraversal, object):
subclass = buildclass[1][0].attr subclass = buildclass[1][0].attr
subclass_info = node[0] subclass_info = node[0]
else: else:
buildclass = node if (node == 'classdefdeco2') else node[0] if node == 'classdefdeco2':
buildclass = node
else:
buildclass = node[0]
build_list = buildclass[1][0] build_list = buildclass[1][0]
if hasattr(buildclass[-3][0], 'attr'): if hasattr(buildclass[-3][0], 'attr'):
subclass = buildclass[-3][0].attr subclass = buildclass[-3][0].attr

View File

@@ -12,7 +12,10 @@ def maybe_show_asm(showasm, tokens):
:param tokens: The asm tokens to show. :param tokens: The asm tokens to show.
""" """
if showasm: if showasm:
stream = showasm if hasattr(showasm, 'write') else sys.stdout if hasattr(showasm, 'write'):
stream = showasm
else:
stream = sys.stdout
for t in tokens: for t in tokens:
stream.write(str(t)) stream.write(str(t))
stream.write('\n') stream.write('\n')
@@ -29,7 +32,10 @@ def maybe_show_ast(showast, ast):
:param ast: The ast to show. :param ast: The ast to show.
""" """
if showast: if showast:
stream = showast if hasattr(showast, 'write') else sys.stdout if hasattr(showast, 'write'):
stream = showast
else:
stream = sys.stdout
stream.write(str(ast)) stream.write(str(ast))
stream.write('\n') stream.write('\n')
@@ -48,7 +54,10 @@ def maybe_show_ast_param_default(showast, name, default):
:param default: The function parameter default. :param default: The function parameter default.
""" """
if showast: if showast:
stream = showast if hasattr(showast, 'write') else sys.stdout if hasattr(showast, 'write'):
stream = showast
else:
stream = sys.stdout
stream.write('\n') stream.write('\n')
stream.write('--' + name) stream.write('--' + name)
stream.write('\n') stream.write('\n')