]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40807: Show warnings once from codeop._maybe_compile (GH-20486)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 4 Jun 2020 23:59:44 +0000 (16:59 -0700)
committerGitHub <noreply@github.com>
Thu, 4 Jun 2020 23:59:44 +0000 (16:59 -0700)
* bpo-40807: Show warnings once from codeop._maybe_compile

* Move catch_warnings

* news

Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
(cherry picked from commit 052d3fc0907be253cfd64b2c737a0b0aca586011)

Co-authored-by: Cheryl Sabella <cheryl.sabella@gmail.com>
Lib/codeop.py
Lib/test/test_codeop.py
Misc/NEWS.d/next/Library/2020-06-04-16-25-15.bpo-40807.yYyLWx.rst [new file with mode: 0644]

index 835e68c09ba2725ba02da9568cf14d639501e62b..7e192ea6a10a03af56f62ef4b2157505a58f4028 100644 (file)
@@ -57,6 +57,7 @@ Compile():
 """
 
 import __future__
+import warnings
 
 _features = [getattr(__future__, fname)
              for fname in __future__.all_feature_names]
@@ -83,15 +84,18 @@ def _maybe_compile(compiler, source, filename, symbol):
     except SyntaxError:
         pass
 
-    try:
-        code1 = compiler(source + "\n", filename, symbol)
-    except SyntaxError as e:
-        err1 = e
-
-    try:
-        code2 = compiler(source + "\n\n", filename, symbol)
-    except SyntaxError as e:
-        err2 = e
+    # Suppress warnings after the first compile to avoid duplication.
+    with warnings.catch_warnings():
+        warnings.simplefilter("ignore")
+        try:
+            code1 = compiler(source + "\n", filename, symbol)
+        except SyntaxError as e:
+            err1 = e
+
+        try:
+            code2 = compiler(source + "\n\n", filename, symbol)
+        except SyntaxError as e:
+            err2 = e
 
     try:
         if code:
index 0c5e362feea0ca13b3697c611d0fe7dbaafff7f9..45cb1a7b74e9036dcf95ba5ce7874adc83459e5a 100644 (file)
@@ -303,6 +303,11 @@ class CodeopTests(unittest.TestCase):
         self.assertNotEqual(compile_command("a = 1\n", "abc").co_filename,
                             compile("a = 1\n", "def", 'single').co_filename)
 
+    def test_warning(self):
+        # Test that the warning is only returned once.
+        with support.check_warnings((".*literal", SyntaxWarning)) as w:
+            compile_command("0 is 0")
+            self.assertEqual(len(w.warnings), 1)
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2020-06-04-16-25-15.bpo-40807.yYyLWx.rst b/Misc/NEWS.d/next/Library/2020-06-04-16-25-15.bpo-40807.yYyLWx.rst
new file mode 100644 (file)
index 0000000..532b809
--- /dev/null
@@ -0,0 +1,2 @@
+Stop codeop._maybe_compile, used by code.InteractiveInterpreter (and IDLE).
+from from emitting each warning three times.