From 675a737b9d9be80639c6907e13089937cca3bd9c Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sun, 17 Jul 2022 21:57:16 -0700 Subject: [PATCH 1/3] gh-94949: Disallow parsing parenthesised ctx manager with old feature_version --- Grammar/python.gram | 2 +- Lib/test/test_ast.py | 6 ++++++ Parser/parser.c | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Grammar/python.gram b/Grammar/python.gram index 91d73a9fffd193..7fc2ebde16588a 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 { - _PyAST_With(a, b, NULL, EXTRA) } + CHECK_VERSION(stmt_ty, 10, "Parenthesized context managers are", _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 225892414b8c19..5d9c5e8451c0c7 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -738,6 +738,12 @@ def test_ast_asdl_signature(self): expressions[0] = f"expr = {ast.expr.__subclasses__()[0].__doc__}" self.assertCountEqual(ast.expr.__doc__.split("\n"), expressions) + def test_parenthesized_with_feature_version(self): + ast.parse('with (CtxManager() as example): ...', feature_version=(3, 10)) + with self.assertRaises(SyntaxError): + ast.parse('with (CtxManager() as example): ...', feature_version=(3, 9)) + ast.parse('with CtxManager() as example: ...', feature_version=(3, 9)) + def test_issue40614_feature_version(self): ast.parse('f"{x=}"', feature_version=(3, 8)) with self.assertRaises(SyntaxError): diff --git a/Parser/parser.c b/Parser/parser.c index 3d59d4a704c239..2e2f1bee944de1 100644 --- a/Parser/parser.c +++ b/Parser/parser.c @@ -6515,7 +6515,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 = _PyAST_With ( a , b , NULL , EXTRA ); + _res = CHECK_VERSION ( stmt_ty , 10 , "Parenthesized context managers are" , _PyAST_With ( a , b , NULL , EXTRA ) ); if (_res == NULL && PyErr_Occurred()) { p->error_indicator = 1; p->level--; From 5c0ad11058b898de7b182c70e536e4f1ce5ec353 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 18 Jul 2022 05:10:31 +0000 Subject: [PATCH 2/3] =?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 --- .../2022-07-18-05-10-29.gh-issue-94949.OsZ7_s.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-07-18-05-10-29.gh-issue-94949.OsZ7_s.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-07-18-05-10-29.gh-issue-94949.OsZ7_s.rst b/Misc/NEWS.d/next/Core and Builtins/2022-07-18-05-10-29.gh-issue-94949.OsZ7_s.rst new file mode 100644 index 00000000000000..f1f9928921b4e1 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-07-18-05-10-29.gh-issue-94949.OsZ7_s.rst @@ -0,0 +1 @@ +:func:`ast.parse` will no longer parse parenthesized context managers when passed ``feature_version`` less than ``(3, 10)``. Patch by Shantanu Jain. From 3bf98c30227de7ab9ade7e9451079f4b6e940855 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Mon, 18 Jul 2022 09:30:36 -0700 Subject: [PATCH 3/3] Allow it with feature_version=(3, 9) as well --- Grammar/python.gram | 2 +- Lib/test/test_ast.py | 6 ++++-- .../2022-07-18-05-10-29.gh-issue-94949.OsZ7_s.rst | 2 +- Parser/parser.c | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Grammar/python.gram b/Grammar/python.gram index 7fc2ebde16588a..efdabdd77ff53f 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, 10, "Parenthesized context managers are", _PyAST_With(a, b, NULL, EXTRA)) } + CHECK_VERSION(stmt_ty, 9, "Parenthesized context managers are", _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 5d9c5e8451c0c7..4ff673701e57ae 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -740,9 +740,11 @@ def test_ast_asdl_signature(self): 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, 9)) - ast.parse('with CtxManager() as example: ...', feature_version=(3, 9)) + ast.parse('with (CtxManager() as example): ...', feature_version=(3, 8)) + ast.parse('with CtxManager() as example: ...', feature_version=(3, 8)) def test_issue40614_feature_version(self): ast.parse('f"{x=}"', feature_version=(3, 8)) diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-07-18-05-10-29.gh-issue-94949.OsZ7_s.rst b/Misc/NEWS.d/next/Core and Builtins/2022-07-18-05-10-29.gh-issue-94949.OsZ7_s.rst index f1f9928921b4e1..bc452d434da0f6 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2022-07-18-05-10-29.gh-issue-94949.OsZ7_s.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2022-07-18-05-10-29.gh-issue-94949.OsZ7_s.rst @@ -1 +1 @@ -:func:`ast.parse` will no longer parse parenthesized context managers when passed ``feature_version`` less than ``(3, 10)``. Patch by Shantanu Jain. +:func:`ast.parse` will no longer parse parenthesized context managers when passed ``feature_version`` less than ``(3, 9)``. Patch by Shantanu Jain. diff --git a/Parser/parser.c b/Parser/parser.c index 2e2f1bee944de1..e789ff8e5d55a3 100644 --- a/Parser/parser.c +++ b/Parser/parser.c @@ -6515,7 +6515,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 , 10 , "Parenthesized context managers are" , _PyAST_With ( a , b , NULL , EXTRA ) ); + _res = CHECK_VERSION ( stmt_ty , 9 , "Parenthesized context managers are" , _PyAST_With ( a , b , NULL , EXTRA ) ); if (_res == NULL && PyErr_Occurred()) { p->error_indicator = 1; p->level--;