Skip to content

Commit

Permalink
withasstmt -> with_as
Browse files Browse the repository at this point in the history
Matches AST a little closer and is not as confusing a name
  • Loading branch information
rocky committed Mar 7, 2024
1 parent 755c16f commit 190c068
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 50 deletions.
14 changes: 7 additions & 7 deletions decompyle3/parsers/p37/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ def customize_grammar_rules37(self, tokens, customize):
rules_str = """
stmt ::= with
stmt ::= with_as_pass
stmt ::= withasstmt
stmt ::= with_as
c_stmt ::= c_with
c_with ::= expr SETUP_WITH POP_TOP
Expand All @@ -1021,14 +1021,14 @@ def customize_grammar_rules37(self, tokens, customize):
COME_FROM_WITH
with_suffix
withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
with_as ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
with_suffix
with ::= expr
SETUP_WITH POP_TOP suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
withasstmt ::= expr
with_as ::= expr
SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
Expand All @@ -1037,7 +1037,7 @@ def customize_grammar_rules37(self, tokens, customize):
SETUP_WITH POP_TOP suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
withasstmt ::= expr
with_as ::= expr
SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
Expand Down Expand Up @@ -1067,16 +1067,16 @@ def customize_grammar_rules37(self, tokens, customize):
with_suffix
withasstmt ::= expr
with_as ::= expr
SETUP_WITH store suite_stmts
POP_BLOCK LOAD_CONST COME_FROM_WITH
withasstmt ::= expr
with_as ::= expr
SETUP_WITH store suite_stmts
POP_BLOCK BEGIN_FINALLY COME_FROM_WITH
with_suffix
# withasstmt ::= expr SETUP_WITH store suite_stmts
# with_as ::= expr SETUP_WITH store suite_stmts
# COME_FROM expr COME_FROM POP_BLOCK ROT_TWO
# BEGIN_FINALLY WITH_CLEANUP_START WITH_CLEANUP_FINISH
# POP_FINALLY RETURN_VALUE COME_FROM_WITH
Expand Down
2 changes: 1 addition & 1 deletion decompyle3/parsers/p37/lambda_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ def customize_grammar_rules_lambda37(self, tokens, customize):
WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
# Removes POP_BLOCK LOAD_CONST from 3.6-
withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
with_as ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
"""
if self.version < (3, 8):
Expand Down
36 changes: 19 additions & 17 deletions decompyle3/parsers/p38/full_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ def customize_grammar_rules_full38(self, tokens, customize):
rules_str = """
stmt ::= with
stmt ::= with_as_pass
stmt ::= withasstmt
stmt ::= with_as
c_stmt ::= c_with
Expand All @@ -679,23 +679,25 @@ def customize_grammar_rules_full38(self, tokens, customize):
COME_FROM_WITH
with_suffix
withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
with ::= expr
SETUP_WITH POP_TOP suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
with ::= expr
SETUP_WITH POP_TOP suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
withasstmt ::= expr
with_as ::= expr
SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
with ::= expr
SETUP_WITH POP_TOP suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_as ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
with_suffix
withasstmt ::= expr
with_as ::= expr
SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
Expand Down Expand Up @@ -729,16 +731,16 @@ def customize_grammar_rules_full38(self, tokens, customize):
with_suffix
withasstmt ::= expr
with_as ::= expr
SETUP_WITH store suite_stmts
POP_BLOCK LOAD_CONST COME_FROM_WITH
withasstmt ::= expr
with_as ::= expr
SETUP_WITH store suite_stmts
POP_BLOCK BEGIN_FINALLY COME_FROM_WITH
with_suffix
# withasstmt ::= expr SETUP_WITH store suite_stmts
# with_as ::= expr SETUP_WITH store suite_stmts
# COME_FROM expr COME_FROM POP_BLOCK ROT_TWO
# BEGIN_FINALLY WITH_CLEANUP_START WITH_CLEANUP_FINISH
# POP_FINALLY RETURN_VALUE COME_FROM_WITH
Expand Down Expand Up @@ -1229,7 +1231,7 @@ def customize_grammar_rules38(self, tokens, customize):
elif opname == "SETUP_WITH":
rules_str = """
stmt ::= with
stmt ::= withasstmt
stmt ::= with_as
c_stmt ::= c_with
c_with ::= expr SETUP_WITH POP_TOP
Expand All @@ -1246,14 +1248,14 @@ def customize_grammar_rules38(self, tokens, customize):
COME_FROM_WITH
with_suffix
withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
with_as ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
with_suffix
with ::= expr
SETUP_WITH POP_TOP suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
withasstmt ::= expr
with_as ::= expr
SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
Expand All @@ -1262,7 +1264,7 @@ def customize_grammar_rules38(self, tokens, customize):
SETUP_WITH POP_TOP suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
withasstmt ::= expr
with_as ::= expr
SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
Expand Down Expand Up @@ -1291,16 +1293,16 @@ def customize_grammar_rules38(self, tokens, customize):
with_suffix
withasstmt ::= expr
with_as ::= expr
SETUP_WITH store suite_stmts
POP_BLOCK LOAD_CONST COME_FROM_WITH
withasstmt ::= expr
with_as ::= expr
SETUP_WITH store suite_stmts
POP_BLOCK BEGIN_FINALLY COME_FROM_WITH
with_suffix
# withasstmt ::= expr SETUP_WITH store suite_stmts
# with_as ::= expr SETUP_WITH store suite_stmts
# COME_FROM expr COME_FROM POP_BLOCK ROT_TWO
# BEGIN_FINALLY WITH_CLEANUP_START WITH_CLEANUP_FINISH
# POP_FINALLY RETURN_VALUE COME_FROM_WITH
Expand Down
4 changes: 2 additions & 2 deletions decompyle3/parsers/p38/lambda_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,8 +612,8 @@ def customize_grammar_rules_lambda38(self, tokens, customize):
WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
# Removes POP_BLOCK LOAD_CONST from 3.6-
withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
with_as ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
a WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
"""
if self.version < (3, 8):
rules_str += """
Expand Down
36 changes: 18 additions & 18 deletions decompyle3/parsers/p38pypy/full_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ def customize_grammar_rules_full38(self, tokens, customize):
elif opname == "SETUP_WITH":
rules_str = """
stmt ::= with
stmt ::= withasstmt
stmt ::= with_as
c_stmt ::= c_with
c_with ::= expr SETUP_WITH POP_TOP
Expand All @@ -677,14 +677,14 @@ def customize_grammar_rules_full38(self, tokens, customize):
COME_FROM_WITH
with_suffix
withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
with_as ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
with_suffix
with ::= expr
SETUP_WITH POP_TOP suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
withasstmt ::= expr
with_as ::= expr
SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
Expand All @@ -693,7 +693,7 @@ def customize_grammar_rules_full38(self, tokens, customize):
SETUP_WITH POP_TOP suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
withasstmt ::= expr
with_as ::= expr
SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
Expand Down Expand Up @@ -722,16 +722,16 @@ def customize_grammar_rules_full38(self, tokens, customize):
with_suffix
withasstmt ::= expr
with_as ::= expr
SETUP_WITH store suite_stmts
POP_BLOCK LOAD_CONST COME_FROM_WITH
withasstmt ::= expr
with_as ::= expr
SETUP_WITH store suite_stmts
POP_BLOCK BEGIN_FINALLY COME_FROM_WITH
with_suffix
# withasstmt ::= expr SETUP_WITH store suite_stmts
# with_as ::= expr SETUP_WITH store suite_stmts
# COME_FROM expr COME_FROM POP_BLOCK ROT_TWO
# BEGIN_FINALLY WITH_CLEANUP_START WITH_CLEANUP_FINISH
# POP_FINALLY RETURN_VALUE COME_FROM_WITH
Expand Down Expand Up @@ -1222,7 +1222,7 @@ def customize_grammar_rules38(self, tokens, customize):
elif opname == "SETUP_WITH":
rules_str = """
stmt ::= with
stmt ::= withasstmt
stmt ::= with_as
c_stmt ::= c_with
c_with ::= expr SETUP_WITH POP_TOP
Expand All @@ -1239,14 +1239,14 @@ def customize_grammar_rules38(self, tokens, customize):
COME_FROM_WITH
with_suffix
withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
with_as ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
with_suffix
with ::= expr
SETUP_WITH POP_TOP suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
withasstmt ::= expr
with_as ::= expr
SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
Expand All @@ -1255,7 +1255,7 @@ def customize_grammar_rules38(self, tokens, customize):
SETUP_WITH POP_TOP suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
withasstmt ::= expr
with_as ::= expr
SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
Expand Down Expand Up @@ -1284,20 +1284,20 @@ def customize_grammar_rules38(self, tokens, customize):
with_suffix
withasstmt ::= expr
with_as ::= expr
SETUP_WITH store suite_stmts
POP_BLOCK LOAD_CONST COME_FROM_WITH
withasstmt ::= expr
with_as ::= expr
SETUP_WITH store suite_stmts
POP_BLOCK BEGIN_FINALLY COME_FROM_WITH
with_suffix
# withasstmt ::= expr SETUP_WITH store suite_stmts
# COME_FROM expr COME_FROM POP_BLOCK ROT_TWO
# BEGIN_FINALLY WITH_CLEANUP_START WITH_CLEANUP_FINISH
# POP_FINALLY RETURN_VALUE COME_FROM_WITH
# WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
# with_as ::= expr SETUP_WITH store suite_stmts
# COME_FROM expr COME_FROM POP_BLOCK ROT_TWO
# BEGIN_FINALLY WITH_CLEANUP_START WITH_CLEANUP_FINISH
# POP_FINALLY RETURN_VALUE COME_FROM_WITH
# WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
with ::= expr SETUP_WITH POP_TOP suite_stmts_opt POP_BLOCK
BEGIN_FINALLY COME_FROM_WITH
Expand Down
2 changes: 1 addition & 1 deletion decompyle3/parsers/p38pypy/lambda_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ def customize_grammar_rules_lambda38(self, tokens, customize):
WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
# Removes POP_BLOCK LOAD_CONST from 3.6-
withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
with_as ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
"""
if self.version < (3, 8):
Expand Down
6 changes: 3 additions & 3 deletions decompyle3/semantics/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,12 +577,12 @@
"import_from_star": ("%|from %[2]{pattr} import *\n",),

# If there are situations where we need "with ... as ()"
# We may need to customize this in n_withasstmt
"withasstmt": (
# We may need to customize this in n_with_as
"with_as": (
"%|with %c as %c:\n%+%c%-",
(0, "expr"),
(2, "store"),
(3, "suite_stmts_opt"),
(3, ("suite_stmts_opt", "_stmts")),
),

}
Expand Down
2 changes: 1 addition & 1 deletion decompyle3/semantics/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
# "return_expr",
# "set_comp_func",
"tryfinallystmt",
"withasstmt",
"with_as",
)


Expand Down
Binary file added test/bytecode_3.7/run/08_test_contextmanager.pyc
Binary file not shown.
Binary file added test/bytecode_3.8/run/08_test_contextmanager.pyc
Binary file not shown.
21 changes: 21 additions & 0 deletions test/simple_source/stmts/08_test_contextmanager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
This program is self checking!
"""


class TestContextManager:
def __enter__(self):
return 1, 2

def __exit__(self, exc_type, exc_value, exc_tb):
return self, exc_type, exc_value, exc_tb


with open(__file__) as a:
assert a

with open(__file__) as a, open(__file__) as b:
assert a.read() == b.read()

with TestContextManager() as a, b:
assert (a, b) == (1, 2)

0 comments on commit 190c068

Please sign in to comment.