Skip to content

Commit

Permalink
pythongh-107967: Fix infinite recursion on invalid escape sequence wa…
Browse files Browse the repository at this point in the history
…rning (python#107968)
  • Loading branch information
lysnikolaou authored Aug 15, 2023
1 parent 13c36dc commit d66bc9e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
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

0 comments on commit d66bc9e

Please sign in to comment.