[3.14] gh-139640: Fix swallowing syntax warnings in different modules (GH-139755) (GH-140117)

Revert GH-131993.

Fix swallowing some syntax warnings in different modules if they accidentally
have the same message and are emitted from the same line.

Fix duplicated warnings in the "finally" block.

(cherry picked from commit 279db6bede)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>

* Update 2025-10-06-10-03-37.gh-issue-139640.gY5oTb.rst

---------

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2025-10-14 17:56:01 +02:00 committed by GitHub
parent 99e42ab49c
commit af28efd8b4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 63 additions and 74 deletions

View file

@ -1662,22 +1662,21 @@ class WeirdDict(dict):
self.assertRaises(NameError, ns['foo'])
def test_compile_warnings(self):
# See gh-131927
# Compile warnings originating from the same file and
# line are now only emitted once.
# Each invocation of compile() emits compiler warnings, even if they
# have the same message and line number.
source = textwrap.dedent(r"""
# tokenizer
1or 0 # line 3
# code generator
1 is 1 # line 5
""")
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("default")
compile('1 is 1', '<stdin>', 'eval')
compile('1 is 1', '<stdin>', 'eval')
for i in range(2):
# Even if compile() is at the same line.
compile(source, '<stdin>', 'exec')
self.assertEqual(len(caught), 1)
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
compile('1 is 1', '<stdin>', 'eval')
compile('1 is 1', '<stdin>', 'eval')
self.assertEqual(len(caught), 2)
self.assertEqual([wm.lineno for wm in caught], [3, 5] * 2)
def test_compile_warning_in_finally(self):
# Ensure that warnings inside finally blocks are
@ -1688,16 +1687,47 @@ def test_compile_warning_in_finally(self):
try:
pass
finally:
1 is 1
1 is 1 # line 5
try:
pass
finally: # nested
1 is 1 # line 9
""")
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("default")
warnings.simplefilter("always")
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))
self.assertEqual(sorted(wm.lineno for wm in caught), [5, 9])
for wm in caught:
self.assertEqual(wm.category, SyntaxWarning)
self.assertIn("\"is\" with 'int' literal", str(wm.message))
# Other code path is used for "try" with "except*".
source = textwrap.dedent("""
try:
pass
except *Exception:
pass
finally:
1 is 1 # line 7
try:
pass
except *Exception:
pass
finally: # nested
1 is 1 # line 13
""")
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
compile(source, '<stdin>', 'exec')
self.assertEqual(sorted(wm.lineno for wm in caught), [7, 13])
for wm in caught:
self.assertEqual(wm.category, SyntaxWarning)
self.assertIn("\"is\" with 'int' literal", str(wm.message))
class TestBooleanExpression(unittest.TestCase):
class Value: