bpo-40246: Report a better error message for invalid string prefixes (GH-19476)

This commit is contained in:
Lysandros Nikolaou 2020-04-12 21:21:00 +03:00 committed by GitHub
parent 402e1cdb13
commit 41d5b94af4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 10 additions and 1 deletions

View File

@ -31,6 +31,7 @@ extern "C" {
#define E_LINECONT 25 /* Unexpected characters after a line continuation */
#define E_IDENTIFIER 26 /* Invalid characters in identifier */
#define E_BADSINGLE 27 /* Ill-formed single statement input */
#define E_BADPREFIX 28 /* Bad string prefixes */
#ifdef __cplusplus
}

View File

@ -841,7 +841,7 @@ non-important content
self.assertEqual(f'{f"{y}"*3}', '555')
def test_invalid_string_prefixes(self):
self.assertAllRaise(SyntaxError, 'unexpected EOF while parsing',
self.assertAllRaise(SyntaxError, 'invalid string prefix',
["fu''",
"uf''",
"Fu''",

View File

@ -0,0 +1 @@
Report a specialized error message, `invalid string prefix`, when the tokenizer encounters a string with an invalid prefix.

View File

@ -1392,6 +1392,10 @@ tok_get(struct tok_state *tok, const char **p_start, const char **p_end)
if (nonascii && !verify_identifier(tok)) {
return ERRORTOKEN;
}
if (c == '"' || c == '\'') {
tok->done = E_BADPREFIX;
return ERRORTOKEN;
}
*p_start = tok->start;
*p_end = tok->cur;

View File

@ -1574,6 +1574,9 @@ err_input(perrdetail *err)
case E_BADSINGLE:
msg = "multiple statements found while compiling a single statement";
break;
case E_BADPREFIX:
msg = "invalid string prefix";
break;
default:
fprintf(stderr, "error=%d\n", err->error);
msg = "unknown parsing error";