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@udel.edu>
This commit is contained in:
Cheryl Sabella 2020-06-04 19:40:24 -04:00 committed by GitHub
parent 3744ed2c9c
commit 052d3fc090
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 8 deletions

View File

@ -57,6 +57,7 @@ Compile():
""" """
import __future__ import __future__
import warnings
_features = [getattr(__future__, fname) _features = [getattr(__future__, fname)
for fname in __future__.all_feature_names] for fname in __future__.all_feature_names]
@ -83,15 +84,18 @@ def _maybe_compile(compiler, source, filename, symbol):
except SyntaxError: except SyntaxError:
pass pass
try: # Suppress warnings after the first compile to avoid duplication.
code1 = compiler(source + "\n", filename, symbol) with warnings.catch_warnings():
except SyntaxError as e: warnings.simplefilter("ignore")
err1 = e try:
code1 = compiler(source + "\n", filename, symbol)
except SyntaxError as e:
err1 = e
try: try:
code2 = compiler(source + "\n\n", filename, symbol) code2 = compiler(source + "\n\n", filename, symbol)
except SyntaxError as e: except SyntaxError as e:
err2 = e err2 = e
try: try:
if code: if code:

View File

@ -303,6 +303,11 @@ class CodeopTests(unittest.TestCase):
self.assertNotEqual(compile_command("a = 1\n", "abc").co_filename, self.assertNotEqual(compile_command("a = 1\n", "abc").co_filename,
compile("a = 1\n", "def", 'single').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__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -0,0 +1,2 @@
Stop codeop._maybe_compile, used by code.InteractiveInterpreter (and IDLE).
from from emitting each warning three times.