From 946788f6eeeef12289d06ebe99d3f26c3763daf2 Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Sat, 12 Apr 2025 13:44:17 +0200 Subject: [PATCH 1/3] Test syntax warnings in a finally block --- Lib/test/test_compile.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index 0377b3f954f44b..0f323792f667d4 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -1664,6 +1664,24 @@ 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, '', 'exec') + + self.assertEqual(len(caught), 1) + class TestBooleanExpression(unittest.TestCase): class Value: def __init__(self): From 6b0c13cb877b4e06eb1873443e05c140450cf052 Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Sat, 12 Apr 2025 23:25:48 +0200 Subject: [PATCH 2/3] Improve test --- Lib/test/test_compile.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index 0f323792f667d4..57e5f29b015637 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -1681,6 +1681,8 @@ def test_compile_warning_in_finally(self): 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: From b3db7ed2b87eb076c882b2f98b0b9911c6f79c3e Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Sun, 13 Apr 2025 10:35:47 +0200 Subject: [PATCH 3/3] Add news entry --- .../2025-04-13-10-34-27.gh-issue-131927.otp80n.rst | 3 +++ 1 file changed, 3 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/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 00000000000000..9aa940a10daa69 --- /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.