Skip to content

Commit

Permalink
pythongh-115881: Ensure ast.parse() parses conditional context mana…
Browse files Browse the repository at this point in the history
…gers even with low `feature_version` passed (python#115920)
  • Loading branch information
AlexWaygood committed Feb 26, 2024
1 parent 0f7f5a4 commit 0d93c54
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ for_stmt[stmt_ty]:
with_stmt[stmt_ty]:
| invalid_with_stmt_indent
| 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
CHECK_VERSION(stmt_ty, 9, "Parenthesized context managers are", _PyAST_With(a, b, NULL, EXTRA)) }
_PyAST_With(a, b, NULL, EXTRA) }
| 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
_PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
| ASYNC 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
Expand Down
12 changes: 4 additions & 8 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,14 +772,6 @@ def test_positional_only_feature_version(self):
with self.assertRaises(SyntaxError):
ast.parse('lambda x=1, /: ...', feature_version=(3, 7))

def test_parenthesized_with_feature_version(self):
ast.parse('with (CtxManager() as example): ...', feature_version=(3, 10))
# While advertised as a feature in Python 3.10, this was allowed starting 3.9
ast.parse('with (CtxManager() as example): ...', feature_version=(3, 9))
with self.assertRaises(SyntaxError):
ast.parse('with (CtxManager() as example): ...', feature_version=(3, 8))
ast.parse('with CtxManager() as example: ...', feature_version=(3, 8))

def test_debug_f_string_feature_version(self):
ast.parse('f"{x=}"', feature_version=(3, 8))
with self.assertRaises(SyntaxError):
Expand All @@ -790,6 +782,10 @@ def test_assignment_expression_feature_version(self):
with self.assertRaises(SyntaxError):
ast.parse('(x := 0)', feature_version=(3, 7))

def test_conditional_context_managers_parse_with_low_feature_version(self):
# regression test for gh-115881
ast.parse('with (x() if y else z()): ...', feature_version=(3, 8))

def test_exception_groups_feature_version(self):
code = dedent('''
try: ...
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix issue where :func:`ast.parse` would incorrectly flag conditional context
managers (such as ``with (x() if y else z()): ...``) as invalid syntax if
``feature_version=(3, 8)`` was passed. This reverts changes to the
grammar made as part of gh-94949.
2 changes: 1 addition & 1 deletion Parser/parser.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0d93c54

Please sign in to comment.