Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[3.13] gh-132435: Test syntax warnings in a finally block (GH-132436)
(cherry picked from commit 887eabc)

Co-authored-by: Tomas R. <tomas.roun8@gmail.com>
  • Loading branch information
tomasr8 committed Apr 14, 2025
commit 7f65bda123beed4d2dde3347c7c13d3323dc4b1d
20 changes: 20 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,26 @@ def test_compile_warnings(self):

self.assertEqual(len(caught), 2)

def test_compile_warning_in_finally(self):
# Ensure that warnings inside finally blocks are
# only emitted once despite the block being
# compiled twice (for normal execution and for
# exception handling).
source = textwrap.dedent("""
try:
pass
finally:
1 is 1
""")

with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("default")
compile(source, '<stdin>', 'exec')

self.assertEqual(len(caught), 1)
self.assertEqual(caught[0].category, SyntaxWarning)
self.assertIn("\"is\" with 'int' literal", str(caught[0].message))

@requires_debug_ranges()
class TestSourcePositions(unittest.TestCase):
# Ensure that compiled code snippets have correct line and column numbers
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Compiler warnings originating from the same module and line number are now
only emitted once, matching the behaviour of warnings emitted from user
code. This can also be configured with :mod:`warnings` filters.