Skip to content

Commit d189480

Browse files
[3.12] gh-107967: Fix infinite recursion on invalid escape sequence warning (GH-107968) (#107970)
gh-107967: Fix infinite recursion on invalid escape sequence warning (GH-107968) (cherry picked from commit d66bc9e) Co-authored-by: Lysandros Nikolaou <[email protected]>
1 parent e8963a8 commit d189480

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

Lib/test/test_fstring.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,5 +1673,15 @@ def test_debug_in_file(self):
16731673
self.assertEqual(stdout.decode('utf-8').strip().replace('\r\n', '\n').replace('\r', '\n'),
16741674
"3\n=3")
16751675

1676+
def test_syntax_warning_infinite_recursion_in_file(self):
1677+
with temp_cwd():
1678+
script = 'script.py'
1679+
with open(script, 'w') as f:
1680+
f.write(r"print(f'\{1}')")
1681+
1682+
_, stdout, stderr = assert_python_ok(script)
1683+
self.assertIn(rb'\1', stdout)
1684+
self.assertEqual(len(stderr.strip().splitlines()), 2)
1685+
16761686
if __name__ == '__main__':
16771687
unittest.main()

Parser/tokenizer.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,9 @@ parser_warn(struct tok_state *tok, PyObject *category, const char *format, ...)
15441544
static int
15451545
warn_invalid_escape_sequence(struct tok_state *tok, int first_invalid_escape_char)
15461546
{
1547+
if (!tok->report_warnings) {
1548+
return 0;
1549+
}
15471550

15481551
PyObject *msg = PyUnicode_FromFormat(
15491552
"invalid escape sequence '\\%c'",

0 commit comments

Comments
 (0)