From d7972eed3b43f854c6e0da2efc1e3ee23caf3563 Mon Sep 17 00:00:00 2001 From: Lysandros Nikolaou Date: Sat, 11 Apr 2020 20:46:47 +0300 Subject: [PATCH 1/2] bpo-40246: Report a different error message for invalid string prefixes --- Include/errcode.h | 1 + Lib/test/test_fstring.py | 2 +- Parser/tokenizer.c | 4 ++++ Python/pythonrun.c | 3 +++ 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Include/errcode.h b/Include/errcode.h index b37cd261d5ec4d..9af8d5c03d59ba 100644 --- a/Include/errcode.h +++ b/Include/errcode.h @@ -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 } diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py index 49663923e7f5aa..ef0ccb8cf53c10 100644 --- a/Lib/test/test_fstring.py +++ b/Lib/test/test_fstring.py @@ -841,7 +841,7 @@ def test_nested_fstrings(self): 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''", diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c index c650442a4a7fb8..97986aa8ef40aa 100644 --- a/Parser/tokenizer.c +++ b/Parser/tokenizer.c @@ -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; diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 95571a8c7518a1..eb9159f1b5c522 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -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"; From 5eb365b3531b4024d4aad32d05cff82b41659f01 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 11 Apr 2020 17:52:07 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Core and Builtins/2020-04-11-17-52-03.bpo-40246.vXPze5.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-04-11-17-52-03.bpo-40246.vXPze5.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-04-11-17-52-03.bpo-40246.vXPze5.rst b/Misc/NEWS.d/next/Core and Builtins/2020-04-11-17-52-03.bpo-40246.vXPze5.rst new file mode 100644 index 00000000000000..056b7f84729120 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-04-11-17-52-03.bpo-40246.vXPze5.rst @@ -0,0 +1 @@ +Report a specialized error message, `invalid string prefix`, when the tokenizer encounters a string with an invalid prefix. \ No newline at end of file