bpo-44168: Fix error message in the parser for keyword arguments for invalid expressions (GH-26210)

This commit is contained in:
Pablo Galindo 2021-05-19 19:03:04 +01:00 committed by GitHub
parent 24ccc89547
commit 33c0c90dea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 1334 additions and 1284 deletions

View File

@ -843,7 +843,7 @@ invalid_arguments:
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, asdl_seq_GET(b, b->size-1)->target, "Generator expression must be parenthesized") } RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, asdl_seq_GET(b, b->size-1)->target, "Generator expression must be parenthesized") }
| a=args ',' args { _PyPegen_arguments_parsing_error(p, a) } | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
invalid_kwarg: invalid_kwarg:
| a=expression b='=' { | !(NAME '=') a=expression b='=' {
RAISE_SYNTAX_ERROR_KNOWN_RANGE( RAISE_SYNTAX_ERROR_KNOWN_RANGE(
a, b, "expression cannot contain assignment, perhaps you meant \"==\"?") } a, b, "expression cannot contain assignment, perhaps you meant \"==\"?") }

View File

@ -458,28 +458,33 @@ SyntaxError: expected ':'
... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS ... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299)
# >>> f(lambda x: x[0] = 3) >>> f(lambda x: x[0] = 3)
# Traceback (most recent call last): Traceback (most recent call last):
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="? SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
# Check that this error doesn't trigger for names:
>>> f(a={x: for x in {}})
Traceback (most recent call last):
SyntaxError: invalid syntax
The grammar accepts any test (basically, any expression) in the The grammar accepts any test (basically, any expression) in the
keyword slot of a call site. Test a few different options. keyword slot of a call site. Test a few different options.
# >>> f(x()=2) >>> f(x()=2)
# Traceback (most recent call last): Traceback (most recent call last):
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="? SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
# >>> f(a or b=1) >>> f(a or b=1)
# Traceback (most recent call last): Traceback (most recent call last):
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="? SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
# >>> f(x.y=1) >>> f(x.y=1)
# Traceback (most recent call last): Traceback (most recent call last):
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="? SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
# >>> f((x)=2) >>> f((x)=2)
# Traceback (most recent call last): Traceback (most recent call last):
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="? SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
# >>> f(True=2) >>> f(True=2)
# Traceback (most recent call last): Traceback (most recent call last):
# SyntaxError: cannot assign to True here. Maybe you meant '==' instead of '='? SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
>>> f(__debug__=1) >>> f(__debug__=1)
Traceback (most recent call last): Traceback (most recent call last):
SyntaxError: cannot assign to __debug__ SyntaxError: cannot assign to __debug__

View File

@ -0,0 +1,2 @@
Fix error message in the parser involving keyword arguments with invalid
expressions. Patch by Pablo Galindo

File diff suppressed because it is too large Load Diff