Skip to content

Commit 6fb5138

Browse files
authored
gh-88535: Improve syntax error for wrongly closed strings (#26633)
1 parent 56eda25 commit 6fb5138

File tree

6 files changed

+779
-611
lines changed

6 files changed

+779
-611
lines changed

Doc/whatsnew/3.14.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ Improved error messages
165165
error message prints the received number of values in more cases than before.
166166
(Contributed by Tushar Sadhwani in :gh:`122239`.)
167167

168-
.. code-block:: pycon
168+
.. code-block:: python
169169
170170
>>> x, y, z = 1, 2, 3, 4
171171
Traceback (most recent call last):
@@ -175,6 +175,17 @@ Improved error messages
175175
ValueError: too many values to unpack (expected 3, got 4)
176176
177177
178+
* When incorrectly closed strings are detected, the error message suggests
179+
that the string may be intended to be part of the string. (Contributed by
180+
Pablo Galindo in :gh:`88535`.)
181+
182+
.. code-block:: python
183+
184+
>>> "The interesting object "The important object" is very important"
185+
Traceback (most recent call last):
186+
SyntaxError: invalid syntax. Is this intended to be part of the string?
187+
188+
178189
.. _whatsnew314-pep741:
179190

180191
PEP 741: Python Configuration C API

Grammar/python.gram

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,9 @@ invalid_type_param:
11781178
}
11791179

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

Lib/test/test_exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2303,7 +2303,7 @@ def test_encodings(self):
23032303
)
23042304
err = run_script(source.encode('cp437'))
23052305
self.assertEqual(err[-3], ' "¢¢¢¢¢¢" + f(4, x for x in range(1))')
2306-
self.assertEqual(err[-2], ' ^^^^^^^^^^^^^^^^^^^')
2306+
self.assertEqual(err[-2], ' ^^^')
23072307

23082308
# Check backwards tokenizer errors
23092309
source = '# -*- coding: ascii -*-\n\n(\n'

Lib/test/test_syntax.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,12 @@
312312
Traceback (most recent call last):
313313
SyntaxError: did you forget parentheses around the comprehension target?
314314
315+
# Incorrectly closed strings
316+
317+
>>> "The interesting object "The important object" is very important"
318+
Traceback (most recent call last):
319+
SyntaxError: invalid syntax. Is this intended to be part of the string?
320+
315321
# Missing commas in literals collections should not
316322
# produce special error messages regarding missing
317323
# parentheses, but about missing commas instead
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve syntax errors for incorrectly closed strings. Patch by Pablo Galindo

0 commit comments

Comments
 (0)