@@ -1664,22 +1664,21 @@ class WeirdDict(dict):
1664
1664
self .assertRaises (NameError , ns ['foo' ])
1665
1665
1666
1666
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
+ """ )
1670
1675
with warnings .catch_warnings (record = True ) as caught :
1671
1676
warnings .simplefilter ("default" )
1672
- compile ('1 is 1' , '<stdin>' , 'eval' )
1673
- compile ('1 is 1' , '<stdin>' , 'eval' )
1674
-
1675
- self .assertEqual (len (caught ), 1 )
1676
-
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' )
1677
+ for i in range (2 ):
1678
+ # Even if compile() is at the same line.
1679
+ compile (source , '<stdin>' , 'exec' )
1681
1680
1682
- self .assertEqual (len ( caught ), 2 )
1681
+ self .assertEqual ([ wm . lineno for wm in caught ], [ 3 , 5 ] * 2 )
1683
1682
1684
1683
def test_compile_warning_in_finally (self ):
1685
1684
# Ensure that warnings inside finally blocks are
@@ -1690,16 +1689,46 @@ def test_compile_warning_in_finally(self):
1690
1689
try:
1691
1690
pass
1692
1691
finally:
1693
- 1 is 1
1692
+ 1 is 1 # line 5
1693
+ try:
1694
+ pass
1695
+ finally: # nested
1696
+ 1 is 1 # line 9
1694
1697
""" )
1695
1698
1696
1699
with warnings .catch_warnings (record = True ) as caught :
1697
- warnings .simplefilter ("default" )
1700
+ warnings .simplefilter ("always" )
1701
+ compile (source , '<stdin>' , 'exec' )
1702
+
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" )
1698
1726
compile (source , '<stdin>' , 'exec' )
1699
1727
1700
- self .assertEqual (len (caught ), 1 )
1701
- self .assertEqual (caught [0 ].category , SyntaxWarning )
1702
- self .assertIn ("\" is\" with 'int' literal" , str (caught [0 ].message ))
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 ))
1703
1732
1704
1733
class TestBooleanExpression (unittest .TestCase ):
1705
1734
class Value :
0 commit comments