Sync from decompyle3

This commit is contained in:
rocky
2022-11-06 01:24:28 -05:00
parent 283e493270
commit 514b0d0b0c
2 changed files with 26 additions and 19 deletions

View File

@@ -37,7 +37,7 @@ def while1stmt(self, lhs, n, rule, ast, tokens, first, last):
if tokens[loop_end] == "JUMP_BACK": if tokens[loop_end] == "JUMP_BACK":
loop_end += 1 loop_end += 1
loop_end_offset = tokens[loop_end].off2int(prefer_last=False) loop_end_offset = tokens[loop_end].off2int(prefer_last=False)
for t in range(first+1, loop_end): for t in range(first + 1, loop_end):
token = tokens[t] token = tokens[t]
# token could be a pseudo-op like "LOAD_STR", which is not in # token could be a pseudo-op like "LOAD_STR", which is not in
# token.opc. We will replace that with LOAD_CONST as an # token.opc. We will replace that with LOAD_CONST as an
@@ -46,6 +46,5 @@ def while1stmt(self, lhs, n, rule, ast, tokens, first, last):
if token.attr >= loop_end_offset: if token.attr >= loop_end_offset:
return True return True
# SETUP_LOOP location must jump either to the last token or the token after the last one # SETUP_LOOP location must jump either to the last token or the token after the last one
return tokens[first].attr not in (offset, offset + 2) return tokens[first].attr not in (offset, offset + 2)

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2019-2021 by Rocky Bernstein # Copyright (c) 2019-2022 by Rocky Bernstein
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@@ -156,7 +156,7 @@ def make_function36(self, node, is_lambda, nested=1, code_node=None):
defparams.reverse() defparams.reverse()
try: try:
ast = self.build_ast( tree = self.build_ast(
scanner_code._tokens, scanner_code._tokens,
scanner_code._customize, scanner_code._customize,
scanner_code, scanner_code,
@@ -176,7 +176,9 @@ def make_function36(self, node, is_lambda, nested=1, code_node=None):
if defparams: if defparams:
for i, defparam in enumerate(defparams): for i, defparam in enumerate(defparams):
params.append( params.append(
build_param(paramnames[i], defparam, annotate_dict.get(paramnames[i])) build_param(
tree, paramnames[i], defparam, annotate_dict.get(paramnames[i])
)
) )
for param in paramnames[i + 1 :]: for param in paramnames[i + 1 :]:
@@ -204,7 +206,13 @@ def make_function36(self, node, is_lambda, nested=1, code_node=None):
# dump parameter list (with default values) # dump parameter list (with default values)
if is_lambda: if is_lambda:
self.write("lambda ", ", ".join(params)) self.write("lambda")
if len(params):
self.write(" ", ", ".join(params))
elif kwonlyargcount > 0 and not (4 & code.co_flags):
assert argc == 0
self.write(" ")
# If the last statement is None (which is the # If the last statement is None (which is the
# same thing as "return None" in a lambda) and the # same thing as "return None" in a lambda) and the
# next to last statement is a "yield". Then we want to # next to last statement is a "yield". Then we want to
@@ -212,16 +220,16 @@ def make_function36(self, node, is_lambda, nested=1, code_node=None):
# to have something to after the yield finishes. # to have something to after the yield finishes.
# FIXME: this is a bit hoaky and not general # FIXME: this is a bit hoaky and not general
if ( if (
len(ast) > 1 len(tree) > 1
and self.traverse(ast[-1]) == "None" and self.traverse(tree[-1]) == "None"
and self.traverse(ast[-2]).strip().startswith("yield") and self.traverse(tree[-2]).strip().startswith("yield")
): ):
del ast[-1] del tree[-1]
# Now pick out the expr part of the last statement # Now pick out the expr part of the last statement
ast_expr = ast[-1] tree_expr = tree[-1]
while ast_expr.kind != "expr": while tree_expr.kind != "expr":
ast_expr = ast_expr[0] tree_expr = tree_expr[0]
ast[-1] = ast_expr tree[-1] = tree_expr
pass pass
else: else:
self.write("(", ", ".join(params)) self.write("(", ", ".join(params))
@@ -331,11 +339,11 @@ def make_function36(self, node, is_lambda, nested=1, code_node=None):
# docstring exists, dump it # docstring exists, dump it
self.println(self.traverse(node[-2])) self.println(self.traverse(node[-2]))
assert ast == "stmts" assert tree in ("stmts", "lambda_start")
all_globals = find_all_globals(ast, set()) all_globals = find_all_globals(tree, set())
globals, nonlocals = find_globals_and_nonlocals( globals, nonlocals = find_globals_and_nonlocals(
ast, set(), set(), code, self.version tree, set(), set(), code, self.version
) )
for g in sorted((all_globals & self.mod_globs) | globals): for g in sorted((all_globals & self.mod_globs) | globals):
@@ -346,9 +354,9 @@ def make_function36(self, node, is_lambda, nested=1, code_node=None):
self.mod_globs -= all_globals self.mod_globs -= all_globals
has_none = "None" in code.co_names has_none = "None" in code.co_names
rn = has_none and not find_none(ast) rn = has_none and not find_none(tree)
self.gen_source( self.gen_source(
ast, tree,
code.co_name, code.co_name,
scanner_code._customize, scanner_code._customize,
is_lambda=is_lambda, is_lambda=is_lambda,