ifelsestmt transform must object types more

This commit is contained in:
rocky
2020-01-26 10:12:26 -05:00
parent b893a9ae21
commit e80e72e6ab
2 changed files with 37 additions and 17 deletions

View File

@@ -101,7 +101,9 @@ def decompile(
)
)
if PYTHON_VERSION < 3.0 and bytecode_version >= 3.0:
write("# Warning: this version has problems handling the Python 3 byte type in contants properly.\n")
write(
"# Warning: this version has problems handling the Python 3 byte type in contants properly.\n"
)
if co.co_filename:
write("# Embedded file name: %s" % co.co_filename,)

View File

@@ -30,14 +30,18 @@ def is_docstring(node):
except:
return False
def is_not_docstring(call_stmt_node):
try:
return (call_stmt_node == "call_stmt" and
call_stmt_node[0][0] == "LOAD_STR" and
call_stmt_node[1] == "POP_TOP")
return (
call_stmt_node == "call_stmt"
and call_stmt_node[0][0] == "LOAD_STR"
and call_stmt_node[1] == "POP_TOP"
)
except:
return False
class TreeTransform(GenericASTTraversal, object):
def __init__(self, version, show_ast=None, is_pypy=False):
self.version = version
@@ -133,7 +137,7 @@ class TreeTransform(GenericASTTraversal, object):
and 1 <= len(testtrue_or_false) <= 2
and raise_stmt.first_child().pattr == "AssertionError"
):
if testtrue_or_false == "testtrue":
if testtrue_or_false == "testtrue":
# Skip over the testtrue because because it would
# produce a "not" and we don't want that here.
assert_expr = testtrue_or_false[0]
@@ -182,7 +186,7 @@ class TreeTransform(GenericASTTraversal, object):
RAISE_VARARGS_1,
],
)
node.transformed_by="n_ifstmt"
node.transformed_by = "n_ifstmt"
pass
pass
else:
@@ -264,9 +268,19 @@ class TreeTransform(GenericASTTraversal, object):
else_suite_index = 2
pass
pass
elif len(n) > 1 and 1 == len(n[0]) and n[0] == "stmt" and n[1].kind == "stmt":
elif (
len(n) > 1
and isinstance(n[0], SyntaxTree)
and 1 == len(n[0])
and n[0] == "stmt"
and n[1].kind == "stmt"
):
else_suite_stmts = n[0]
if else_suite_stmts[0].kind not in ("ifstmt", "iflaststmt", "ifelsestmtl"):
if else_suite_stmts[0].kind not in (
"ifstmt",
"iflaststmt",
"ifelsestmtl",
):
return node
old_stmts = n
n = else_suite_stmts[0]
@@ -327,14 +341,14 @@ class TreeTransform(GenericASTTraversal, object):
# import_from37 ::= LOAD_CONST LOAD_CONST IMPORT_NAME_ATTR importlist37 POP_TOP
# import_as37 ::= LOAD_CONST LOAD_CONST importlist37 store POP_TOP
# 'import_as37': ( '%|import %c as %c\n', 2, -2),
node = SyntaxTree("import_as37",
[node[0], node[1], import_name_attr, store, node[-1]])
node.transformed_by="n_import_from37"
node = SyntaxTree(
"import_as37", [node[0], node[1], import_name_attr, store, node[-1]]
)
node.transformed_by = "n_import_from37"
pass
pass
return node
def n_list_for(self, list_for_node):
expr = list_for_node[0]
if expr == "expr" and expr[0] == "get_iter":
@@ -350,14 +364,18 @@ class TreeTransform(GenericASTTraversal, object):
new_stmts = [node[0]]
for i, sstmt in enumerate(node[1:]):
ann_assign = sstmt[0][0]
if (sstmt[0] == "stmt" and ann_assign == "ann_assign" and prev == "assign"):
if (
sstmt[0] == "stmt"
and ann_assign == "ann_assign"
and prev == "assign"
):
annotate_var = ann_assign[-2]
if annotate_var.attr == prev[-1][0].attr:
del new_stmts[-1]
sstmt[0][0] = SyntaxTree(
"ann_assign_init",
[ann_assign[0], prev[0], annotate_var])
sstmt[0][0].transformed_by="n_stmts"
"ann_assign_init", [ann_assign[0], prev[0], annotate_var]
)
sstmt[0][0].transformed_by = "n_stmts"
pass
pass
new_stmts.append(sstmt)
@@ -400,7 +418,7 @@ class TreeTransform(GenericASTTraversal, object):
)
],
)
docstring_ast.transformed_by="transform"
docstring_ast.transformed_by = "transform"
del self.ast[i]
self.ast.insert(0, docstring_ast)
break