@@ -1679,22 +1679,21 @@ class WeirdDict(dict):
16791679 self .assertRaises (NameError , ns ['foo' ])
16801680
16811681 def test_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+ """ )
16851690 with warnings .catch_warnings (record = True ) as caught :
16861691 warnings .simplefilter ("default" )
1687- compile ('1 is 1' , '<stdin>' , 'eval' )
1688- compile ('1 is 1' , '<stdin>' , 'eval' )
1689-
1690- self .assertEqual (len (caught ), 1 )
1692+ for i in range (2 ):
1693+ # Even if compile() is at the same line.
1694+ compile (source , '<stdin>' , 'exec' )
16911695
1692- with warnings .catch_warnings (record = True ) as caught :
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 .lineno for wm in caught ], [3 , 5 ] * 2 )
16981697
16991698 def test_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 """ )
17101713
17111714 with warnings .catch_warnings (record = True ) as caught :
1712- warnings .simplefilter ("default " )
1715+ warnings .simplefilter ("always " )
17131716 compile (source , '<stdin>' , 'exec' )
17141717
1715- 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 .lineno for wm in caught ), [5 , 9 ])
1719+ for wm in caught :
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+ with warnings .catch_warnings (record = True ) as caught :
1740+ warnings .simplefilter ("always" )
1741+ compile (source , '<stdin>' , 'exec' )
1742+
1743+ self .assertEqual (sorted (wm .lineno for wm in caught ), [7 , 13 ])
1744+ for wm in caught :
1745+ self .assertEqual (wm .category , SyntaxWarning )
1746+ self .assertIn ("\" is\" with 'int' literal" , str (wm .message ))
1747+
17181748
17191749class TestBooleanExpression (unittest .TestCase ):
17201750 class Value :
0 commit comments