Skip to content

Commit afff51f

Browse files
bpo-41520: codeop no longer ignores SyntaxWarning (GH-21838)
(cherry picked from commit 369a1cb) Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 622d90f commit afff51f

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

Lib/codeop.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ def _maybe_compile(compiler, source, filename, symbol):
8484
except SyntaxError as err:
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

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Nick Mathewson
44
"""
55
import unittest
6+
import warnings
67
from test import support
78

89
from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT
@@ -300,5 +301,11 @@ def test_warning(self):
300301
compile_command("0 is 0")
301302
self.assertEqual(len(w.warnings), 1)
302303

304+
# bpo-41520: check SyntaxWarning treated as an SyntaxError
305+
with self.assertRaises(SyntaxError):
306+
warnings.simplefilter('error', SyntaxWarning)
307+
compile_command('1 is 1\n', symbol='exec')
308+
309+
303310
if __name__ == "__main__":
304311
unittest.main()
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)