Skip to content

Commit

Permalink
Merge pull request #168 from rocky/withasstmt-to-with_as
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky authored Mar 7, 2024
2 parents 59a1be5 + 190c068 commit 16c08fa
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 55 deletions.
16 changes: 9 additions & 7 deletions decompyle3/parsers/p37/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,8 @@ def customize_grammar_rules37(self, tokens, customize):
elif opname == "SETUP_WITH":
rules_str = """
stmt ::= with
stmt ::= withasstmt
stmt ::= with_as_pass
stmt ::= with_as
c_stmt ::= c_with
c_with ::= expr SETUP_WITH POP_TOP
Expand All @@ -1020,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 @@ -1036,10 +1037,11 @@ 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
"""
if self.version < (3, 8):
rules_str += """
Expand All @@ -1065,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
43 changes: 26 additions & 17 deletions decompyle3/parsers/p38/full_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,9 @@ def customize_grammar_rules_full38(self, tokens, customize):
elif opname == "SETUP_WITH":
rules_str = """
stmt ::= with
stmt ::= withasstmt
stmt ::= with_as_pass
stmt ::= with_as
c_stmt ::= c_with
c_with ::= expr SETUP_WITH POP_TOP
Expand All @@ -677,26 +679,33 @@ 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
with_as_pass ::= expr
SETUP_WITH store pass
POP_BLOCK BEGIN_FINALLY COME_FROM_WITH
with_suffix
"""
if self.version < (3, 8):
rules_str += """
Expand All @@ -722,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 @@ -1222,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 @@ -1239,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 @@ -1255,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 @@ -1284,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
11 changes: 7 additions & 4 deletions decompyle3/semantics/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,6 @@
(2, NO_PARENTHESIS_EVER)
),

"IMPORT_FROM": ("%{pattr}",),
"IMPORT_NAME_ATTR": ("%{pattr}",),
"attribute": ("%c.%[1]{pattr}", (0, "expr")),
"delete_subscript": (
"%|del %p[%c]\n",
Expand Down Expand Up @@ -579,8 +577,13 @@
"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": ("%|with %c as %c:\n%+%c%-", 0, 2, 3),
# 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", "_stmts")),
),

}
# fmt: on
Expand Down
14 changes: 10 additions & 4 deletions decompyle3/semantics/customize38.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2023 by Rocky Bernstein
# Copyright (c) 2019-2024 by Rocky Bernstein
#
# 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
Expand Down Expand Up @@ -153,6 +153,11 @@ def customize_for_version38(self, version):
-2,
),
"list_if_not38": (" if not %c", (2, "expr", PRECEDENCE["unary_not"])),
"named_expr": ( # AKA "walrus operator"
"%c := %p",
(2, "store"),
(0, "expr", PRECEDENCE["named_expr"] - 1),
),
"or_in_ifexp": (
"%c or %c",
(0, ("or_in_ifexp", "expr_pjit")),
Expand Down Expand Up @@ -315,10 +320,11 @@ def customize_for_version38(self, version):
(2, "suite_stmts_opt"),
(8, "suite_stmts_opt"),
),
"named_expr": ( # AKA "walrus operator"
"%c := %p",
"with_as_pass": (
"%|with %c as %c:\n%+%c%-",
(0, "expr"),
(2, "store"),
(0, "expr", PRECEDENCE["named_expr"] - 1),
(3, "pass"),
),
}
)
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.
Loading

0 comments on commit 16c08fa

Please sign in to comment.