Skip to content

Commit 16c08fa

Browse files
authored
Merge pull request #168 from rocky/withasstmt-to-with_as
https://github.com/rocky/python-decompile3/pull/new/withasstmt-to-with_as
2 parents 59a1be5 + 190c068 commit 16c08fa

File tree

12 files changed

+96
-55
lines changed

12 files changed

+96
-55
lines changed

decompyle3/parsers/p37/base.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,8 @@ def customize_grammar_rules37(self, tokens, customize):
10031003
elif opname == "SETUP_WITH":
10041004
rules_str = """
10051005
stmt ::= with
1006-
stmt ::= withasstmt
1006+
stmt ::= with_as_pass
1007+
stmt ::= with_as
10071008
c_stmt ::= c_with
10081009
10091010
c_with ::= expr SETUP_WITH POP_TOP
@@ -1020,14 +1021,14 @@ def customize_grammar_rules37(self, tokens, customize):
10201021
COME_FROM_WITH
10211022
with_suffix
10221023
1023-
withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
1024+
with_as ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
10241025
with_suffix
10251026
10261027
with ::= expr
10271028
SETUP_WITH POP_TOP suite_stmts_opt
10281029
POP_BLOCK LOAD_CONST COME_FROM_WITH
10291030
with_suffix
1030-
withasstmt ::= expr
1031+
with_as ::= expr
10311032
SETUP_WITH store suite_stmts_opt
10321033
POP_BLOCK LOAD_CONST COME_FROM_WITH
10331034
with_suffix
@@ -1036,10 +1037,11 @@ def customize_grammar_rules37(self, tokens, customize):
10361037
SETUP_WITH POP_TOP suite_stmts_opt
10371038
POP_BLOCK LOAD_CONST COME_FROM_WITH
10381039
with_suffix
1039-
withasstmt ::= expr
1040+
with_as ::= expr
10401041
SETUP_WITH store suite_stmts_opt
10411042
POP_BLOCK LOAD_CONST COME_FROM_WITH
10421043
with_suffix
1044+
10431045
"""
10441046
if self.version < (3, 8):
10451047
rules_str += """
@@ -1065,16 +1067,16 @@ def customize_grammar_rules37(self, tokens, customize):
10651067
with_suffix
10661068
10671069
1068-
withasstmt ::= expr
1070+
with_as ::= expr
10691071
SETUP_WITH store suite_stmts
10701072
POP_BLOCK LOAD_CONST COME_FROM_WITH
10711073
1072-
withasstmt ::= expr
1074+
with_as ::= expr
10731075
SETUP_WITH store suite_stmts
10741076
POP_BLOCK BEGIN_FINALLY COME_FROM_WITH
10751077
with_suffix
10761078
1077-
# withasstmt ::= expr SETUP_WITH store suite_stmts
1079+
# with_as ::= expr SETUP_WITH store suite_stmts
10781080
# COME_FROM expr COME_FROM POP_BLOCK ROT_TWO
10791081
# BEGIN_FINALLY WITH_CLEANUP_START WITH_CLEANUP_FINISH
10801082
# POP_FINALLY RETURN_VALUE COME_FROM_WITH

decompyle3/parsers/p37/lambda_custom.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ def customize_grammar_rules_lambda37(self, tokens, customize):
542542
WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
543543
544544
# Removes POP_BLOCK LOAD_CONST from 3.6-
545-
withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
545+
with_as ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
546546
WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
547547
"""
548548
if self.version < (3, 8):

