mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
gh-105938: Emit a SyntaxWarning for escaped braces in an f-string (#105939)
This commit is contained in:
parent
155577de1b
commit
6586cee27f
3 changed files with 18 additions and 6 deletions
|
@ -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();
|
||||
|
||||
|
|
|
@ -1559,12 +1559,12 @@ warn_invalid_escape_sequence(struct tok_state *tok, int first_invalid_escape_cha
|
|||
return -1;
|
||||
}
|
||||
|
||||
if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg, tok->filename,
|
||||
if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, tok->filename,
|
||||
tok->lineno, NULL, NULL) < 0) {
|
||||
Py_DECREF(msg);
|
||||
|
||||
if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
|
||||
/* Replace the DeprecationWarning exception with a SyntaxError
|
||||
if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
|
||||
/* Replace the SyntaxWarning exception with a SyntaxError
|
||||
to get a more accurate error report */
|
||||
PyErr_Clear();
|
||||
return syntaxerror(tok, "invalid escape sequence '\\%c'", (char) first_invalid_escape_char);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue