Skip to content

Commit 41d5b94

Browse files
authored
bpo-40246: Report a better error message for invalid string prefixes (GH-19476)
1 parent 402e1cd commit 41d5b94

File tree

5 files changed

+10
-1
lines changed

5 files changed

+10
-1
lines changed

Include/errcode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ extern "C" {
3131
#define E_LINECONT 25 /* Unexpected characters after a line continuation */
3232
#define E_IDENTIFIER 26 /* Invalid characters in identifier */
3333
#define E_BADSINGLE 27 /* Ill-formed single statement input */
34+
#define E_BADPREFIX 28 /* Bad string prefixes */
3435

3536
#ifdef __cplusplus
3637
}

Lib/test/test_fstring.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ def test_nested_fstrings(self):
841841
self.assertEqual(f'{f"{y}"*3}', '555')
842842

843843
def test_invalid_string_prefixes(self):
844-
self.assertAllRaise(SyntaxError, 'unexpected EOF while parsing',
844+
self.assertAllRaise(SyntaxError, 'invalid string prefix',
845845
["fu''",
846846
"uf''",
847847
"Fu''",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Report a specialized error message, `invalid string prefix`, when the tokenizer encounters a string with an invalid prefix.

Parser/tokenizer.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,10 @@ tok_get(struct tok_state *tok, const char **p_start, const char **p_end)
13921392
if (nonascii && !verify_identifier(tok)) {
13931393
return ERRORTOKEN;
13941394
}
1395+
if (c == '"' || c == '\'') {
1396+
tok->done = E_BADPREFIX;
1397+
return ERRORTOKEN;
1398+
}
13951399
*p_start = tok->start;
13961400
*p_end = tok->cur;
13971401

Python/pythonrun.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,9 @@ err_input(perrdetail *err)
15741574
case E_BADSINGLE:
15751575
msg = "multiple statements found while compiling a single statement";
15761576
break;
1577+
case E_BADPREFIX:
1578+
msg = "invalid string prefix";
1579+
break;
15771580
default:
15781581
fprintf(stderr, "error=%d\n", err->error);
15791582
msg = "unknown parsing error";

0 commit comments

Comments
 (0)