Skip to content

Commit 369a1cb

Browse files
authored
bpo-41520: codeop no longer ignores SyntaxWarning (GH-21838)
1 parent 0e95bbf commit 369a1cb

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

Lib/codeop.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ def _maybe_compile(compiler, source, filename, symbol):
8484
except SyntaxError:
8585
pass
8686

87-
# Suppress warnings after the first compile to avoid duplication.
87+
# Catch syntax warnings after the first compile
88+
# to emit SyntaxWarning at most once.
8889
with warnings.catch_warnings():
89-
warnings.simplefilter("ignore")
90+
warnings.simplefilter("error", SyntaxWarning)
91+
9092
try:
9193
code1 = compiler(source + "\n", filename, symbol)
9294
except SyntaxError as e:

Lib/test/test_codeop.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55
import sys
66
import unittest
7+
import warnings
78
from test import support
89
from test.support import warnings_helper
910

@@ -310,5 +311,11 @@ def test_warning(self):
310311
compile_command("0 is 0")
311312
self.assertEqual(len(w.warnings), 1)
312313

314+
# bpo-41520: check SyntaxWarning treated as an SyntaxError
315+
with self.assertRaises(SyntaxError):
316+
warnings.simplefilter('error', SyntaxWarning)
317+
compile_command('1 is 1\n', symbol='exec')
318+
319+
313320
if __name__ == "__main__":
314321
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :mod:`codeop` regression: it no longer ignores :exc:`SyntaxWarning`.

0 commit comments

Comments
 (0)