Skip to content

Commit 3724e02

Browse files
authored
Merge pull request #488 from rocky/with-as-with-pass
Add context manager test...
2 parents b5c4e4b + 8542df4 commit 3724e02

File tree

6 files changed

+64
-14
lines changed

6 files changed

+64
-14
lines changed
722 Bytes
Binary file not shown.
732 Bytes
Binary file not shown.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
This program is self checking!
3+
"""
4+
5+
6+
class TestContextManager:
7+
def __enter__(self):
8+
return 1, 2
9+
10+
def __exit__(self, exc_type, exc_value, exc_tb):
11+
return self, exc_type, exc_value, exc_tb
12+
13+
14+
with open(__file__) as a:
15+
assert a
16+
17+
with open(__file__) as a, open(__file__) as b:
18+
assert a.read() == b.read()
19+
20+
with TestContextManager() as a, b:
21+
assert (a, b) == (1, 2)

uncompyle6/parsers/parse38.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,15 @@ def customize_grammar_rules(self, tokens, customize):
586586
GET_ITER CALL_FUNCTION_1
587587
"""
588588
self.addRule(rule, nop_func)
589+
elif opname == "SETUP_WITH":
590+
rules_str = """
591+
stmt ::= with_as_pass
592+
with_as_pass ::= expr
593+
SETUP_WITH store pass
594+
POP_BLOCK BEGIN_FINALLY COME_FROM_WITH
595+
with_suffix
596+
"""
597+
self.addRule(rules_str, nop_func)
589598

590599
def reduce_is_invalid(self, rule, ast, tokens, first, last):
591600
invalid = super(Python38Parser, self).reduce_is_invalid(

uncompyle6/semantics/consts.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,6 @@
272272
(2, NO_PARENTHESIS_EVER)
273273
),
274274

275-
"IMPORT_FROM": ("%{pattr}",),
276-
"IMPORT_NAME_ATTR": ("%{pattr}",),
277275
"attribute": ("%c.%[1]{pattr}", (0, "expr")),
278276
"delete_subscript": (
279277
"%|del %p[%c]\n",
@@ -431,7 +429,12 @@
431429

432430
# If there are situations where we need "with ... as ()"
433431
# We may need to customize this in n_withasstmt
434-
"withasstmt": ("%|with %c as %c:\n%+%c%-", 0, 2, 3),
432+
"withasstmt": (
433+
"%|with %c as %c:\n%+%c%-",
434+
(0, "expr"),
435+
(2, "store"),
436+
(3, ("suite_stmts_opt", "suite_stmts")),
437+
),
435438

436439
"expr_stmt": (
437440
"%|%p\n",

uncompyle6/semantics/customize38.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
from uncompyle6.semantics.customize37 import FSTRING_CONVERSION_MAP
2424
from uncompyle6.semantics.helper import escape_string, strip_quotes
2525

26-
def customize_for_version38(self, version):
2726

27+
def customize_for_version38(self, version):
2828
# FIXME: pytest doesn't add proper keys in testing. Reinstate after we have fixed pytest.
2929
# for lhs in 'for forelsestmt forelselaststmt '
3030
# 'forelselaststmtc tryfinally38'.split():
@@ -40,10 +40,10 @@ def customize_for_version38(self, version):
4040
),
4141
"async_forelse_stmt38": (
4242
"%|async for %c in %c:\n%+%c%-%|else:\n%+%c%-\n\n",
43-
(7, 'store'),
44-
(0, 'expr'),
45-
(8, 'for_block'),
46-
(-1, 'else_suite')
43+
(7, "store"),
44+
(0, "expr"),
45+
(8, "for_block"),
46+
(-1, "else_suite"),
4747
),
4848
"async_with_stmt38": (
4949
"%|async with %c:\n%+%c%-\n",
@@ -70,8 +70,15 @@ def customize_for_version38(self, version):
7070
),
7171
# Python 3.8 reverses the order of keys and items
7272
# from all prior versions of Python.
73-
"dict_comp_body": ("%c: %c", (0, "expr"), (1, "expr"),),
74-
"except_cond1a": ("%|except %c:\n", (1, "expr"),),
73+
"dict_comp_body": (
74+
"%c: %c",
75+
(0, "expr"),
76+
(1, "expr"),
77+
),
78+
"except_cond1a": (
79+
"%|except %c:\n",
80+
(1, "expr"),
81+
),
7582
"except_cond_as": (
7683
"%|except %c as %c:\n",
7784
(1, "expr"),
@@ -121,10 +128,19 @@ def customize_for_version38(self, version):
121128
-2,
122129
),
123130
"ifpoplaststmtc": ("%|if %c:\n%+%c%-", (0, "testexpr"), (2, "l_stmts")),
131+
"named_expr": ( # AKA "walrus operator"
132+
"%c := %p",
133+
(2, "store"),
134+
(0, "expr", PRECEDENCE["named_expr"] - 1),
135+
),
124136
"pop_return": ("%|return %c\n", (1, "return_expr")),
125137
"popb_return": ("%|return %c\n", (0, "return_expr")),
126138
"pop_ex_return": ("%|return %c\n", (0, "return_expr")),
127-
"set_for": (" for %c in %c", (2, "store"), (0, "expr_or_arg"),),
139+
"set_for": (
140+
" for %c in %c",
141+
(2, "store"),
142+
(0, "expr_or_arg"),
143+
),
128144
"whilestmt38": (
129145
"%|while %c:\n%+%c%-\n\n",
130146
(1, ("bool_op", "testexpr", "testexprc")),
@@ -211,10 +227,11 @@ def customize_for_version38(self, version):
211227
(2, "suite_stmts_opt"),
212228
(8, "suite_stmts_opt"),
213229
),
214-
"named_expr": ( # AKA "walrus operator"
215-
"%c := %p",
230+
"with_as_pass": (
231+
"%|with %c as %c:\n%+%c%-",
232+
(0, "expr"),
216233
(2, "store"),
217-
(0, "expr", PRECEDENCE["named_expr"] - 1),
234+
(3, "pass"),
218235
),
219236
}
220237
)

0 commit comments

Comments
 (0)