From 0755dda11d0207d6dc63bf44bae51b52f2eda25c Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Mon, 18 Jul 2022 21:30:03 -0700 Subject: [PATCH 1/3] gh-94996: Disallow parsing pos only param with feature_version < (3, 8) --- Grammar/python.gram | 4 ++-- Lib/test/test_ast.py | 10 +++++++++- Parser/parser.c | 4 ++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Grammar/python.gram b/Grammar/python.gram index 6a81e14decbde0..afae85d7233979 100644 --- a/Grammar/python.gram +++ b/Grammar/python.gram @@ -287,9 +287,9 @@ params[arguments_ty]: parameters[arguments_ty]: | a=slash_no_default b[asdl_arg_seq*]=param_no_default* c=param_with_default* d=[star_etc] { - _PyPegen_make_arguments(p, a, NULL, b, c, d) } + CHECK_VERSION(arguments_ty, 8, "Positional-only parameters are", _PyPegen_make_arguments(p, a, NULL, b, c, d)) } | a=slash_with_default b=param_with_default* c=[star_etc] { - _PyPegen_make_arguments(p, NULL, a, NULL, b, c) } + CHECK_VERSION(arguments_ty, 8, "Positional-only parameters are", _PyPegen_make_arguments(p, NULL, a, NULL, b, c)) } | a[asdl_arg_seq*]=param_no_default+ b=param_with_default* c=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, a, b, c) } | a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)} diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index 480089aa8af448..d39d7fce68a203 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -738,6 +738,14 @@ 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_positional_only_feature_version(self): + ast.parse('def foo(x, /): ...', feature_version=(3, 8)) + ast.parse('def bar(x=1, /): ...', feature_version=(3, 8)) + with self.assertRaises(SyntaxError): + ast.parse('def foo(x, /): ...', feature_version=(3, 7)) + with self.assertRaises(SyntaxError): + ast.parse('def bar(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 @@ -746,7 +754,7 @@ def test_parenthesized_with_feature_version(self): 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): + def test_debug_f_string_feature_version(self): ast.parse('f"{x=}"', feature_version=(3, 8)) with self.assertRaises(SyntaxError): ast.parse('f"{x=}"', feature_version=(3, 7)) diff --git a/Parser/parser.c b/Parser/parser.c index f4082aba56a272..15e833af47711e 100644 --- a/Parser/parser.c +++ b/Parser/parser.c @@ -4637,7 +4637,7 @@ parameters_rule(Parser *p) ) { D(fprintf(stderr, "%*c+ parameters[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "slash_no_default param_no_default* param_with_default* star_etc?")); - _res = _PyPegen_make_arguments ( p , a , NULL , b , c , d ); + _res = CHECK_VERSION ( arguments_ty , 8 , "Positional-only parameters are" , _PyPegen_make_arguments ( p , a , NULL , b , c , d ) ); if (_res == NULL && PyErr_Occurred()) { p->error_indicator = 1; p->level--; @@ -4667,7 +4667,7 @@ parameters_rule(Parser *p) ) { D(fprintf(stderr, "%*c+ parameters[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "slash_with_default param_with_default* star_etc?")); - _res = _PyPegen_make_arguments ( p , NULL , a , NULL , b , c ); + _res = CHECK_VERSION ( arguments_ty , 8 , "Positional-only parameters are" , _PyPegen_make_arguments ( p , NULL , a , NULL , b , c ) ); if (_res == NULL && PyErr_Occurred()) { p->error_indicator = 1; p->level--; From 15d10d0d815db0c519044022bb8cd85cdf9ec1c4 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 19 Jul 2022 04:34:58 +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-19-04-34-56.gh-issue-94996.dV564A.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-07-19-04-34-56.gh-issue-94996.dV564A.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-07-19-04-34-56.gh-issue-94996.dV564A.rst b/Misc/NEWS.d/next/Core and Builtins/2022-07-19-04-34-56.gh-issue-94996.dV564A.rst new file mode 100644 index 00000000000000..90c9ada079e077 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-07-19-04-34-56.gh-issue-94996.dV564A.rst @@ -0,0 +1 @@ +:func:`ast.parse` will no longer parse function definitions with positional-only params when passed ``feature_version`` less than ``(3, 8)``. Patch by Shantanu Jain. From df2af9f2e2e27ffca76e11a5ba26fda5b0ef50f0 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Mon, 18 Jul 2022 22:04:08 -0700 Subject: [PATCH 3/3] fix test_type_comments.test_longargs --- 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 668c1787f64d97..8db7394d1512aa 100644 --- a/Lib/test/test_type_comments.py +++ b/Lib/test/test_type_comments.py @@ -322,7 +322,7 @@ def test_ignores(self): self.assertEqual(tree.type_ignores, []) def test_longargs(self): - for tree in self.parse_all(longargs): + for tree in self.parse_all(longargs, minver=8): for t in tree.body: # The expected args are encoded in the function name todo = set(t.name[1:])