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

(cherry picked from commit 887eabc5a7)
This commit is contained in:
Tomas R. 2025-04-14 09:21:36 +02:00 committed by GitHub
parent ff9198dab3
commit 5f1eaff5b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 23 additions and 0 deletions

View file

@ -1530,6 +1530,26 @@ class TestSpecifics(unittest.TestCase):
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

View file

@ -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.