Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
22 changes: 13 additions & 9 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 @@ -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:
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 @@ -294,6 +294,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((".*invalid", DeprecationWarning)) as w:
compile_command("\e")

@terryjreedy terryjreedy Jun 9, 2020 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot that the outer quotes are not part of the compiled syntax. (I have done this too.) \e is a syntax error, '\e' is valid and gives no warning in 3.7.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that. I was actually having trouble getting the '\e' to show the Deprecation Warning on any release until I started the REPL or IDLE with the ./python -Wall. I did this change quickly just to see how the tests would be run and it looks like I did it too quickly. :-(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The important point is '\e' warns when starting IDLE (legitimately) with py -3.7 -Wall -m idlelib. So the backport is needed.

Should the tests always pass, not just with CI? At least if one does not explicitly turn off warnings for tests (which seems like a bad idea)? I made a local 3.7 branch from this PR and ran python -m test test_codeop and it passed normally. python -m test.test_codeop reports 5 tests rather than the previous 4 and prints the DeprecationWarning.

So I will merge and we will see how this fares on buildbots.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

20 seconds later I realize that warnings <= 1 is sufficient to prove not 3 or even 2. But 0 could also result from a bad patch that suppressed too much. So if a fix is needed, better to add a context forcing the warning on.

self.assertEqual(len(w.warnings), 1)

if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Stop codeop._maybe_compile, used by code.InteractiveInterpreter (and IDLE).
from from emitting each warning three times.