From a965db37f27ffb232312bc13d9a509f0d93fcd20 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Fri, 12 Aug 2022 11:41:02 -0700 Subject: [PATCH] gh-94996: Disallow lambda pos only params with feature_version < (3, 8) (GH-95934) --- Grammar/python.gram | 4 ++-- Lib/test/test_ast.py | 7 +++++++ Parser/parser.c | 4 ++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Grammar/python.gram b/Grammar/python.gram index afae85d7233..d4df78b679a 100644 --- a/Grammar/python.gram +++ b/Grammar/python.gram @@ -830,9 +830,9 @@ lambda_params[arguments_ty]: # lambda_parameters[arguments_ty]: | a=lambda_slash_no_default b[asdl_arg_seq*]=lambda_param_no_default* c=lambda_param_with_default* d=[lambda_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=lambda_slash_with_default b=lambda_param_with_default* c=[lambda_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*]=lambda_param_no_default+ b=lambda_param_with_default* c=[lambda_star_etc] { _PyPegen_make_arguments(p, NULL, NULL, a, b, c) } | a=lambda_param_with_default+ b=[lambda_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 2f3e9d50190..4cfefe4ac3d 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -746,6 +746,13 @@ class AST_Tests(unittest.TestCase): with self.assertRaises(SyntaxError): ast.parse('def bar(x=1, /): ...', feature_version=(3, 7)) + ast.parse('lambda x, /: ...', feature_version=(3, 8)) + ast.parse('lambda x=1, /: ...', feature_version=(3, 8)) + with self.assertRaises(SyntaxError): + ast.parse('lambda x, /: ...', feature_version=(3, 7)) + 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 diff --git a/Parser/parser.c b/Parser/parser.c index 15e833af477..c9366d59923 100644 --- a/Parser/parser.c +++ b/Parser/parser.c @@ -14659,7 +14659,7 @@ lambda_parameters_rule(Parser *p) ) { D(fprintf(stderr, "%*c+ lambda_parameters[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "lambda_slash_no_default lambda_param_no_default* lambda_param_with_default* lambda_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--; @@ -14689,7 +14689,7 @@ lambda_parameters_rule(Parser *p) ) { D(fprintf(stderr, "%*c+ lambda_parameters[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "lambda_slash_with_default lambda_param_with_default* lambda_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--;