Skip to content

Commit 887eabc

Browse files
authored
gh-132435: Test syntax warnings in a finally block (GH-132436)
1 parent fc7e4e7 commit 887eabc

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

Lib/test/test_compile.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,6 +1664,26 @@ def test_compile_warnings(self):
16641664

16651665
self.assertEqual(len(caught), 2)
16661666

1667+
def test_compile_warning_in_finally(self):
1668+
# Ensure that warnings inside finally blocks are
1669+
# only emitted once despite the block being
1670+
# compiled twice (for normal execution and for
1671+
# exception handling).
1672+
source = textwrap.dedent("""
1673+
try:
1674+
pass
1675+
finally:
1676+
1 is 1
1677+
""")
1678+
1679+
with warnings.catch_warnings(record=True) as caught:
1680+
warnings.simplefilter("default")
1681+
compile(source, '<stdin>', 'exec')
1682+
1683+
self.assertEqual(len(caught), 1)
1684+
self.assertEqual(caught[0].category, SyntaxWarning)
1685+
self.assertIn("\"is\" with 'int' literal", str(caught[0].message))
1686+
16671687
class TestBooleanExpression(unittest.TestCase):
16681688
class Value:
16691689
def __init__(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Compiler warnings originating from the same module and line number are now
2+
only emitted once, matching the behaviour of warnings emitted from user
3+
code. This can also be configured with :mod:`warnings` filters.

0 commit comments

Comments
 (0)