diff options
author | Shantanu <12621235+hauntsaninja@users.noreply.github.com> | 2022-08-12 19:03:44 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-12 19:03:44 (GMT) |
commit | a92c2d6eb55a64881b978cfc4b531dad3d14984b (patch) | |
tree | d91845c151b6c992773082d749e08727b7def465 | |
parent | 44bf05ed0cea7f9ddc0713daed38d9bb2f35c981 (diff) | |
download | cpython-a92c2d6eb55a64881b978cfc4b531dad3d14984b.zip cpython-a92c2d6eb55a64881b978cfc4b531dad3d14984b.tar.gz cpython-a92c2d6eb55a64881b978cfc4b531dad3d14984b.tar.bz2 |
[3.10] gh-94996: Disallow parsing pos only params with feature_version < (3, 8) (GH-95935)
(cherry picked from commit https://github.com/python/cpython/commit/b5e3ea286289fcad12be78480daf3756e350f69f)
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Automerge-Triggered-By: GH:lysnikolaou
-rw-r--r-- | Grammar/python.gram | 4 | ||||
-rw-r--r-- | Lib/test/test_ast.py | 10 | ||||
-rw-r--r-- | Lib/test/test_type_comments.py | 2 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Core and Builtins/2022-07-19-04-34-56.gh-issue-94996.dV564A.rst | 1 | ||||
-rw-r--r-- | Parser/parser.c | 4 |
5 files changed, 15 insertions, 6 deletions
diff --git a/Grammar/python.gram b/Grammar/python.gram index 7570748..dcba7ad 100644 --- a/Grammar/python.gram +++ b/Grammar/python.gram @@ -427,9 +427,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 eb86109..5bbee8c 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -686,6 +686,14 @@ class AST_Tests(unittest.TestCase): 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 @@ -694,7 +702,7 @@ class AST_Tests(unittest.TestCase): 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/Lib/test/test_type_comments.py b/Lib/test/test_type_comments.py index 71d1430..7a348be 100644 --- a/Lib/test/test_type_comments.py +++ b/Lib/test/test_type_comments.py @@ -323,7 +323,7 @@ class TypeCommentTests(unittest.TestCase): 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:]) 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 0000000..90c9ada --- /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. diff --git a/Parser/parser.c b/Parser/parser.c index c0080e80..cadb295 100644 --- a/Parser/parser.c +++ b/Parser/parser.c @@ -8997,7 +8997,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--; @@ -9027,7 +9027,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--; |