Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
bpo-44369: Improve syntax error for wrongly closed strings
  • Loading branch information
pablogsal committed Feb 13, 2025
commit 76d287388f9bb80ee3e86083fa01dccaf89eb489
3 changes: 3 additions & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,9 @@ invalid_type_param:
invalid_expression:
# !(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
| 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 | SOFT_KEYWORD) a=disjunction b=expression_without_invalid {
_PyPegen_check_legacy_stmt(p, a) ? NULL : p->tokens[p->mark-1]->level == 0 ? NULL :
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") }
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@
Traceback (most recent call last):
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
# produce special error messages regarding missing
# parentheses, but about missing commas instead
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve syntax errors for incorrectly closed strings. Patch by Pablo Galindo