From b05e4bdd321c0834d97afdaef9fd5cf879fdb1e6 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Fri, 27 Oct 2023 12:19:34 +0900 Subject: [PATCH 1/3] gh-111380: Show SyntaxWarnings only once when parsing if invalid syntax is encouintered (GH-111381) (cherry picked from commit 3d2f1f0b830d86f16f42c42b54d3ea4453dac318) Co-authored-by: Pablo Galindo Salgado --- Lib/test/test_string_literals.py | 12 ++++++++++++ .../2023-10-27-11-51-40.gh-issue-111380.vgSbir.rst | 2 ++ Parser/string_parser.c | 5 +++++ 3 files changed, 19 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-10-27-11-51-40.gh-issue-111380.vgSbir.rst diff --git a/Lib/test/test_string_literals.py b/Lib/test/test_string_literals.py index 7247b7e48bc2b6..409d95e2c1ffe7 100644 --- a/Lib/test/test_string_literals.py +++ b/Lib/test/test_string_literals.py @@ -131,6 +131,18 @@ def test_eval_str_invalid_escape(self): self.assertEqual(exc.lineno, 1) self.assertEqual(exc.offset, 1) + # Check that the warning is raised ony once if there are syntax errors + + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter('always', category=SyntaxWarning) + with self.assertRaises(SyntaxError) as cm: + eval("'\\e' $") + exc = cm.exception + self.assertEqual(len(w), 1) + self.assertEqual(w[0].category, SyntaxWarning) + self.assertRegex(str(w[0].message), 'invalid escape sequence') + self.assertEqual(w[0].filename, '') + def test_eval_str_invalid_octal_escape(self): for i in range(0o400, 0o1000): with self.assertWarns(DeprecationWarning): diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-10-27-11-51-40.gh-issue-111380.vgSbir.rst b/Misc/NEWS.d/next/Core and Builtins/2023-10-27-11-51-40.gh-issue-111380.vgSbir.rst new file mode 100644 index 00000000000000..4ce6398dbfe3b1 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-10-27-11-51-40.gh-issue-111380.vgSbir.rst @@ -0,0 +1,2 @@ +Fix a bug that was causing :exc:`SyntaxWarning` to appear twice when parsing +if invalid syntax is encountered later. Patch by Pablo galindo diff --git a/Parser/string_parser.c b/Parser/string_parser.c index fb2b9808af15a2..7079b82d04f8ec 100644 --- a/Parser/string_parser.c +++ b/Parser/string_parser.c @@ -11,6 +11,11 @@ static int warn_invalid_escape_sequence(Parser *p, const char *first_invalid_escape, Token *t) { + if (p->call_invalid_rules) { + // Do not report warnings if we are in the second pass of the parser + // to avoid showing the warning twice. + return 0; + } unsigned char c = *first_invalid_escape; int octal = ('4' <= c && c <= '7'); PyObject *msg = From 945040a6e413ca180ae9ee19d713a77aa32f8d28 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Mon, 30 Oct 2023 19:54:37 +0000 Subject: [PATCH 2/3] Update Lib/test/test_string_literals.py --- Lib/test/test_string_literals.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_string_literals.py b/Lib/test/test_string_literals.py index 409d95e2c1ffe7..c5497139d07a7e 100644 --- a/Lib/test/test_string_literals.py +++ b/Lib/test/test_string_literals.py @@ -134,7 +134,7 @@ def test_eval_str_invalid_escape(self): # Check that the warning is raised ony once if there are syntax errors with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always', category=SyntaxWarning) + warnings.simplefilter('always', category=DeprecationWarning) with self.assertRaises(SyntaxError) as cm: eval("'\\e' $") exc = cm.exception From 7ae6044b699d78efd6e1fb3036f2e584ccc73576 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Tue, 31 Oct 2023 13:03:15 +0000 Subject: [PATCH 3/3] Update Lib/test/test_string_literals.py --- Lib/test/test_string_literals.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_string_literals.py b/Lib/test/test_string_literals.py index c5497139d07a7e..aeec703d5f5abd 100644 --- a/Lib/test/test_string_literals.py +++ b/Lib/test/test_string_literals.py @@ -139,7 +139,7 @@ def test_eval_str_invalid_escape(self): eval("'\\e' $") exc = cm.exception self.assertEqual(len(w), 1) - self.assertEqual(w[0].category, SyntaxWarning) + self.assertEqual(w[0].category, DeprecationWarning) self.assertRegex(str(w[0].message), 'invalid escape sequence') self.assertEqual(w[0].filename, '')