Skip to content

Commit 5f1eaff

Browse files
authored
[3.13] gh-132435: Test syntax warnings in a finally block (GH-132436) (GH-132503)
(cherry picked from commit 887eabc)
1 parent ff9198d commit 5f1eaff

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
@@ -1530,6 +1530,26 @@ def test_compile_warnings(self):
15301530

15311531
self.assertEqual(len(caught), 2)
15321532

1533+
def test_compile_warning_in_finally(self):
1534+
# Ensure that warnings inside finally blocks are
1535+
# only emitted once despite the block being
1536+
# compiled twice (for normal execution and for
1537+
# exception handling).
1538+
source = textwrap.dedent("""
1539+
try:
1540+
pass
1541+
finally:
1542+
1 is 1
1543+
""")
1544+
1545+
with warnings.catch_warnings(record=True) as caught:
1546+
warnings.simplefilter("default")
1547+
compile(source, '<stdin>', 'exec')
1548+
1549+
self.assertEqual(len(caught), 1)
1550+
self.assertEqual(caught[0].category, SyntaxWarning)
1551+
self.assertIn("\"is\" with 'int' literal", str(caught[0].message))
1552+
15331553
@requires_debug_ranges()
15341554
class TestSourcePositions(unittest.TestCase):
15351555
# Ensure that compiled code snippets have correct line and column numbers
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)