From 887eabc5a74316708460120d60d0fa4f8bdf5960 Mon Sep 17 00:00:00 2001 From: "Tomas R." Date: Sun, 13 Apr 2025 22:44:00 +0200 Subject: gh-132435: Test syntax warnings in a finally block (GH-132436) --- Lib/test/test_compile.py | 20 ++++++++++++++++++++ .../2025-04-13-10-34-27.gh-issue-131927.otp80n.rst | 3 +++ 2 files changed, 23 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-04-13-10-34-27.gh-issue-131927.otp80n.rst diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index 0377b3f..57e5f29 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -1664,6 +1664,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, '', 'exec') + + self.assertEqual(len(caught), 1) + self.assertEqual(caught[0].category, SyntaxWarning) + self.assertIn("\"is\" with 'int' literal", str(caught[0].message)) + class TestBooleanExpression(unittest.TestCase): class Value: def __init__(self): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-04-13-10-34-27.gh-issue-131927.otp80n.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-13-10-34-27.gh-issue-131927.otp80n.rst new file mode 100644 index 0000000..9aa940a --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-13-10-34-27.gh-issue-131927.otp80n.rst @@ -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. -- cgit v0.12