[Python-checkins] bpo-40807: Show warnings once from codeop._maybe_compile (#20486)

Cheryl Sabella webhook-mailer at python.org
Thu Jun 4 19:40:29 EDT 2020


https://github.com/python/cpython/commit/052d3fc0907be253cfd64b2c737a0b0aca586011
commit: 052d3fc0907be253cfd64b2c737a0b0aca586011
branch: master
author: Cheryl Sabella <cheryl.sabella at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-06-04T19:40:24-04:00
summary:

bpo-40807: Show warnings once from codeop._maybe_compile (#20486)

* bpo-40807: Show warnings once from codeop._maybe_compile

* Move catch_warnings

* news

Co-authored-by: Terry Jan Reedy <tjreedy at udel.edu>

files:
A Misc/NEWS.d/next/Library/2020-06-04-16-25-15.bpo-40807.yYyLWx.rst
M Lib/codeop.py
M Lib/test/test_codeop.py

diff --git a/Lib/codeop.py b/Lib/codeop.py
index 835e68c09ba27..7e192ea6a10a0 100644
--- a/Lib/codeop.py
+++ b/Lib/codeop.py
@@ -57,6 +57,7 @@
 """
 
 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:
diff --git a/Lib/test/test_codeop.py b/Lib/test/test_codeop.py
index 0c5e362feea0c..45cb1a7b74e90 100644
--- a/Lib/test/test_codeop.py
+++ b/Lib/test/test_codeop.py
@@ -303,6 +303,11 @@ def test_filename(self):
         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
index 0000000000000..532b809b77eed
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-06-04-16-25-15.bpo-40807.yYyLWx.rst
@@ -0,0 +1,2 @@
+Stop codeop._maybe_compile, used by code.InteractiveInterpreter (and IDLE).
+from from emitting each warning three times.



More information about the Python-checkins mailing list