diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2020-06-06 01:24:45 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-06 01:24:45 -0700 |
commit | c067183605cf84bb1a246635f52827251d0476f8 (patch) | |
tree | 12cb0a05c0f35d7570f95f7f6c3232d2abf6d88a | |
parent | a5d6aba318ead9cc756ba750a70da41f5def3f8f (diff) | |
download | cpython-git-c067183605cf84bb1a246635f52827251d0476f8.tar.gz |
bpo-40807: Show warnings once from codeop._maybe_compile (GH-20486)
* 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>
-rw-r--r-- | Lib/codeop.py | 22 | ||||
-rw-r--r-- | Lib/test/test_codeop.py | 5 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2020-06-04-16-25-15.bpo-40807.yYyLWx.rst | 2 |
3 files changed, 20 insertions, 9 deletions
diff --git a/Lib/codeop.py b/Lib/codeop.py index 3c37f35eb0..3c2bb60835 100644 --- a/Lib/codeop.py +++ b/Lib/codeop.py @@ -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: diff --git a/Lib/test/test_codeop.py b/Lib/test/test_codeop.py index 4d52d15fa0..1e57ab9d51 100644 --- a/Lib/test/test_codeop.py +++ b/Lib/test/test_codeop.py @@ -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((".*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 0000000000..532b809b77 --- /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. |