Skip to content

[3.13] gh-132435: Test syntax warnings in a finally block (GH-132436) #132503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
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
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.
Loading