From 06b5bfea2c93eed40c5fbe8b24897ec97eeaa7e5 Mon Sep 17 00:00:00 2001 From: Cheryl Sabella Date: Thu, 28 May 2020 09:56:38 -0400 Subject: [PATCH 1/3] bpo-40807: Show warnings once from codeop._maybe_compile --- Lib/codeop.py | 10 ++++++++-- Lib/test/test_codeop.py | 5 +++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Lib/codeop.py b/Lib/codeop.py index 835e68c09ba272..ca021590af6866 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] @@ -79,17 +80,22 @@ def _maybe_compile(compiler, source, filename, symbol): code = code1 = code2 = None try: + # Capture warnings only on the first call to avoid duplication. code = compiler(source, filename, symbol) except SyntaxError: pass try: - code1 = compiler(source + "\n", filename, symbol) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + code1 = compiler(source + "\n", filename, symbol) except SyntaxError as e: err1 = e try: - code2 = compiler(source + "\n\n", filename, symbol) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + code2 = compiler(source + "\n\n", filename, symbol) except SyntaxError as e: err2 = e diff --git a/Lib/test/test_codeop.py b/Lib/test/test_codeop.py index 0c5e362feea0ca..45cb1a7b74e903 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() From 8530b381c7072fa22daf86799d56391154ef2713 Mon Sep 17 00:00:00 2001 From: Cheryl Sabella Date: Sat, 30 May 2020 05:31:36 -0400 Subject: [PATCH 2/3] Move catch_warnings --- Lib/codeop.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/Lib/codeop.py b/Lib/codeop.py index ca021590af6866..7e192ea6a10a03 100644 --- a/Lib/codeop.py +++ b/Lib/codeop.py @@ -80,24 +80,22 @@ def _maybe_compile(compiler, source, filename, symbol): code = code1 = code2 = None try: - # Capture warnings only on the first call to avoid duplication. code = compiler(source, filename, symbol) except SyntaxError: pass - try: - with warnings.catch_warnings(): - warnings.simplefilter("ignore") + # 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 + except SyntaxError as e: + err1 = e - try: - with warnings.catch_warnings(): - warnings.simplefilter("ignore") + try: code2 = compiler(source + "\n\n", filename, symbol) - except SyntaxError as e: - err2 = e + except SyntaxError as e: + err2 = e try: if code: From d74883e371d0d2c304b371c9065d975167d124af Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Thu, 4 Jun 2020 16:25:36 -0400 Subject: [PATCH 3/3] news --- .../next/Library/2020-06-04-16-25-15.bpo-40807.yYyLWx.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-06-04-16-25-15.bpo-40807.yYyLWx.rst 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 00000000000000..532b809b77eed3 --- /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.