mirror of https://github.com/python/cpython
bpo-43823: Improve syntax errors for invalid dictionary literals (GH-25378)
This commit is contained in:
parent
23acadcc1c
commit
da74350174
|
@ -650,6 +650,8 @@ dict[expr_ty]:
|
|||
CHECK(asdl_expr_seq*, _PyPegen_get_keys(p, a)),
|
||||
CHECK(asdl_expr_seq*, _PyPegen_get_values(p, a)),
|
||||
EXTRA) }
|
||||
| '{' invalid_double_starred_kvpairs '}'
|
||||
|
||||
dictcomp[expr_ty]:
|
||||
| '{' a=kvpair b=for_if_clauses '}' { _PyAST_DictComp(a->key, a->value, b, EXTRA) }
|
||||
| invalid_dict_comprehension
|
||||
|
@ -882,3 +884,12 @@ invalid_elif_stmt:
|
|||
| 'elif' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
|
||||
invalid_while_stmt:
|
||||
| 'while' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
|
||||
|
||||
invalid_double_starred_kvpairs:
|
||||
| ','.double_starred_kvpair+ ',' invalid_kvpair
|
||||
| expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use a starred expression in a dictionary value") }
|
||||
| expression a=':' &('}'|',') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
|
||||
invalid_kvpair:
|
||||
| a=expression !(':') { RAISE_SYNTAX_ERROR("':' expected after dictionary key") }
|
||||
| expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use a starred expression in a dictionary value") }
|
||||
| expression a=':' {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
|
||||
|
|
|
@ -880,6 +880,38 @@ Ensure that early = are not matched by the parser as invalid comparisons
|
|||
Traceback (most recent call last):
|
||||
SyntaxError: invalid syntax
|
||||
|
||||
Incomplete dictionary literals
|
||||
|
||||
>>> {1:2, 3:4, 5}
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: ':' expected after dictionary key
|
||||
|
||||
>>> {1:2, 3:4, 5:}
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: expression expected after dictionary key and ':'
|
||||
|
||||
>>> {1: *12+1, 23: 1}
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: cannot use a starred expression in a dictionary value
|
||||
|
||||
>>> {1: *12+1}
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: cannot use a starred expression in a dictionary value
|
||||
|
||||
>>> {1: 23, 1: *12+1}
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: cannot use a starred expression in a dictionary value
|
||||
|
||||
>>> {1:}
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: expression expected after dictionary key and ':'
|
||||
|
||||
# Ensure that the error is not raise for syntax errors that happen after sets
|
||||
|
||||
>>> {1} $
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: invalid syntax
|
||||
|
||||
Make sure that the old "raise X, Y[, Z]" form is gone:
|
||||
>>> raise X, Y
|
||||
Traceback (most recent call last):
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
Improve syntax errors for invalid dictionary literals. Patch by Pablo
|
||||
Galindo.
|
1364
Parser/parser.c
1364
Parser/parser.c
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue