Skip to content

Commit d66bc9e

Browse files
authored
gh-107967: Fix infinite recursion on invalid escape sequence warning (#107968)
1 parent 13c36dc commit d66bc9e

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
@@ -1539,6 +1539,9 @@ parser_warn(struct tok_state *tok, PyObject *category, const char *format, ...)
15391539
static int
15401540
warn_invalid_escape_sequence(struct tok_state *tok, int first_invalid_escape_char)
15411541
{
1542+
if (!tok->report_warnings) {
1543+
return 0;
1544+
}
15421545

15431546
PyObject *msg = PyUnicode_FromFormat(
15441547
"invalid escape sequence '\\%c'",

0 commit comments

Comments
 (0)