Merge pull request #488 from rocky/with-as-with-pass

Add context manager test...
This commit is contained in:
R. Bernstein
2024-03-07 18:53:14 -05:00
committed by GitHub
6 changed files with 64 additions and 14 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -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)

View File

@@ -586,6 +586,15 @@ class Python38Parser(Python37Parser):
GET_ITER CALL_FUNCTION_1 GET_ITER CALL_FUNCTION_1
""" """
self.addRule(rule, nop_func) self.addRule(rule, nop_func)
elif opname == "SETUP_WITH":
rules_str = """
stmt ::= with_as_pass
with_as_pass ::= expr
SETUP_WITH store pass
POP_BLOCK BEGIN_FINALLY COME_FROM_WITH
with_suffix
"""
self.addRule(rules_str, nop_func)
def reduce_is_invalid(self, rule, ast, tokens, first, last): def reduce_is_invalid(self, rule, ast, tokens, first, last):
invalid = super(Python38Parser, self).reduce_is_invalid( invalid = super(Python38Parser, self).reduce_is_invalid(

View File

@@ -272,8 +272,6 @@ TABLE_DIRECT = {
(2, NO_PARENTHESIS_EVER) (2, NO_PARENTHESIS_EVER)
), ),
"IMPORT_FROM": ("%{pattr}",),
"IMPORT_NAME_ATTR": ("%{pattr}",),
"attribute": ("%c.%[1]{pattr}", (0, "expr")), "attribute": ("%c.%[1]{pattr}", (0, "expr")),
"delete_subscript": ( "delete_subscript": (
"%|del %p[%c]\n", "%|del %p[%c]\n",
@@ -431,7 +429,12 @@ TABLE_DIRECT = {
# If there are situations where we need "with ... as ()" # If there are situations where we need "with ... as ()"
# We may need to customize this in n_withasstmt # We may need to customize this in n_withasstmt
"withasstmt": ("%|with %c as %c:\n%+%c%-", 0, 2, 3), "withasstmt": (
"%|with %c as %c:\n%+%c%-",
(0, "expr"),
(2, "store"),
(3, ("suite_stmts_opt", "suite_stmts")),
),
"expr_stmt": ( "expr_stmt": (
"%|%p\n", "%|%p\n",

View File

@@ -23,8 +23,8 @@ from uncompyle6.semantics.consts import PRECEDENCE, TABLE_DIRECT
from uncompyle6.semantics.customize37 import FSTRING_CONVERSION_MAP from uncompyle6.semantics.customize37 import FSTRING_CONVERSION_MAP
from uncompyle6.semantics.helper import escape_string, strip_quotes from uncompyle6.semantics.helper import escape_string, strip_quotes
def customize_for_version38(self, version):
def customize_for_version38(self, version):
# FIXME: pytest doesn't add proper keys in testing. Reinstate after we have fixed pytest. # FIXME: pytest doesn't add proper keys in testing. Reinstate after we have fixed pytest.
# for lhs in 'for forelsestmt forelselaststmt ' # for lhs in 'for forelsestmt forelselaststmt '
# 'forelselaststmtc tryfinally38'.split(): # 'forelselaststmtc tryfinally38'.split():
@@ -40,10 +40,10 @@ def customize_for_version38(self, version):
), ),
"async_forelse_stmt38": ( "async_forelse_stmt38": (
"%|async for %c in %c:\n%+%c%-%|else:\n%+%c%-\n\n", "%|async for %c in %c:\n%+%c%-%|else:\n%+%c%-\n\n",
(7, 'store'), (7, "store"),
(0, 'expr'), (0, "expr"),
(8, 'for_block'), (8, "for_block"),
(-1, 'else_suite') (-1, "else_suite"),
), ),
"async_with_stmt38": ( "async_with_stmt38": (
"%|async with %c:\n%+%c%-\n", "%|async with %c:\n%+%c%-\n",
@@ -70,8 +70,15 @@ def customize_for_version38(self, version):
), ),
# Python 3.8 reverses the order of keys and items # Python 3.8 reverses the order of keys and items
# from all prior versions of Python. # from all prior versions of Python.
"dict_comp_body": ("%c: %c", (0, "expr"), (1, "expr"),), "dict_comp_body": (
"except_cond1a": ("%|except %c:\n", (1, "expr"),), "%c: %c",
(0, "expr"),
(1, "expr"),
),
"except_cond1a": (
"%|except %c:\n",
(1, "expr"),
),
"except_cond_as": ( "except_cond_as": (
"%|except %c as %c:\n", "%|except %c as %c:\n",
(1, "expr"), (1, "expr"),
@@ -121,10 +128,19 @@ def customize_for_version38(self, version):
-2, -2,
), ),
"ifpoplaststmtc": ("%|if %c:\n%+%c%-", (0, "testexpr"), (2, "l_stmts")), "ifpoplaststmtc": ("%|if %c:\n%+%c%-", (0, "testexpr"), (2, "l_stmts")),
"named_expr": ( # AKA "walrus operator"
"%c := %p",
(2, "store"),
(0, "expr", PRECEDENCE["named_expr"] - 1),
),
"pop_return": ("%|return %c\n", (1, "return_expr")), "pop_return": ("%|return %c\n", (1, "return_expr")),
"popb_return": ("%|return %c\n", (0, "return_expr")), "popb_return": ("%|return %c\n", (0, "return_expr")),
"pop_ex_return": ("%|return %c\n", (0, "return_expr")), "pop_ex_return": ("%|return %c\n", (0, "return_expr")),
"set_for": (" for %c in %c", (2, "store"), (0, "expr_or_arg"),), "set_for": (
" for %c in %c",
(2, "store"),
(0, "expr_or_arg"),
),
"whilestmt38": ( "whilestmt38": (
"%|while %c:\n%+%c%-\n\n", "%|while %c:\n%+%c%-\n\n",
(1, ("bool_op", "testexpr", "testexprc")), (1, ("bool_op", "testexpr", "testexprc")),
@@ -211,10 +227,11 @@ def customize_for_version38(self, version):
(2, "suite_stmts_opt"), (2, "suite_stmts_opt"),
(8, "suite_stmts_opt"), (8, "suite_stmts_opt"),
), ),
"named_expr": ( # AKA "walrus operator" "with_as_pass": (
"%c := %p", "%|with %c as %c:\n%+%c%-",
(0, "expr"),
(2, "store"), (2, "store"),
(0, "expr", PRECEDENCE["named_expr"] - 1), (3, "pass"),
), ),
} }
) )