From a8b1c82af8da97d857de9d7e3af6e1284366f83f Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sun, 25 Feb 2024 18:59:45 +0000 Subject: [PATCH 1/5] Add a failing test --- Lib/test/test_ast.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index 3789ac22e3899c..de11fa8db3e16d 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -1058,6 +1058,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: ... From c82b85afb708c6a85f0e72377b794d54d46c2016 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sun, 25 Feb 2024 19:10:47 +0000 Subject: [PATCH 2/5] Revert "gh-94949: Disallow parsing parenthesised ctx mgr with old feature_version (#94950)" This reverts commit 0daba822212cd5d6c63384a27f390f0945330c2b. --- Grammar/python.gram | 2 +- Lib/test/test_ast.py | 8 -------- Parser/parser.c | 2 +- 3 files changed, 2 insertions(+), 10 deletions(-) diff --git a/Grammar/python.gram b/Grammar/python.gram index 174b4dbb6f7842..797c195a0a91ba 100644 --- a/Grammar/python.gram +++ b/Grammar/python.gram @@ -393,7 +393,7 @@ for_stmt[stmt_ty]: with_stmt[stmt_ty]: | invalid_with_stmt_indent | 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' tc=[TYPE_COMMENT] b=block { - CHECK_VERSION(stmt_ty, 9, "Parenthesized context managers are", _PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA)) } + _PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), 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 de11fa8db3e16d..d49c149da42592 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -1045,14 +1045,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_assignment_expression_feature_version(self): ast.parse('(x := 0)', feature_version=(3, 8)) with self.assertRaises(SyntaxError): diff --git a/Parser/parser.c b/Parser/parser.c index 779b18e9650e9f..f1170c26197452 100644 --- a/Parser/parser.c +++ b/Parser/parser.c @@ -6603,7 +6603,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 , NEW_TYPE_COMMENT ( p , tc ) , EXTRA ) ); + _res = _PyAST_With ( a , b , NEW_TYPE_COMMENT ( p , tc ) , EXTRA ); if (_res == NULL && PyErr_Occurred()) { p->error_indicator = 1; p->level--; From 09dc88def9c81ad40a54c5c5da3c9c1b4862707e Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 25 Feb 2024 19:20:10 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Library/2024-02-25-19-20-05.gh-issue-115881.ro_Kuw.rst | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-02-25-19-20-05.gh-issue-115881.ro_Kuw.rst 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..d7401ef2c63741 --- /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 `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. From cec56a866f46459b2ce15cb757b6148666b4bdeb Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sun, 25 Feb 2024 19:21:02 +0000 Subject: [PATCH 4/5] ugh not markdown --- .../2024-02-25-19-20-05.gh-issue-115881.ro_Kuw.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 index d7401ef2c63741..99bccb265ff80c 100644 --- 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 @@ -1,4 +1,4 @@ -Fix issue where `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. +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. From cb49140a6faa69681ed4f57393545a58050e75fc Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sun, 25 Feb 2024 19:33:04 +0000 Subject: [PATCH 5/5] fix `test_type_comment` --- Lib/test/test_type_comments.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_type_comments.py b/Lib/test/test_type_comments.py index 5a911da56f8f8a..ee8939f62d082c 100644 --- a/Lib/test/test_type_comments.py +++ b/Lib/test/test_type_comments.py @@ -309,7 +309,7 @@ def test_withstmt(self): self.assertEqual(tree.body[0].type_comment, None) def test_parenthesized_withstmt(self): - for tree in self.parse_all(parenthesized_withstmt, minver=9): + for tree in self.parse_all(parenthesized_withstmt): self.assertEqual(tree.body[0].type_comment, "int") self.assertEqual(tree.body[1].type_comment, "int") tree = self.classic_parse(parenthesized_withstmt)