8000 gh-100445: Improve error message for unterminated strings with escapes · hauntsaninja/cpython@bf712e8 · GitHub
[go: up one dir, main page]

Skip to content

Commit bf712e8

Browse files
committed
pythongh-100445: Improve error message for unterminated strings with escapes
1 parent 4cc63e0 commit bf712e8

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

Lib/test/test_syntax.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2156,8 +2156,14 @@ def test_error_parenthesis(self):
21562156

21572157
def test_error_string_literal(self):
21582158

2159-
self._check_error("'blech", "unterminated string literal")
2160-
self._check_error('"blech', "unterminated string literal")
2159+
self._check_error("'blech", r"unterminated string literal \(.*\)$")
2160+
self._check_error('"blech', r"unterminated string literal \(.*\)$")
2161+
self._check_error(
2162+
r'"blech\"', r"unterminated string literal \(.*\); perhaps you escaped the end quote"
2163+
)
2164+
self._check_error(
2165+
r'r"blech\"', r"unterminated string literal \(.*\); perhaps you escaped the end quote"
2166+
)
21612167
self._check_error("'''blech", "unterminated triple-quoted string literal")
21622168
self._check_error('"""blech', "unterminated triple-quoted string literal")
21632169

Parser/tokenizer.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,6 +2009,7 @@ tok_get(struct tok_state *tok, struct token *token)
20092009
int quote = c;
20102010
int quote_size = 1; /* 1 or 3 */
20112011
int end_quote_size = 0;
2012+
int has_escaped_quote = 0;
20122013

20132014
/* Nodes of type STRING, especially multi line strings
20142015
must be handled differently in order to get both
@@ -2056,8 +2057,13 @@ tok_get(struct tok_state *tok, struct token *token)
20562057
return MAKE_TOKEN(ERRORTOKEN);
20572058
}
20582059
else {
2059-
syntaxerror(tok, "unterminated string literal (detected at"
2060-
" line %d)", start);
2060+
if (has_escaped_quote) {
2061+
syntaxerror(tok, "unterminated string literal (detected at"
2062+
" line %d); perhaps you escaped the end quote?", start);
2063+
} else {
2064+
syntaxerror(tok, "unterminated string literal (detected at"
2065+
" line %d)", start);
2066+
}
20612067
if (c != '\n') {
20622068
tok->done = E_EOLS;
20632069
}
@@ -2070,7 +2076,11 @@ tok_get(struct tok_state *tok, struct token *token)
20702076
else {
20712077
end_quote_size = 0;
20722078
if (c == '\\') {
2073-
tok_nextc(tok); /* skip escaped char */
2079+
// skip escaped char, but record whether the escaped char
2080+
// was a quote
2081+
if (tok_nextc(tok) == quote) {
2082+
has_escaped_quote = 1;
2083+
}
20742084
}
20752085
}
20762086
}

0 commit comments

Comments
 (0)
0