8000 gh-105800: Issue SyntaxWarning in f-strings for invalid escape sequen… · python/cpython@12b6d84 · GitHub
[go: up one dir, main page]

Skip to content

Commit 12b6d84

Browse files
authored
gh-105800: Issue SyntaxWarning in f-strings for invalid escape sequences (#105801)
1 parent 698a0da commit 12b6d84

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

Lib/test/test_fstring.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,9 @@ def test_backslashes_in_string_part(self):
907907
with self.assertWarns(DeprecationWarning): # invalid escape sequence
908908
value = eval(r"f'\{6*7}'")
909909
self.assertEqual(value, '\\42')
910+
with self.assertWarns(SyntaxWarning): # invalid escape sequence
911+
value = eval(r"f'\g'")
912+
self.assertEqual(value, '\\g')
910913
self.assertEqual(f'\\{6*7}', '\\42')
911914
self.assertEqual(fr'\{6*7}', '\\42')
912915

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Correctly issue :exc:`SyntaxWarning` in f-strings if invalid sequences are
2+
used. Patch by Pablo Galindo

Parser/action_helpers.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,7 @@ _PyPegen_nonparen_genexp_in_call(Parser *p, expr_ty args, asdl_comprehension_seq
12251225
// Fstring stuff
12261226

12271227
static expr_ty
1228-
_PyPegen_decode_fstring_part(Parser* p, int is_raw, expr_ty constant) {
1228+
_PyPegen_decode_fstring_part(Parser* p, int is_raw, expr_ty constant, Token* token) {
12291229
assert(PyUnicode_CheckExact(constant->v.Constant.value));
12301230

12311231
const char* bstr = PyUnicode_AsUTF8(constant->v.Constant.value);
@@ -1241,7 +1241,7 @@ _PyPegen_decode_fstring_part(Parser* p, int is_raw, expr_ty constant) {
12411241
}
12421242

12431243
is_raw = is_raw || strchr(bstr, '\\') == NULL;
1244-
PyObject *str = _PyPegen_decode_string(p, is_raw, bstr, len, NULL);
1244+
PyObject *str = _PyPegen_decode_string(p, is_raw, bstr, len, token);
12451245
if (str == NULL) {
12461246
_Pypegen_raise_decode_error(p);
12471247
return NULL;
@@ -1315,7 +1315,7 @@ _PyPegen_joined_str(Parser *p, Token* a, asdl_expr_seq* raw_expressions, Token*b
13151315
for (Py_ssize_t i = 0; i < n_items; i++) {
13161316
expr_ty item = asdl_seq_GET(expr, i);
13171317
if (item->kind == Constant_kind) {
1318-
item = _PyPegen_decode_fstring_part(p, is_raw, item);
1318+
item = _PyPegen_decode_fstring_part(p, is_raw, item, b);
13191319
if (item == NULL) {
13201320
return NULL;
13211321
}

0 commit comments

Comments
 (0)
0