@@ -1664,22 +1664,21 @@ class WeirdDict(dict):
16641664 self .assertRaises (NameError , ns ['foo' ])
16651665
16661666 def test_compile_warnings (self ):
1667- # See gh-131927
1668- # Compile warnings originating from the same file and
1669- # line are now only emitted once.
1667+ # Each invocation of compile() emits compiler warnings, even if they
1668+ # have the same message and line number.
1669+ source = textwrap .dedent (r"""
1670+ # tokenizer
1671+ 1or 0 # line 3
1672+ # code generator
1673+ 1 is 1 # line 5
1674+ """ )
16701675 with warnings .catch_warnings (record = True ) as caught :
16711676 warnings .simplefilter ("default" )
1672- compile ('1 is 1' , '<stdin>' , 'eval' )
1673- compile ('1 is 1' , '<stdin>' , 'eval' )
1674-
1675- self .assertEqual (len (caught ), 1 )
1677+ for i in range (2 ):
1678+ # Even if compile() is at the same line.
1679+ compile (source , '<stdin>' , 'exec' )
16761680
1677- with warnings .catch_warnings (record = True ) as caught :
1678- warnings .simplefilter ("always" )
1679- compile ('1 is 1' , '<stdin>' , 'eval' )
1680- compile ('1 is 1' , '<stdin>' , 'eval' )
1681-
1682- self .assertEqual (len (caught ), 2 )
1681+ self .assertEqual ([wm .lineno for wm in caught ], [3 , 5 ] * 2 )
16831682
16841683 def test_compile_warning_in_finally (self ):
16851684 # Ensure that warnings inside finally blocks are
@@ -1690,16 +1689,47 @@ def test_compile_warning_in_finally(self):
16901689 try:
16911690 pass
16921691 finally:
1693- 1 is 1
1692+ 1 is 1 # line 5
1693+ try:
1694+ pass
1695+ finally: # nested
1696+ 1 is 1 # line 9
16941697 """ )
16951698
16961699 with warnings .catch_warnings (record = True ) as caught :
1697- warnings .simplefilter ("default " )
1700+ warnings .simplefilter ("always " )
16981701 compile (source , '<stdin>' , 'exec' )
16991702
1700- self .assertEqual (len (caught ), 1 )
1701- self .assertEqual (caught [0 ].category , SyntaxWarning )
1702- self .assertIn ("\" is\" with 'int' literal" , str (caught [0 ].message ))
1703+ self .assertEqual (sorted (wm .lineno for wm in caught ), [5 , 9 ])
1704+ for wm in caught :
1705+ self .assertEqual (wm .category , SyntaxWarning )
1706+ self .assertIn ("\" is\" with 'int' literal" , str (wm .message ))
1707+
1708+ # Other code path is used for "try" with "except*".
1709+ source = textwrap .dedent ("""
1710+ try:
1711+ pass
1712+ except *Exception:
1713+ pass
1714+ finally:
1715+ 1 is 1 # line 7
1716+ try:
1717+ pass
1718+ except *Exception:
1719+ pass
1720+ finally: # nested
1721+ 1 is 1 # line 13
1722+ """ )
1723+
1724+ with warnings .catch_warnings (record = True ) as caught :
1725+ warnings .simplefilter ("always" )
1726+ compile (source , '<stdin>' , 'exec' )
1727+
1728+ self .assertEqual (sorted (wm .lineno for wm in caught ), [7 , 13 ])
1729+ for wm in caught :
1730+ self .assertEqual (wm .category , SyntaxWarning )
1731+ self .assertIn ("\" is\" with 'int' literal" , str (wm .message ))
1732+
17031733
17041734class TestBooleanExpression (unittest .TestCase ):
17051735 class Value :
0 commit comments