gh-139640: Fix swallowing syntax warnings in different modules (GH-13… · python/cpython@279db6b

GitHub

@@ -1679,22 +1679,21 @@ class WeirdDict(dict):

16791679self.assertRaises(NameError, ns['foo'])

1680168016811681deftest_compile_warnings(self):

1682-# See gh-131927

1683-# Compile warnings originating from the same file and

1684-# line are now only emitted once.

1682+# Each invocation of compile() emits compiler warnings, even if they

1683+# have the same message and line number.

1684+source=textwrap.dedent(r"""

1685+ # tokenizer

1686+ 1or 0 # line 3

1687+ # code generator

1688+ 1 is 1 # line 5

1689+ """)

16851690withwarnings.catch_warnings(record=True) ascaught:

16861691warnings.simplefilter("default")

1687-compile('1 is 1', '<stdin>', 'eval')

1688-compile('1 is 1', '<stdin>', 'eval')

1689-1690-self.assertEqual(len(caught), 1)

1692+foriinrange(2):

1693+# Even if compile() is at the same line.

1694+compile(source, '<stdin>', 'exec')

169116951692-withwarnings.catch_warnings(record=True) ascaught:

1693-warnings.simplefilter("always")

1694-compile('1 is 1', '<stdin>', 'eval')

1695-compile('1 is 1', '<stdin>', 'eval')

1696-1697-self.assertEqual(len(caught), 2)

1696+self.assertEqual([wm.linenoforwmincaught], [3, 5] *2)

1698169716991698deftest_compile_warning_in_finally(self):

17001699# Ensure that warnings inside finally blocks are

@@ -1705,16 +1704,47 @@ def test_compile_warning_in_finally(self):

17051704 try:

17061705 pass

17071706 finally:

1708- 1 is 1

1707+ 1 is 1 # line 5

1708+ try:

1709+ pass

1710+ finally: # nested

1711+ 1 is 1 # line 9

17091712 """)

1710171317111714withwarnings.catch_warnings(record=True) ascaught:

1712-warnings.simplefilter("default")

1715+warnings.simplefilter("always")

17131716compile(source, '<stdin>', 'exec')

171417171715-self.assertEqual(len(caught), 1)

1716-self.assertEqual(caught[0].category, SyntaxWarning)

1717-self.assertIn("\"is\" with 'int' literal", str(caught[0].message))

1718+self.assertEqual(sorted(wm.linenoforwmincaught), [5, 9])

1719+forwmincaught:

1720+self.assertEqual(wm.category, SyntaxWarning)

1721+self.assertIn("\"is\" with 'int' literal", str(wm.message))

1722+1723+# Other code path is used for "try" with "except*".

1724+source=textwrap.dedent("""

1725+ try:

1726+ pass

1727+ except *Exception:

1728+ pass

1729+ finally:

1730+ 1 is 1 # line 7

1731+ try:

1732+ pass

1733+ except *Exception:

1734+ pass

1735+ finally: # nested

1736+ 1 is 1 # line 13

1737+ """)

1738+1739+withwarnings.catch_warnings(record=True) ascaught:

1740+warnings.simplefilter("always")

1741+compile(source, '<stdin>', 'exec')

1742+1743+self.assertEqual(sorted(wm.linenoforwmincaught), [7, 13])

1744+forwmincaught:

1745+self.assertEqual(wm.category, SyntaxWarning)

1746+self.assertIn("\"is\" with 'int' literal", str(wm.message))

1747+1718174817191749classTestBooleanExpression(unittest.TestCase):

17201750classValue: