Skip to content

Commit 4b378ac

Browse files
Add quotes to code to be a string
1 parent 22faf6a commit 4b378ac

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

Lib/codeop.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"""
5858

5959
import __future__
60+
import warnings
6061

6162
_features = [getattr(__future__, fname)
6263
for fname in __future__.all_feature_names]
@@ -83,15 +84,18 @@ def _maybe_compile(compiler, source, filename, symbol):
8384
except SyntaxError as err:
8485
pass
8586

86-
try:
87-
code1 = compiler(source + "\n", filename, symbol)
88-
except SyntaxError as e:
89-
err1 = e
90-
91-
try:
92-
code2 = compiler(source + "\n\n", filename, symbol)
93-
except SyntaxError as e:
94-
err2 = e
87+
# Suppress warnings after the first compile to avoid duplication.
88+
with warnings.catch_warnings():
89+
warnings.simplefilter("ignore")
90+
try:
91+
code1 = compiler(source + "\n", filename, symbol)
92+
except SyntaxError as e:
93+
err1 = e
94+
95+
try:
96+
code2 = compiler(source + "\n\n", filename, symbol)
97+
except SyntaxError as e:
98+
err2 = e
9599

96100
try:
97101
if code:

Lib/test/test_codeop.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,11 @@ def test_filename(self):
294294
self.assertNotEqual(compile_command("a = 1\n", "abc").co_filename,
295295
compile("a = 1\n", "def", 'single').co_filename)
296296

297+
def test_warning(self):
298+
# Test that the warning is only returned once.
299+
with support.check_warnings((".*invalid", DeprecationWarning)) as w:
300+
compile_command("'\e'")
301+
self.assertEqual(len(w.warnings), 1)
297302

298303
if __name__ == "__main__":
299304
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Stop codeop._maybe_compile, used by code.InteractiveInterpreter (and IDLE).
2+
from from emitting each warning three times.

0 commit comments

Comments
 (0)