decompyle3/parsers/p38/full_custom.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,9 @@ def customize_grammar_rules_full38(self, tokens, customize):
660660
elif opname == "SETUP_WITH":
661661
rules_str = """
662662
stmt ::= with
663-
stmt ::= withasstmt
663+
stmt ::= with_as_pass
664+
stmt ::= with_as
665+
664666
c_stmt ::= c_with
665667
666668
c_with ::= expr SETUP_WITH POP_TOP
@@ -677,26 +679,33 @@ def customize_grammar_rules_full38(self, tokens, customize):
677679
COME_FROM_WITH
678680
with_suffix
679681
680-
withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
682+
with ::= expr
683+
SETUP_WITH POP_TOP suite_stmts_opt
684+
POP_BLOCK LOAD_CONST COME_FROM_WITH
681685
with_suffix
682686
683687
with ::= expr
684688
SETUP_WITH POP_TOP suite_stmts_opt
685689
POP_BLOCK LOAD_CONST COME_FROM_WITH
686690
with_suffix
687-
withasstmt ::= expr
691+
692+
with_as ::= expr
688693
SETUP_WITH store suite_stmts_opt
689694
POP_BLOCK LOAD_CONST COME_FROM_WITH
690695
with_suffix
691696
692-
with ::= expr
693-
SETUP_WITH POP_TOP suite_stmts_opt
694-
POP_BLOCK LOAD_CONST COME_FROM_WITH
697+
with_as ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
695698
with_suffix
696-
withasstmt ::= expr
699+
700+
with_as ::= expr
697701
SETUP_WITH store suite_stmts_opt
698702
POP_BLOCK LOAD_CONST COME_FROM_WITH
699703
with_suffix
704+
705+
with_as_pass ::= expr
706+
SETUP_WITH store pass
707+
POP_BLOCK BEGIN_FINALLY COME_FROM_WITH
708+
with_suffix
700709
"""
701710
if self.version < (3, 8):
702711
rules_str += """
@@ -722,16 +731,16 @@ def customize_grammar_rules_full38(self, tokens, customize):
722731
with_suffix
723732
724733
725-
withasstmt ::= expr
734+
with_as ::= expr
726735
SETUP_WITH store suite_stmts
727736
POP_BLOCK LOAD_CONST COME_FROM_WITH
728737
729-
withasstmt ::= expr
738+
with_as ::= expr
730739
SETUP_WITH store suite_stmts
731740
POP_BLOCK BEGIN_FINALLY COME_FROM_WITH
732741
with_suffix
733742
734-
# withasstmt ::= expr SETUP_WITH store suite_stmts
743+
# with_as ::= expr SETUP_WITH store suite_stmts
735744
# COME_FROM expr COME_FROM POP_BLOCK ROT_TWO
736745
# BEGIN_FINALLY WITH_CLEANUP_START WITH_CLEANUP_FINISH
737746
# POP_FINALLY RETURN_VALUE COME_FROM_WITH
@@ -1222,7 +1231,7 @@ def customize_grammar_rules38(self, tokens, customize):
12221231
elif opname == "SETUP_WITH":
12231232
rules_str = """
12241233
stmt ::= with
1225-
stmt ::= withasstmt
1234+
stmt ::= with_as
12261235
c_stmt ::= c_with
12271236
12281237
c_with ::= expr SETUP_WITH POP_TOP
@@ -1239,14 +1248,14 @@ def customize_grammar_rules38(self, tokens, customize):
12391248
COME_FROM_WITH
12401249
with_suffix
12411250
1242-
withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
1251+
with_as ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
12431252
with_suffix
12441253
12451254
with ::= expr
12461255
SETUP_WITH POP_TOP suite_stmts_opt
12471256
POP_BLOCK LOAD_CONST COME_FROM_WITH
12481257
with_suffix
1249-
withasstmt ::= expr
1258+
with_as ::= expr
12501259
SETUP_WITH store suite_stmts_opt
12511260
POP_BLOCK LOAD_CONST COME_FROM_WITH
12521261
with_suffix
@@ -1255,7 +1264,7 @@ def customize_grammar_rules38(self, tokens, customize):
12551264
SETUP_WITH POP_TOP suite_stmts_opt
12561265
POP_BLOCK LOAD_CONST COME_FROM_WITH
12571266
with_suffix
1258-
withasstmt ::= expr
1267+
with_as ::= expr
12591268
SETUP_WITH store suite_stmts_opt
12601269
POP_BLOCK LOAD_CONST COME_FROM_WITH
12611270
with_suffix
@@ -1284,16 +1293,16 @@ def customize_grammar_rules38(self, tokens, customize):
12841293
with_suffix
12851294
12861295
1287-
withasstmt ::= expr
1296+
with_as ::= expr
12881297
SETUP_WITH store suite_stmts
12891298
POP_BLOCK LOAD_CONST COME_FROM_WITH
12901299
1291-
withasstmt ::= expr
1300+
with_as ::= expr
12921301
SETUP_WITH store suite_stmts
12931302
POP_BLOCK BEGIN_FINALLY COME_FROM_WITH
12941303
with_suffix
12951304
1296-
# withasstmt ::= expr SETUP_WITH store suite_stmts
1305+
# with_as ::= expr SETUP_WITH store suite_stmts
12971306
# COME_FROM expr COME_FROM POP_BLOCK ROT_TWO
12981307
# BEGIN_FINALLY WITH_CLEANUP_START WITH_CLEANUP_FINISH
12991308
# POP_FINALLY RETURN_VALUE COME_FROM_WITH

