mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
bpo-24612: Improve syntax error for 'not' after an operator (GH-28170)
Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
This commit is contained in:
parent
771902c257
commit
61599a48f5
4 changed files with 1392 additions and 964 deletions
|
@ -778,6 +778,7 @@ bitwise_and[expr_ty]:
|
||||||
shift_expr[expr_ty]:
|
shift_expr[expr_ty]:
|
||||||
| a=shift_expr '<<' b=sum { _PyAST_BinOp(a, LShift, b, EXTRA) }
|
| a=shift_expr '<<' b=sum { _PyAST_BinOp(a, LShift, b, EXTRA) }
|
||||||
| a=shift_expr '>>' b=sum { _PyAST_BinOp(a, RShift, b, EXTRA) }
|
| a=shift_expr '>>' b=sum { _PyAST_BinOp(a, RShift, b, EXTRA) }
|
||||||
|
| invalid_arithmetic
|
||||||
| sum
|
| sum
|
||||||
|
|
||||||
# Arithmetic operators
|
# Arithmetic operators
|
||||||
|
@ -794,6 +795,7 @@ term[expr_ty]:
|
||||||
| a=term '//' b=factor { _PyAST_BinOp(a, FloorDiv, b, EXTRA) }
|
| a=term '//' b=factor { _PyAST_BinOp(a, FloorDiv, b, EXTRA) }
|
||||||
| a=term '%' b=factor { _PyAST_BinOp(a, Mod, b, EXTRA) }
|
| a=term '%' b=factor { _PyAST_BinOp(a, Mod, b, EXTRA) }
|
||||||
| a=term '@' b=factor { CHECK_VERSION(expr_ty, 5, "The '@' operator is", _PyAST_BinOp(a, MatMult, b, EXTRA)) }
|
| a=term '@' b=factor { CHECK_VERSION(expr_ty, 5, "The '@' operator is", _PyAST_BinOp(a, MatMult, b, EXTRA)) }
|
||||||
|
| invalid_factor
|
||||||
| factor
|
| factor
|
||||||
|
|
||||||
factor[expr_ty] (memo):
|
factor[expr_ty] (memo):
|
||||||
|
@ -1415,3 +1417,8 @@ invalid_replacement_field:
|
||||||
invalid_conversion_character:
|
invalid_conversion_character:
|
||||||
| '!' &(':' | '}') { RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: missing conversion character") }
|
| '!' &(':' | '}') { RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: missing conversion character") }
|
||||||
| '!' !NAME { RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: invalid conversion character") }
|
| '!' !NAME { RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: invalid conversion character") }
|
||||||
|
|
||||||
|
invalid_arithmetic:
|
||||||
|
| sum ('+'|'-'|'*'|'/'|'%'|'//'|'@') a='not' b=inversion { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "'not' after an operator must be parenthesized") }
|
||||||
|
invalid_factor:
|
||||||
|
| ('+' | '-' | '~') a='not' b=factor { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "'not' after an operator must be parenthesized") }
|
||||||
|
|
|
@ -1712,6 +1712,49 @@ SyntaxError: only single target (not tuple) can be annotated
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
SyntaxError: only single target (not list) can be annotated
|
SyntaxError: only single target (not list) can be annotated
|
||||||
|
|
||||||
|
# 'not' after operators:
|
||||||
|
|
||||||
|
>>> 3 + not 3
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: 'not' after an operator must be parenthesized
|
||||||
|
|
||||||
|
>>> 3 * not 3
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: 'not' after an operator must be parenthesized
|
||||||
|
|
||||||
|
>>> + not 3
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: 'not' after an operator must be parenthesized
|
||||||
|
|
||||||
|
>>> - not 3
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: 'not' after an operator must be parenthesized
|
||||||
|
|
||||||
|
>>> ~ not 3
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: 'not' after an operator must be parenthesized
|
||||||
|
|
||||||
|
>>> 3 + - not 3
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: 'not' after an operator must be parenthesized
|
||||||
|
|
||||||
|
>>> 3 + not -1
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: 'not' after an operator must be parenthesized
|
||||||
|
|
||||||
|
# Check that we don't introduce misleading errors
|
||||||
|
>>> not 1 */ 2
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: invalid syntax
|
||||||
|
|
||||||
|
>>> not 1 +
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: invalid syntax
|
||||||
|
|
||||||
|
>>> not + 1 +
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: invalid syntax
|
||||||
|
|
||||||
Corner-cases that used to fail to raise the correct error:
|
Corner-cases that used to fail to raise the correct error:
|
||||||
|
|
||||||
>>> def f(*, x=lambda __debug__:0): pass
|
>>> def f(*, x=lambda __debug__:0): pass
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Improve the :exc:`SyntaxError` that happens when 'not' appears after an
|
||||||
|
operator. Patch by Pablo Galindo
|
2304
Parser/parser.c
generated
2304
Parser/parser.c
generated
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue