]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Add quotes to code to be a string
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 9 Jun 2020 12:39:01 +0000 (05:39 -0700)
committerGitHub <noreply@github.com>
Tue, 9 Jun 2020 12:39:01 +0000 (05:39 -0700)
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 3c37f35eb025062920b83dde1cabe0132b9cc6a7..3c2bb6083561eb704ec6ee85d458adc177ca8bf7 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 as err:
         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 4d52d15fa0fb3635c550384a776b2e2461584901..8e278b9b2311e4f0aa788fb9e29b8968dd5d70df 100644 (file)
@@ -294,6 +294,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((".*invalid", DeprecationWarning)) as w:
+            compile_command("'\e'")
+            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.