Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
bpo-40807: Show warnings once from codeop._maybe_compile
  • Loading branch information
csabella committed May 28, 2020
commit 06b5bfea2c93eed40c5fbe8b24897ec97eeaa7e5
10 changes: 8 additions & 2 deletions Lib/codeop.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"""

import __future__
import warnings

_features = [getattr(__future__, fname)
for fname in __future__.all_feature_names]
Expand All @@ -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.
Comment thread
csabella marked this conversation as resolved.
Outdated
Comment thread
csabella marked this conversation as resolved.
Outdated
code = compiler(source, filename, symbol)
except SyntaxError:
pass

try:
code1 = compiler(source + "\n", filename, symbol)
with warnings.catch_warnings():
Comment thread
csabella marked this conversation as resolved.
Outdated
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

Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_codeop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()