@@ -1528,22 +1528,21 @@ async def name_4():
15281528 [[]]
15291529
15301530 def test_compile_warnings (self ):
1531- # See gh-131927
1532- # Compile warnings originating from the same file and
1533- # line are now only emitted once.
1531+ # Each invocation of compile() emits compiler warnings, even if they
1532+ # have the same message and line number.
1533+ source = textwrap .dedent (r"""
1534+ # tokenizer
1535+ 1or 0 # line 3
1536+ # code generator
1537+ 1 is 1 # line 5
1538+ """ )
15341539 with warnings .catch_warnings (record = True ) as caught :
15351540 warnings .simplefilter ("default" )
1536- compile ('1 is 1' , '<stdin>' , 'eval' )
1537- compile ('1 is 1' , '<stdin>' , 'eval' )
1538-
1539- self .assertEqual (len (caught ), 1 )
1541+ for i in range (2 ):
1542+ # Even if compile() is at the same line.
1543+ compile (source , '<stdin>' , 'exec' )
15401544
1541- with warnings .catch_warnings (record = True ) as caught :
1542- warnings .simplefilter ("always" )
1543- compile ('1 is 1' , '<stdin>' , 'eval' )
1544- compile ('1 is 1' , '<stdin>' , 'eval' )
1545-
1546- self .assertEqual (len (caught ), 2 )
1545+ self .assertEqual ([wm .lineno for wm in caught ], [3 , 5 ] * 2 )
15471546
15481547 def test_compile_warning_in_finally (self ):
15491548 # Ensure that warnings inside finally blocks are
@@ -1554,16 +1553,47 @@ def test_compile_warning_in_finally(self):
15541553 try:
15551554 pass
15561555 finally:
1557- 1 is 1
1556+ 1 is 1 # line 5
1557+ try:
1558+ pass
1559+ finally: # nested
1560+ 1 is 1 # line 9
15581561 """ )
15591562
15601563 with warnings .catch_warnings (record = True ) as caught :
1561- warnings .simplefilter ("default " )
1564+ warnings .simplefilter ("always " )
15621565 compile (source , '<stdin>' , 'exec' )
15631566
1564- self .assertEqual (len (caught ), 1 )
1565- self .assertEqual (caught [0 ].category , SyntaxWarning )
1566- self .assertIn ("\" is\" with 'int' literal" , str (caught [0 ].message ))
1567+ self .assertEqual (sorted (wm .lineno for wm in caught ), [5 , 9 ])
1568+ for wm in caught :
1569+ self .assertEqual (wm .category , SyntaxWarning )
1570+ self .assertIn ("\" is\" with 'int' literal" , str (wm .message ))
1571+
1572+ # Other code path is used for "try" with "except*".
1573+ source = textwrap .dedent ("""
1574+ try:
1575+ pass
1576+ except *Exception:
1577+ pass
1578+ finally:
1579+ 1 is 1 # line 7
1580+ try:
1581+ pass
1582+ except *Exception:
1583+ pass
1584+ finally: # nested
1585+ 1 is 1 # line 13
1586+ """ )
1587+
1588+ with warnings .catch_warnings (record = True ) as caught :
1589+ warnings .simplefilter ("always" )
1590+ compile (source , '<stdin>' , 'exec' )
1591+
1592+ self .assertEqual (sorted (wm .lineno for wm in caught ), [7 , 13 ])
1593+ for wm in caught :
1594+ self .assertEqual (wm .category , SyntaxWarning )
1595+ self .assertIn ("\" is\" with 'int' literal" , str (wm .message ))
1596+
15671597
15681598@requires_debug_ranges ()
15691599class TestSourcePositions (unittest .TestCase ):
0 commit comments