Skip to content

Commit f1bf860

Browse files
committed
Sync with decompyle3
1 parent c8b92e2 commit f1bf860

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

uncompyle6/semantics/pysource.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ def __str__(self):
210210

211211
class SourceWalker(GenericASTTraversal, NonterminalActions, ComprehensionMixin):
212212
"""
213-
Class to traverses a Parse Tree of the bytecode instruction built from parsing to produce some sort of source text.
213+
Class to traverses a Parse Tree of the bytecode instruction built from parsing to
214+
produce some sort of source text.
214215
The Parse tree may be turned an Abstract Syntax tree as an intermediate step.
215216
"""
216217

@@ -318,7 +319,7 @@ def __init__(
318319
customize_for_version(self, is_pypy, version)
319320
return
320321

321-
def maybe_show_tree(self, ast, phase):
322+
def maybe_show_tree(self, tree, phase):
322323
if self.showast.get("before", False):
323324
self.println(
324325
"""
@@ -334,7 +335,7 @@ def maybe_show_tree(self, ast, phase):
334335
+ " "
335336
)
336337
if self.showast.get(phase, False):
337-
maybe_show_tree(self, ast)
338+
maybe_show_tree(self, tree)
338339

339340
def str_with_template(self, ast):
340341
stream = sys.stdout

uncompyle6/semantics/transform.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2019-2023 by Rocky Bernstein
1+
# Copyright (c) 2019-2024 by Rocky Bernstein
22

33
# This program is free software: you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License as published by
@@ -14,6 +14,7 @@
1414
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1515

1616
from copy import copy
17+
from typing import Optional
1718

1819
from spark_parser import GenericASTTraversal, GenericASTTraversalPruningException
1920

@@ -56,29 +57,34 @@ def is_docstring(node, version, co_consts):
5657
return node == ASSIGN_DOC_STRING(co_consts[0], doc_load)
5758

5859

59-
def is_not_docstring(call_stmt_node):
60+
def is_not_docstring(call_stmt_node) -> bool:
6061
try:
6162
return (
6263
call_stmt_node == "call_stmt"
6364
and call_stmt_node[0][0] == "LOAD_STR"
6465
and call_stmt_node[1] == "POP_TOP"
6566
)
66-
except:
67+
except Exception:
6768
return False
6869

6970

7071
class TreeTransform(GenericASTTraversal, object):
71-
def __init__(self, version, show_ast=None, is_pypy=False):
72+
def __init__(
73+
self,
74+
version: tuple,
75+
is_pypy=False,
76+
show_ast: Optional[dict] = None,
77+
):
7278
self.version = version
7379
self.showast = show_ast
7480
self.is_pypy = is_pypy
7581
return
7682

77-
def maybe_show_tree(self, ast):
83+
def maybe_show_tree(self, tree):
7884
if isinstance(self.showast, dict) and (
7985
self.showast.get("before") or self.showast.get("after")
8086
):
81-
maybe_show_tree(self, ast)
87+
maybe_show_tree(self, tree)
8288

8389
def preorder(self, node=None):
8490
"""Walk the tree in roughly 'preorder' (a bit of a lie explained below).
@@ -122,6 +128,7 @@ def n_mkfunc(self, node):
122128

123129
mkfunc_pattr = node[-1].pattr
124130
if isinstance(mkfunc_pattr, tuple):
131+
assert isinstance(mkfunc_pattr, tuple)
125132
assert len(mkfunc_pattr) == 4 and isinstance(mkfunc_pattr, int)
126133

127134
if len(code.co_consts) > 0 and isinstance(code.co_consts[0], str):
@@ -216,6 +223,7 @@ def n_ifstmt(self, node):
216223
return node
217224
if isinstance(call[1], SyntaxTree):
218225
expr = call[1][0]
226+
assert_expr.transformed_by = "n_ifstmt"
219227
node = SyntaxTree(
220228
kind,
221229
[
@@ -225,8 +233,8 @@ def n_ifstmt(self, node):
225233
expr,
226234
RAISE_VARARGS_1,
227235
],
236+
transformed_by="n_ifstmt",
228237
)
229-
node.transformed_by = "n_ifstmt"
230238
pass
231239
pass
232240
else:
@@ -254,9 +262,10 @@ def n_ifstmt(self, node):
254262

255263
LOAD_ASSERT = expr[0]
256264
node = SyntaxTree(
257-
kind, [assert_expr, jump_cond, LOAD_ASSERT, RAISE_VARARGS_1]
265+
kind,
266+
[assert_expr, jump_cond, LOAD_ASSERT, RAISE_VARARGS_1],
267+
transformed_by="n_ifstmt",
258268
)
259-
node.transformed_by = ("n_ifstmt",)
260269
pass
261270
pass
262271
return node
@@ -416,6 +425,12 @@ def n_list_for(self, list_for_node):
416425
list_for_node.transformed_by = ("n_list_for",)
417426
return list_for_node
418427

428+
def n_negated_testtrue(self, node):
429+
assert node[0] == "testtrue"
430+
test_node = node[0][0]
431+
test_node.transformed_by = "n_negated_testtrue"
432+
return test_node
433+
419434
def n_stmts(self, node):
420435
if node.first_child() == "SETUP_ANNOTATIONS":
421436
prev = node[0][0]
@@ -448,26 +463,28 @@ def traverse(self, node, is_lambda=False):
448463
node = self.preorder(node)
449464
return node
450465

451-
def transform(self, ast, code):
452-
self.maybe_show_tree(ast)
453-
self.ast = copy(ast)
466+
def transform(self, parse_tree: GenericASTTraversal, code) -> GenericASTTraversal:
467+
self.maybe_show_tree(parse_tree)
468+
self.ast = copy(parse_tree)
469+
del parse_tree
454470
self.ast = self.traverse(self.ast, is_lambda=False)
471+
n = len(self.ast)
455472

456473
try:
457474
# Disambiguate a string (expression) which appears as a "call_stmt" at
458475
# the beginning of a function versus a docstring. Seems pretty academic,
459476
# but this is Python.
460-
call_stmt = ast[0][0]
477+
call_stmt = self.ast[0][0]
461478
if is_not_docstring(call_stmt):
462479
call_stmt.kind = "string_at_beginning"
463480
call_stmt.transformed_by = "transform"
464481
pass
465-
except:
482+
except Exception:
466483
pass
467484

468485
try:
469-
for i in range(len(self.ast)):
470-
sstmt = ast[i]
486+
for i in range(n):
487+
sstmt = self.ast[i]
471488
if len(sstmt) == 1 and sstmt == "sstmt":
472489
self.ast[i] = self.ast[i][0]
473490

@@ -493,7 +510,7 @@ def transform(self, ast, code):
493510
if self.ast[-1] == RETURN_NONE:
494511
self.ast.pop() # remove last node
495512
# todo: if empty, add 'pass'
496-
except:
513+
except Exception:
497514
pass
498515

499516
return self.ast

0 commit comments

Comments
 (0)