Skip to content

Commit 6586cee

Browse files
authored
gh-105938: Emit a SyntaxWarning for escaped braces in an f-string (#105939)
1 parent 155577d commit 6586cee

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

Lib/test/test_fstring.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import types
1414
import decimal
1515
import unittest
16+
import warnings
1617
from test import support
1718
from test.support.os_helper import temp_cwd
1819
from test.support.script_helper import assert_python_failure, assert_python_ok
@@ -904,7 +905,7 @@ def test_backslashes_in_string_part(self):
904905
self.assertEqual(f'2\x203', '2 3')
905906
self.assertEqual(f'\x203', ' 3')
906907

907-
with self.assertWarns(DeprecationWarning): # invalid escape sequence
908+
with self.assertWarns(SyntaxWarning): # invalid escape sequence
908909
value = eval(r"f'\{6*7}'")
909910
self.assertEqual(value, '\\42')
910911
with self.assertWarns(SyntaxWarning): # invalid escape sequence
@@ -1037,7 +1038,7 @@ def test_fstring_backslash_before_double_bracket(self):
10371038
]
10381039
for case, expected_result in deprecated_cases:
10391040
with self.subTest(case=case, expected_result=expected_result):
1040-
with self.assertWarns(DeprecationWarning):
1041+
with self.assertWarns(SyntaxWarning):
10411042
result = eval(case)
10421043
self.assertEqual(result, expected_result)
10431044
self.assertEqual(fr'\{{\}}', '\\{\\}')
@@ -1046,6 +1047,12 @@ def test_fstring_backslash_before_double_bracket(self):
10461047
self.assertEqual(fr'\}}{1+1}', '\\}2')
10471048
self.assertEqual(fr'{1+1}\}}', '2\\}')
10481049

1050+
def test_fstring_backslash_before_double_bracket_warns_once(self):
1051+
with warnings.catch_warnings(record=True) as w:
1052+
eval(r"f'\{{'")
1053+
self.assertEqual(len(w), 1)
1054+
self.assertEqual(w[0].category, SyntaxWarning)
1055+
10491056
def test_fstring_backslash_prefix_raw(self):
10501057
self.assertEqual(f'\\', '\\')
10511058
self.assertEqual(f'\\\\', '\\\\')

Parser/string_parser.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ static int
1212
warn_invalid_escape_sequence(Parser *p, const char *first_invalid_escape, Token *t)
1313
{
1414
unsigned char c = *first_invalid_escape;
15+
if ((t->type == FSTRING_MIDDLE || t->type == FSTRING_END) && (c == '{' || c == '}')) { // in this case the tokenizer has already emitted a warning,
16+
// see tokenizer.c:warn_invalid_escape_sequence
17+
return 0;
18+
}
19+
1520
int octal = ('4' <= c && c <= '7');
1621
PyObject *msg =
1722
octal
@@ -31,7 +36,7 @@ warn_invalid_escape_sequence(Parser *p, const char *first_invalid_escape, Token
3136
if (PyErr_WarnExplicitObject(category, msg, p->tok->filename,
3237
t->lineno, NULL, NULL) < 0) {
3338
if (PyErr_ExceptionMatches(category)) {
34-
/* Replace the DeprecationWarning exception with a SyntaxError
39+
/* Replace the Syntax/DeprecationWarning exception with a SyntaxError
3540
to get a more accurate error report */
3641
PyErr_Clear();
3742

Parser/tokenizer.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,12 +1559,12 @@ warn_invalid_escape_sequence(struct tok_state *tok, int first_invalid_escape_cha
15591559
return -1;
15601560
}
15611561

1562-
if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg, tok->filename,
1562+
if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, tok->filename,
15631563
tok->lineno, NULL, NULL) < 0) {
15641564
Py_DECREF(msg);
15651565

1566-
if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
1567-
/* Replace the DeprecationWarning exception with a SyntaxError
1566+
if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
1567+
/* Replace the SyntaxWarning exception with a SyntaxError
15681568
to get a more accurate error report */
15691569
PyErr_Clear();
15701570
return syntaxerror(tok, "invalid escape sequence '\\%c'", (char) first_invalid_escape_char);

0 commit comments

Comments
 (0)