gh-107967: Fix infinite recursion on invalid escape sequence warning (#107968)

This commit is contained in:
Lysandros Nikolaou 2023-08-15 14:26:42 +03:00 committed by GitHub
parent 13c36dc9ae
commit d66bc9e8a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 0 deletions

View File

@ -1673,5 +1673,15 @@ print(f'''{{
self.assertEqual(stdout.decode('utf-8').strip().replace('\r\n', '\n').replace('\r', '\n'), self.assertEqual(stdout.decode('utf-8').strip().replace('\r\n', '\n').replace('\r', '\n'),
"3\n=3") "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__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -1539,6 +1539,9 @@ error:
static int static int
warn_invalid_escape_sequence(struct tok_state *tok, int first_invalid_escape_char) warn_invalid_escape_sequence(struct tok_state *tok, int first_invalid_escape_char)
{ {
if (!tok->report_warnings) {
return 0;
}
PyObject *msg = PyUnicode_FromFormat( PyObject *msg = PyUnicode_FromFormat(
"invalid escape sequence '\\%c'", "invalid escape sequence '\\%c'",