summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLysandros Nikolaou <lisandrosnik@gmail.com>2023-08-15 11:26:42 (GMT)
committerGitHub <noreply@github.com>2023-08-15 11:26:42 (GMT)
commitd66bc9e8a7a8d6774d912a4b9d151885c4d8de1d (patch)
tree1784e3f3647f1b47494ad477b16394cc5316159a
parent13c36dc9ae5240124932137de4a94d81292c6c5f (diff)
downloadcpython-d66bc9e8a7a8d6774d912a4b9d151885c4d8de1d.zip
cpython-d66bc9e8a7a8d6774d912a4b9d151885c4d8de1d.tar.gz
cpython-d66bc9e8a7a8d6774d912a4b9d151885c4d8de1d.tar.bz2
gh-107967: Fix infinite recursion on invalid escape sequence warning (#107968)
-rw-r--r--Lib/test/test_fstring.py10
-rw-r--r--Parser/tokenizer.c3
2 files changed, 13 insertions, 0 deletions
diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py
index cb14bba..16f0197 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 5a42f6f..b10c9f1 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'",