Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 12844e6

Browse files
committed
Add tests for r84209 (crashes in the Ast builder)
Also remove one tab, and move a check closer to the possible failure.
1 parent 8101021 commit 12844e6

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

Lib/test/test_syntax.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,34 @@
484484
Traceback (most recent call last):
485485
SyntaxError: can't assign to literal
486486
487+
Corner-cases that used to fail to raise the correct error:
488+
489+
>>> def f(*, x=lambda __debug__:0): pass
490+
Traceback (most recent call last):
491+
SyntaxError: assignment to keyword
492+
493+
>>> def f(*args:(lambda __debug__:0)): pass
494+
Traceback (most recent call last):
495+
SyntaxError: assignment to keyword
496+
497+
>>> def f(**kwargs:(lambda __debug__:0)): pass
498+
Traceback (most recent call last):
499+
SyntaxError: assignment to keyword
500+
501+
>>> with (lambda *:0): pass
502+
Traceback (most recent call last):
503+
SyntaxError: named arguments must follow bare *
504+
505+
Corner-cases that used to crash:
506+
507+
>>> def f(**__debug__): pass
508+
Traceback (most recent call last):
509+
SyntaxError: assignment to keyword
510+
511+
>>> def f(*xx, __debug__): pass
512+
Traceback (most recent call last):
513+
SyntaxError: assignment to keyword
514+
487515
"""
488516

489517
import re

Python/ast.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ handle_keywordonly_args(struct compiling *c, const node *n, int start,
689689
if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
690690
expression = ast_for_expr(c, CHILD(n, i + 2));
691691
if (!expression)
692-
goto error;
692+
goto error;
693693
asdl_seq_SET(kwdefaults, j, expression);
694694
i += 2; /* '=' and test */
695695
}
@@ -892,14 +892,14 @@ ast_for_arguments(struct compiling *c, const node *n)
892892
ch = CHILD(n, i+1); /* tfpdef */
893893
assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
894894
kwarg = NEW_IDENTIFIER(CHILD(ch, 0));
895+
if (!kwarg)
896+
return NULL;
895897
if (NCH(ch) > 1) {
896898
/* there is an annotation on the kwarg */
897899
kwargannotation = ast_for_expr(c, CHILD(ch, 2));
898900
if (!kwargannotation)
899901
return NULL;
900902
}
901-
if (!kwarg)
902-
return NULL;
903903
if (forbidden_name(kwarg, CHILD(ch, 0), 0))
904904
return NULL;
905905
i += 3;

0 commit comments

Comments
 (0)