diff options
author | Lysandros Nikolaou <lisandrosnik@gmail.com> | 2023-06-20 12:38:46 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-20 12:38:46 (GMT) |
commit | 6586cee27f32f0354fe4e77c7b8c6e399329b5e2 (patch) | |
tree | dae5d2b29d0add36ba8807f563245fab45222b20 /Parser/string_parser.c | |
parent | 155577de1b6a7f4404b2bf90bcc1a588201550da (diff) | |
download | cpython-6586cee27f32f0354fe4e77c7b8c6e399329b5e2.zip cpython-6586cee27f32f0354fe4e77c7b8c6e399329b5e2.tar.gz cpython-6586cee27f32f0354fe4e77c7b8c6e399329b5e2.tar.bz2 |
gh-105938: Emit a SyntaxWarning for escaped braces in an f-string (#105939)
Diffstat (limited to 'Parser/string_parser.c')
-rw-r--r-- | Parser/string_parser.c | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/Parser/string_parser.c b/Parser/string_parser.c index d4ce338..20459e8 100644 --- a/Parser/string_parser.c +++ b/Parser/string_parser.c @@ -12,6 +12,11 @@ static int warn_invalid_escape_sequence(Parser *p, const char *first_invalid_escape, Token *t) { unsigned char c = *first_invalid_escape; + if ((t->type == FSTRING_MIDDLE || t->type == FSTRING_END) && (c == '{' || c == '}')) { // in this case the tokenizer has already emitted a warning, + // see tokenizer.c:warn_invalid_escape_sequence + return 0; + } + int octal = ('4' <= c && c <= '7'); PyObject *msg = octal @@ -31,7 +36,7 @@ warn_invalid_escape_sequence(Parser *p, const char *first_invalid_escape, Token if (PyErr_WarnExplicitObject(category, msg, p->tok->filename, t->lineno, NULL, NULL) < 0) { if (PyErr_ExceptionMatches(category)) { - /* Replace the DeprecationWarning exception with a SyntaxError + /* Replace the Syntax/DeprecationWarning exception with a SyntaxError to get a more accurate error report */ PyErr_Clear(); |