Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/test/test_fstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -1673,5 +1673,15 @@ def test_debug_in_file(self):
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()
3 changes: 3 additions & 0 deletions Parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,9 @@ parser_warn(struct tok_state *tok, PyObject *category, const char *format, ...)
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'",
Expand Down