@@ -1662,22 +1662,21 @@ class WeirdDict(dict):
16621662 self .assertRaises (NameError , ns ['foo' ])
16631663
16641664 def test_compile_warnings (self ):
1665- # See gh-131927
1666- # Compile warnings originating from the same file and
1667- # line are now only emitted once.
1665+ # Each invocation of compile() emits compiler warnings, even if they
1666+ # have the same message and line number.
1667+ source = textwrap .dedent (r"""
1668+ # tokenizer
1669+ 1or 0 # line 3
1670+ # code generator
1671+ 1 is 1 # line 5
1672+ """ )
16681673 with warnings .catch_warnings (record = True ) as caught :
16691674 warnings .simplefilter ("default" )
1670- compile ('1 is 1' , '<stdin>' , 'eval' )
1671- compile ('1 is 1' , '<stdin>' , 'eval' )
1672-
1673- self .assertEqual (len (caught ), 1 )
1675+ for i in range (2 ):
1676+ # Even if compile() is at the same line.
1677+ compile (source , '<stdin>' , 'exec' )
16741678
1675- with warnings .catch_warnings (record = True ) as caught :
1676- warnings .simplefilter ("always" )
1677- compile ('1 is 1' , '<stdin>' , 'eval' )
1678- compile ('1 is 1' , '<stdin>' , 'eval' )
1679-
1680- self .assertEqual (len (caught ), 2 )
1679+ self .assertEqual ([wm .lineno for wm in caught ], [3 , 5 ] * 2 )
16811680
16821681 def test_compile_warning_in_finally (self ):
16831682 # Ensure that warnings inside finally blocks are
@@ -1688,16 +1687,47 @@ def test_compile_warning_in_finally(self):
16881687 try:
16891688 pass
16901689 finally:
1691- 1 is 1
1690+ 1 is 1 # line 5
1691+ try:
1692+ pass
1693+ finally: # nested
1694+ 1 is 1 # line 9
16921695 """ )
16931696
16941697 with warnings .catch_warnings (record = True ) as caught :
1695- warnings .simplefilter ("default " )
1698+ warnings .simplefilter ("always " )
16961699 compile (source , '<stdin>' , 'exec' )
16971700
1698- self .assertEqual (len (caught ), 1 )
1699- self .assertEqual (caught [0 ].category , SyntaxWarning )
1700- self .assertIn ("\" is\" with 'int' literal" , str (caught [0 ].message ))
1701+ self .assertEqual (sorted (wm .lineno for wm in caught ), [5 , 9 ])
1702+ for wm in caught :
1703+ self .assertEqual (wm .category , SyntaxWarning )
1704+ self .assertIn ("\" is\" with 'int' literal" , str (wm .message ))
1705+
1706+ # Other code path is used for "try" with "except*".
1707+ source = textwrap .dedent ("""
1708+ try:
1709+ pass
1710+ except *Exception:
1711+ pass
1712+ finally:
1713+ 1 is 1 # line 7
1714+ try:
1715+ pass
1716+ except *Exception:
1717+ pass
1718+ finally: # nested
1719+ 1 is 1 # line 13
1720+ """ )
1721+
1722+ with warnings .catch_warnings (record = True ) as caught :
1723+ warnings .simplefilter ("always" )
1724+ compile (source , '<stdin>' , 'exec' )
1725+
1726+ self .assertEqual (sorted (wm .lineno for wm in caught ), [7 , 13 ])
1727+ for wm in caught :
1728+ self .assertEqual (wm .category , SyntaxWarning )
1729+ self .assertIn ("\" is\" with 'int' literal" , str (wm .message ))
1730+
17011731
17021732class TestBooleanExpression (unittest .TestCase ):
17031733 class Value :
0 commit comments