Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4fb56c9

Browse filesBrowse files
committed
gh-100445: Improve error message for unterminated strings with escapes
1 parent 0b38ce4 commit 4fb56c9
Copy full SHA for 4fb56c9

File tree

Expand file treeCollapse file tree

3 files changed

+20
-4
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+20
-4
lines changed

‎Lib/test/test_syntax.py

Copy file name to clipboardExpand all lines: Lib/test/test_syntax.py
+8-2Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,8 +2262,14 @@ def test_error_parenthesis(self):
22622262

22632263
def test_error_string_literal(self):
22642264

2265-
self._check_error("'blech", "unterminated string literal")
2266-
self._check_error('"blech', "unterminated string literal")
2265+
self._check_error("'blech", r"unterminated string literal \(.*\)$")
2266+
self._check_error('"blech', r"unterminated string literal \(.*\)$")
2267+
self._check_error(
2268+
r'"blech\"', r"unterminated string literal \(.*\); perhaps you escaped the end quote"
2269+
)
2270+
self._check_error(
2271+
r'r"blech\"', r"unterminated string literal \(.*\); perhaps you escaped the end quote"
2272+
)
22672273
self._check_error("'''blech", "unterminated triple-quoted string literal")
22682274
self._check_error('"""blech', "unterminated triple-quoted string literal")
22692275

+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve error message for unterminated strings with escapes.

‎Parser/tokenizer.c

Copy file name to clipboardExpand all lines: Parser/tokenizer.c
+11-2Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2397,6 +2397,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
23972397
int quote = c;
23982398
int quote_size = 1; /* 1 or 3 */
23992399
int end_quote_size = 0;
2400+
int has_escaped_quote = 0;
24002401

24012402
/* Nodes of type STRING, especially multi line strings
24022403
must be handled differently in order to get both
@@ -2462,8 +2463,13 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
24622463
return MAKE_TOKEN(ERRORTOKEN);
24632464
}
24642465
else {
2465-
syntaxerror(tok, "unterminated string literal (detected at"
2466-
" line %d)", start);
2466+
if (has_escaped_quote) {
2467+
syntaxerror(tok, "unterminated string literal (detected at"
2468+
" line %d); perhaps you escaped the end quote?", start);
2469+
} else {
2470+
syntaxerror(tok, "unterminated string literal (detected at"
2471+
" line %d)", start);
2472+
}
24672473
if (c != '\n') {
24682474
tok->done = E_EOLS;
24692475
}
@@ -2477,6 +2483,9 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
24772483
end_quote_size = 0;
24782484
if (c == '\\') {
24792485
c = tok_nextc(tok); /* skip escaped char */
2486+
if (c == quote) { /* but record whether the escaped char was a quote */
2487+
has_escaped_quote = 1;
2488+
}
24802489
if (c == '\r') {
24812490
c = tok_nextc(tok);
24822491
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.