]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-111380: Show SyntaxWarnings only once when parsing if invalid syntax is...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 31 Oct 2023 13:29:42 +0000 (14:29 +0100)
committerGitHub <noreply@github.com>
Tue, 31 Oct 2023 13:29:42 +0000 (13:29 +0000)
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 <Pablogsal@gmail.com>
Lib/test/test_string_literals.py
Misc/NEWS.d/next/Core and Builtins/2023-10-27-11-51-40.gh-issue-111380.vgSbir.rst [new file with mode: 0644]
Parser/string_parser.c

index 7247b7e48bc2b6f67c3fd15f4047c49784477378..aeec703d5f5abdcd270dde665417224507c8034e 100644 (file)
@@ -131,6 +131,18 @@ class TestLiterals(unittest.TestCase):
         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=DeprecationWarning)
+            with self.assertRaises(SyntaxError) as cm:
+                eval("'\\e' $")
+            exc = cm.exception
+        self.assertEqual(len(w), 1)
+        self.assertEqual(w[0].category, DeprecationWarning)
+        self.assertRegex(str(w[0].message), 'invalid escape sequence')
+        self.assertEqual(w[0].filename, '<string>')
+
     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 (file)
index 0000000..4ce6398
--- /dev/null
@@ -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
index fb2b9808af15a2fb7ddd716bded5ba14a6a8e815..7079b82d04f8ec665c137a2b629bdbb0f52fc97b 100644 (file)
 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 =