https://github.com/python/cpython/commit/a30a1e7a4968f393d598ec5b2b96fa9ef42438be
commit: a30a1e7a4968f393d598ec5b2b96fa9ef42438be
branch: 3.11
author: Alex Waygood <[email protected]>
committer: AlexWaygood <[email protected]>
date: 2024-02-26T16:27:51Z
summary:
[3.11] gh-115881: Ensure `ast.parse()` parses conditional context managers even
with low `feature_version` passed (#115920) (#115960)
files:
A Misc/NEWS.d/next/Library/2024-02-25-19-20-05.gh-issue-115881.ro_Kuw.rst
M Grammar/python.gram
M Lib/test/test_ast.py
M Parser/parser.c
diff --git a/Grammar/python.gram b/Grammar/python.gram
index 7b462fd0cd4157..c7e7233c13261e 100644
--- a/Grammar/python.gram
+++ b/Grammar/python.gram
@@ -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 {
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index c8d2f916dbd5f0..6acde72aa9b302 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -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):
@@ -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: ...
diff --git
a/Misc/NEWS.d/next/Library/2024-02-25-19-20-05.gh-issue-115881.ro_Kuw.rst
b/Misc/NEWS.d/next/Library/2024-02-25-19-20-05.gh-issue-115881.ro_Kuw.rst
new file mode 100644
index 00000000000000..99bccb265ff80c
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-02-25-19-20-05.gh-issue-115881.ro_Kuw.rst
@@ -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.
diff --git a/Parser/parser.c b/Parser/parser.c
index af4c8933b181be..2f1427af501dd2 100644
--- a/Parser/parser.c
+++ b/Parser/parser.c
@@ -6527,7 +6527,7 @@ with_stmt_rule(Parser *p)
UNUSED(_end_lineno); // Only used by EXTRA macro
int _end_col_offset = _token->end_col_offset;
UNUSED(_end_col_offset); // Only used by EXTRA macro
- _res = CHECK_VERSION ( stmt_ty , 9 , "Parenthesized context
managers are" , _PyAST_With ( a , b , NULL , EXTRA ) );
+ _res = _PyAST_With ( a , b , NULL , EXTRA );
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
p->level--;
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]