Skip to content

Commit a92c2d6

Browse files
authored
[3.10] gh-94996: Disallow parsing pos only params with feature_version < (3, 8) (GH-95935)
(cherry picked from commit b5e3ea2) Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Automerge-Triggered-By: GH:lysnikolaou
1 parent 44bf05e commit a92c2d6

File tree

5 files changed

+15
-6
lines changed

5 files changed

+15
-6
lines changed

Grammar/python.gram

+2-2
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,9 @@ params[arguments_ty]:
427427

428428
parameters[arguments_ty]:
429429
| a=slash_no_default b[asdl_arg_seq*]=param_no_default* c=param_with_default* d=[star_etc] {
430-
_PyPegen_make_arguments(p, a, NULL, b, c, d) }
430+
CHECK_VERSION(arguments_ty, 8, "Positional-only parameters are", _PyPegen_make_arguments(p, a, NULL, b, c, d)) }
431431
| a=slash_with_default b=param_with_default* c=[star_etc] {
432-
_PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
432+
CHECK_VERSION(arguments_ty, 8, "Positional-only parameters are", _PyPegen_make_arguments(p, NULL, a, NULL, b, c)) }
433433
| a[asdl_arg_seq*]=param_no_default+ b=param_with_default* c=[star_etc] {
434434
_PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
435435
| a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}

Lib/test/test_ast.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,14 @@ def test_ast_asdl_signature(self):
686686
expressions[0] = f"expr = {ast.expr.__subclasses__()[0].__doc__}"
687687
self.assertCountEqual(ast.expr.__doc__.split("\n"), expressions)
688688

689+
def test_positional_only_feature_version(self):
690+
ast.parse('def foo(x, /): ...', feature_version=(3, 8))
691+
ast.parse('def bar(x=1, /): ...', feature_version=(3, 8))
692+
with self.assertRaises(SyntaxError):
693+
ast.parse('def foo(x, /): ...', feature_version=(3, 7))
694+
with self.assertRaises(SyntaxError):
695+
ast.parse('def bar(x=1, /): ...', feature_version=(3, 7))
696+
689697
def test_parenthesized_with_feature_version(self):
690698
ast.parse('with (CtxManager() as example): ...', feature_version=(3, 10))
691699
# While advertised as a feature in Python 3.10, this was allowed starting 3.9
@@ -694,7 +702,7 @@ def test_parenthesized_with_feature_version(self):
694702
ast.parse('with (CtxManager() as example): ...', feature_version=(3, 8))
695703
ast.parse('with CtxManager() as example: ...', feature_version=(3, 8))
696704

697-
def test_issue40614_feature_version(self):
705+
def test_debug_f_string_feature_version(self):
698706
ast.parse('f"{x=}"', feature_version=(3, 8))
699707
with self.assertRaises(SyntaxError):
700708
ast.parse('f"{x=}"', feature_version=(3, 7))

Lib/test/test_type_comments.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def test_ignores(self):
323323
self.assertEqual(tree.type_ignores, [])
324324

325325
def test_longargs(self):
326-
for tree in self.parse_all(longargs):
326+
for tree in self.parse_all(longargs, minver=8):
327327
for t in tree.body:
328328
# The expected args are encoded in the function name
329329
todo = set(t.name[1:])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

Parser/parser.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -8997,7 +8997,7 @@ parameters_rule(Parser *p)
89978997
)
89988998
{
89998999
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?"));
9000-
_res = _PyPegen_make_arguments ( p , a , NULL , b , c , d );
9000+
_res = CHECK_VERSION ( arguments_ty , 8 , "Positional-only parameters are" , _PyPegen_make_arguments ( p , a , NULL , b , c , d ) );
90019001
if (_res == NULL && PyErr_Occurred()) {
90029002
p->error_indicator = 1;
90039003
p->level--;
@@ -9027,7 +9027,7 @@ parameters_rule(Parser *p)
90279027
)
90289028
{
90299029
D(fprintf(stderr, "%*c+ parameters[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "slash_with_default param_with_default* star_etc?"));
9030-
_res = _PyPegen_make_arguments ( p , NULL , a , NULL , b , c );
9030+
_res = CHECK_VERSION ( arguments_ty , 8 , "Positional-only parameters are" , _PyPegen_make_arguments ( p , NULL , a , NULL , b , c ) );
90319031
if (_res == NULL && PyErr_Occurred()) {
90329032
p->error_indicator = 1;
90339033
p->level--;

0 commit comments

Comments
 (0)