https://github.com/python/cpython/commit/5f1eaff5b54c86be5d247d671645b8f1f0b42433
commit: 5f1eaff5b54c86be5d247d671645b8f1f0b42433
branch: 3.13
author: Tomas R. <tomas.ro...@gmail.com>
committer: serhiy-storchaka <storch...@gmail.com>
date: 2025-04-14T10:21:36+03:00
summary:

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

(cherry picked from commit 887eabc5a74316708460120d60d0fa4f8bdf5960)

files:
A Misc/NEWS.d/next/Core and 
Builtins/2025-04-13-10-34-27.gh-issue-131927.otp80n.rst
M Lib/test/test_compile.py

diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py
index b57adfadb5af5f..e4be26322930e0 100644
--- a/Lib/test/test_compile.py
+++ b/Lib/test/test_compile.py
@@ -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
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.

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to