decompyle3/parsers/p38/lambda_custom.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,8 @@ def customize_grammar_rules_lambda38(self, tokens, customize):
612612
WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
613613
614614
# Removes POP_BLOCK LOAD_CONST from 3.6-
615-
withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
616-
WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
615+
with_as ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
616+
a WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
617617
"""
618618
if self.version < (3, 8):
619619
rules_str += """

decompyle3/parsers/p38pypy/full_custom.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ def customize_grammar_rules_full38(self, tokens, customize):
660660
elif opname == "SETUP_WITH":
661661
rules_str = """
662662
stmt ::= with
663-
stmt ::= withasstmt
663+
stmt ::= with_as
664664
c_stmt ::= c_with
665665
666666
c_with ::= expr SETUP_WITH POP_TOP
@@ -677,14 +677,14 @@ def customize_grammar_rules_full38(self, tokens, customize):
677677
COME_FROM_WITH
678678
with_suffix
679679
680-
withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
680+
with_as ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
681681
with_suffix
682682
683683
with ::= expr
684684
SETUP_WITH POP_TOP suite_stmts_opt
685685
POP_BLOCK LOAD_CONST COME_FROM_WITH
686686
with_suffix
687-
withasstmt ::= expr
687+
with_as ::= expr
688688
SETUP_WITH store suite_stmts_opt
689689
POP_BLOCK LOAD_CONST COME_FROM_WITH
690690
with_suffix
@@ -693,7 +693,7 @@ def customize_grammar_rules_full38(self, tokens, customize):
693693
SETUP_WITH POP_TOP suite_stmts_opt
694694
POP_BLOCK LOAD_CONST COME_FROM_WITH
695695
with_suffix
696-
withasstmt ::= expr
696+
with_as ::= expr
697697
SETUP_WITH store suite_stmts_opt
698698
POP_BLOCK LOAD_CONST COME_FROM_WITH
699699
with_suffix
@@ -722,16 +722,16 @@ def customize_grammar_rules_full38(self, tokens, customize):
722722
with_suffix
723723
724724
725-
withasstmt ::= expr
725+
with_as ::= expr
726726
SETUP_WITH store suite_stmts
727727
POP_BLOCK LOAD_CONST COME_FROM_WITH
728728
729-
withasstmt ::= expr
729+
with_as ::= expr
730730
SETUP_WITH store suite_stmts
731731
POP_BLOCK BEGIN_FINALLY COME_FROM_WITH
732732
with_suffix
733733
734-
# withasstmt ::= expr SETUP_WITH store suite_stmts
734+
# with_as ::= expr SETUP_WITH store suite_stmts
735735
# COME_FROM expr COME_FROM POP_BLOCK ROT_TWO
736736
# BEGIN_FINALLY WITH_CLEANUP_START WITH_CLEANUP_FINISH
737737
# POP_FINALLY RETURN_VALUE COME_FROM_WITH
@@ -1222,7 +1222,7 @@ def customize_grammar_rules38(self, tokens, customize):
12221222
elif opname == "SETUP_WITH":
12231223
rules_str = """
12241224
stmt ::= with
1225-
stmt ::= withasstmt
1225+
stmt ::= with_as
12261226
c_stmt ::= c_with
12271227
12281228
c_with ::= expr SETUP_WITH POP_TOP
@@ -1239,14 +1239,14 @@ def customize_grammar_rules38(self, tokens, customize):
12391239
COME_FROM_WITH
12401240
with_suffix
12411241
1242-
withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
1242+
with_as ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
12431243
with_suffix
12441244
12451245
with ::= expr
12461246
SETUP_WITH POP_TOP suite_stmts_opt
12471247
POP_BLOCK LOAD_CONST COME_FROM_WITH
12481248
with_suffix
1249-
withasstmt ::= expr
1249+
with_as ::= expr
12501250
SETUP_WITH store suite_stmts_opt
12511251
POP_BLOCK LOAD_CONST COME_FROM_WITH
12521252
with_suffix
@@ -1255,7 +1255,7 @@ def customize_grammar_rules38(self, tokens, customize):
12551255
SETUP_WITH POP_TOP suite_stmts_opt
12561256
POP_BLOCK LOAD_CONST COME_FROM_WITH
12571257
with_suffix
1258-
withasstmt ::= expr
1258+
with_as ::= expr
12591259
SETUP_WITH store suite_stmts_opt
12601260
POP_BLOCK LOAD_CONST COME_FROM_WITH
12611261
with_suffix
@@ -1284,20 +1284,20 @@ def customize_grammar_rules38(self, tokens, customize):
12841284
with_suffix
12851285
12861286
1287-
withasstmt ::= expr
1287+
with_as ::= expr
12881288
SETUP_WITH store suite_stmts
12891289
POP_BLOCK LOAD_CONST COME_FROM_WITH
12901290
1291-
withasstmt ::= expr
1291+
with_as ::= expr
12921292
SETUP_WITH store suite_stmts
12931293
POP_BLOCK BEGIN_FINALLY COME_FROM_WITH
12941294
with_suffix
12951295
1296-
# withasstmt ::= expr SETUP_WITH store suite_stmts
1297-
# COME_FROM expr COME_FROM POP_BLOCK ROT_TWO
1298-
# BEGIN_FINALLY WITH_CLEANUP_START WITH_CLEANUP_FINISH
1299-
# POP_FINALLY RETURN_VALUE COME_FROM_WITH
1300-
# WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
1296+
# with_as ::= expr SETUP_WITH store suite_stmts
1297+
# COME_FROM expr COME_FROM POP_BLOCK ROT_TWO
1298+
# BEGIN_FINALLY WITH_CLEANUP_START WITH_CLEANUP_FINISH
1299+
# POP_FINALLY RETURN_VALUE COME_FROM_WITH
1300+
# WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
13011301
13021302
with ::= expr SETUP_WITH POP_TOP suite_stmts_opt POP_BLOCK
13031303
BEGIN_FINALLY COME_FROM_WITH

