summaryrefslogtreecommitdiffstats
path: root/Parser
diff options
context:
space:
mode:
authorShantanu <12621235+hauntsaninja@users.noreply.github.com>2023-10-18 12:58:51 (GMT)
committerGitHub <noreply@github.com>2023-10-18 12:58:51 (GMT)
commit3156d193b81f7fefbafa1a5299bc9588a6768956 (patch)
tree1347c212ffbb880dd707f6e17b7ad6ba3388d127 /Parser
parentbaefbb21d91db2d950706737a6ebee9b2eff5c2d (diff)
downloadcpython-3156d193b81f7fefbafa1a5299bc9588a6768956.zip
cpython-3156d193b81f7fefbafa1a5299bc9588a6768956.tar.gz
cpython-3156d193b81f7fefbafa1a5299bc9588a6768956.tar.bz2
gh-100445: Improve error message for unterminated strings with escapes (#100446)
Diffstat (limited to 'Parser')
-rw-r--r--Parser/lexer/lexer.c18
1 files changed, 16 insertions, 2 deletions
diff --git a/Parser/lexer/lexer.c b/Parser/lexer/lexer.c
index 1a01bb0..2ba24a2 100644
--- a/Parser/lexer/lexer.c
+++ b/Parser/lexer/lexer.c
@@ -972,6 +972,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
int quote = c;
int quote_size = 1; /* 1 or 3 */
int end_quote_size = 0;
+ int has_escaped_quote = 0;
/* Nodes of type STRING, especially multi line strings
must be handled differently in order to get both
@@ -1037,8 +1038,18 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
return MAKE_TOKEN(ERRORTOKEN);
}
else {
- _PyTokenizer_syntaxerror(tok, "unterminated string literal (detected at"
- " line %d)", start);
+ if (has_escaped_quote) {
+ _PyTokenizer_syntaxerror(
+ tok,
+ "unterminated string literal (detected at line %d); "
+ "perhaps you escaped the end quote?",
+ start
+ );
+ } else {
+ _PyTokenizer_syntaxerror(
+ tok, "unterminated string literal (detected at line %d)", start
+ );
+ }
if (c != '\n') {
tok->done = E_EOLS;
}
@@ -1052,6 +1063,9 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
end_quote_size = 0;
if (c == '\\') {
c = tok_nextc(tok); /* skip escaped char */
+ if (c == quote) { /* but record whether the escaped char was a quote */
+ has_escaped_quote = 1;
+ }
if (c == '\r') {
c = tok_nextc(tok);
}