gh-88535: Improve syntax error for wrongly closed strings (#26633)

This commit is contained in:
Pablo Galindo Salgado 2025-02-13 01:30:20 +00:00 committed by GitHub
parent 56eda25633
commit 6fb5138776
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 1098 additions and 930 deletions

View file

@ -165,7 +165,7 @@ Improved error messages
error message prints the received number of values in more cases than before. error message prints the received number of values in more cases than before.
(Contributed by Tushar Sadhwani in :gh:`122239`.) (Contributed by Tushar Sadhwani in :gh:`122239`.)
.. code-block:: pycon .. code-block:: python
>>> x, y, z = 1, 2, 3, 4 >>> x, y, z = 1, 2, 3, 4
Traceback (most recent call last): Traceback (most recent call last):
@ -175,6 +175,17 @@ Improved error messages
ValueError: too many values to unpack (expected 3, got 4) ValueError: too many values to unpack (expected 3, got 4)
* When incorrectly closed strings are detected, the error message suggests
that the string may be intended to be part of the string. (Contributed by
Pablo Galindo in :gh:`88535`.)
.. code-block:: python
>>> "The interesting object "The important object" is very important"
Traceback (most recent call last):
SyntaxError: invalid syntax. Is this intended to be part of the string?
.. _whatsnew314-pep741: .. _whatsnew314-pep741:
PEP 741: Python Configuration C API PEP 741: Python Configuration C API

View file

@ -1178,6 +1178,9 @@ invalid_type_param:
} }
invalid_expression: invalid_expression:
| STRING a=(!STRING expression_without_invalid)+ STRING {
RAISE_SYNTAX_ERROR_KNOWN_RANGE( PyPegen_first_item(a, expr_ty), PyPegen_last_item(a, expr_ty),
"invalid syntax. Is this intended to be part of the string?") }
# !(NAME STRING) is not matched so we don't show this error with some invalid string prefixes like: kf"dsfsdf" # !(NAME STRING) is not matched so we don't show this error with some invalid string prefixes like: kf"dsfsdf"
# Soft keywords need to also be ignored because they can be parsed as NAME NAME # Soft keywords need to also be ignored because they can be parsed as NAME NAME
| !(NAME STRING | SOFT_KEYWORD) a=disjunction b=expression_without_invalid { | !(NAME STRING | SOFT_KEYWORD) a=disjunction b=expression_without_invalid {

View file

@ -2303,7 +2303,7 @@ class SyntaxErrorTests(unittest.TestCase):
) )
err = run_script(source.encode('cp437')) err = run_script(source.encode('cp437'))
self.assertEqual(err[-3], ' "¢¢¢¢¢¢" + f(4, x for x in range(1))') self.assertEqual(err[-3], ' "¢¢¢¢¢¢" + f(4, x for x in range(1))')
self.assertEqual(err[-2], ' ^^^^^^^^^^^^^^^^^^^') self.assertEqual(err[-2], ' ^^^')
# Check backwards tokenizer errors # Check backwards tokenizer errors
source = '# -*- coding: ascii -*-\n\n(\n' source = '# -*- coding: ascii -*-\n\n(\n'

View file

@ -312,6 +312,12 @@ SyntaxError: did you forget parentheses around the comprehension target?
Traceback (most recent call last): Traceback (most recent call last):
SyntaxError: did you forget parentheses around the comprehension target? SyntaxError: did you forget parentheses around the comprehension target?
# Incorrectly closed strings
>>> "The interesting object "The important object" is very important"
Traceback (most recent call last):
SyntaxError: invalid syntax. Is this intended to be part of the string?
# Missing commas in literals collections should not # Missing commas in literals collections should not
# produce special error messages regarding missing # produce special error messages regarding missing
# parentheses, but about missing commas instead # parentheses, but about missing commas instead

View file

@ -0,0 +1 @@
Improve syntax errors for incorrectly closed strings. Patch by Pablo Galindo

2003
Parser/parser.c generated

File diff suppressed because it is too large Load diff