decompyle3/parsers/p38pypy/lambda_custom.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ def customize_grammar_rules_lambda38(self, tokens, customize):
623623
WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
624624
625625
# Removes POP_BLOCK LOAD_CONST from 3.6-
626-
withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
626+
with_as ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
627627
WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
628628
"""
629629
if self.version < (3, 8):

decompyle3/semantics/consts.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,6 @@
271271
(2, NO_PARENTHESIS_EVER)
272272
),
273273

274-
"IMPORT_FROM": ("%{pattr}",),
275-
"IMPORT_NAME_ATTR": ("%{pattr}",),
276274
"attribute": ("%c.%[1]{pattr}", (0, "expr")),
277275
"delete_subscript": (
278276
"%|del %p[%c]\n",
@@ -579,8 +577,13 @@
579577
"import_from_star": ("%|from %[2]{pattr} import *\n",),
580578

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

585588
}
586589
# fmt: on

decompyle3/semantics/customize38.py

Lines changed: 10 additions & 4 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
@@ -153,6 +153,11 @@ def customize_for_version38(self, version):
153153
-2,
154154
),
155155
"list_if_not38": (" if not %c", (2, "expr", PRECEDENCE["unary_not"])),
156+
"named_expr": ( # AKA "walrus operator"
157+
"%c := %p",
158+
(2, "store"),
159+
(0, "expr", PRECEDENCE["named_expr"] - 1),
160+
),
156161
"or_in_ifexp": (
157162
"%c or %c",
158163
(0, ("or_in_ifexp", "expr_pjit")),
@@ -315,10 +320,11 @@ def customize_for_version38(self, version):
315320
(2, "suite_stmts_opt"),
316321
(8, "suite_stmts_opt"),
317322
),
318-
"named_expr": ( # AKA "walrus operator"
319-
"%c := %p",
323+
"with_as_pass": (
324+
"%|with %c as %c:\n%+%c%-",
325+
(0, "expr"),
320326
(2, "store"),
321-
(0, "expr", PRECEDENCE["named_expr"] - 1),
327+
(3, "pass"),
322328
),
323329
}
324330
)

decompyle3/semantics/transform.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
# "return_expr",
6868
# "set_comp_func",
6969
"tryfinallystmt",
70-
"withasstmt",
70+
"with_as",
7171
)
7272

7373

843 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)