From d66bc9e8a7a8d6774d912a4b9d151885c4d8de1d Mon Sep 17 00:00:00 2001 From: Lysandros Nikolaou Date: Tue, 15 Aug 2023 14:26:42 +0300 Subject: [PATCH] gh-107967: Fix infinite recursion on invalid escape sequence warning (#107968) --- Lib/test/test_fstring.py | 10 ++++++++++ Parser/tokenizer.c | 3 +++ 2 files changed, 13 insertions(+) diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py index cb14bba2602..16f01973f99 100644 --- a/Lib/test/test_fstring.py +++ b/Lib/test/test_fstring.py @@ -1673,5 +1673,15 @@ print(f'''{{ self.assertEqual(stdout.decode('utf-8').strip().replace('\r\n', '\n').replace('\r', '\n'), "3\n=3") + def test_syntax_warning_infinite_recursion_in_file(self): + with temp_cwd(): + script = 'script.py' + with open(script, 'w') as f: + f.write(r"print(f'\{1}')") + + _, stdout, stderr = assert_python_ok(script) + self.assertIn(rb'\1', stdout) + self.assertEqual(len(stderr.strip().splitlines()), 2) + if __name__ == '__main__': unittest.main() diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c index 5a42f6f3573..b10c9f1f8ea 100644 --- a/Parser/tokenizer.c +++ b/Parser/tokenizer.c @@ -1539,6 +1539,9 @@ error: static int warn_invalid_escape_sequence(struct tok_state *tok, int first_invalid_escape_char) { + if (!tok->report_warnings) { + return 0; + } PyObject *msg = PyUnicode_FromFormat( "invalid escape sequence '\\%c'",