@@ -685,7 +685,6 @@ def f_with_multiline():
685
685
' ~~~~~~~~^^\n '
686
686
f' File "{ __file__ } ", line { lineno_f + 2 } , in f_with_multiline\n '
687
687
' return compile(code, "?", "exec")\n '
688
- ' ~~~~~~~^^^^^^^^^^^^^^^^^^^\n '
689
688
' File "?", line 7\n '
690
689
' foo(a, z\n '
691
690
' ^'
@@ -775,8 +774,8 @@ def f_with_binary_operator():
775
774
def test_caret_for_binary_operators_with_spaces_and_parenthesis (self ):
776
775
def f_with_binary_operator ():
777
776
a = 1
778
- b = ""
779
- return ( a ) + b
777
+ b = c = ""
778
+ return ( a ) + b + c
780
779
781
780
lineno_f = f_with_binary_operator .__code__ .co_firstlineno
782
781
expected_error = (
@@ -785,7 +784,7 @@ def f_with_binary_operator():
785
784
' callable()\n '
786
785
' ~~~~~~~~^^\n '
787
786
f' File "{ __file__ } ", line { lineno_f + 3 } , in f_with_binary_operator\n '
788
- ' return ( a ) +b\n '
787
+ ' return ( a ) +b + c \n '
789
788
' ~~~~~~~~~~^~\n '
790
789
)
791
790
result_lines = self .get_exception (f_with_binary_operator )
@@ -973,7 +972,7 @@ def f1(a):
973
972
def f2 (b ):
974
973
raise RuntimeError ("fail" )
975
974
return f2
976
- return f1 ("x" )("y" )
975
+ return f1 ("x" )("y" )( "z" )
977
976
978
977
lineno_f = f_with_call .__code__ .co_firstlineno
979
978
expected_error = (
@@ -982,7 +981,7 @@ def f2(b):
982
981
' callable()\n '
983
982
' ~~~~~~~~^^\n '
984
983
f' File "{ __file__ } ", line { lineno_f + 5 } , in f_with_call\n '
985
- ' return f1("x")("y")\n '
984
+ ' return f1("x")("y")("z") \n '
986
985
' ~~~~~~~^^^^^\n '
987
986
f' File "{ __file__ } ", line { lineno_f + 3 } , in f2\n '
988
987
' raise RuntimeError("fail")\n '
@@ -1497,6 +1496,184 @@ def f():
1497
1496
' raise MemoryError()' ]
1498
1497
self .assertEqual (actual , expected )
1499
1498
1499
+ def test_anchors_for_simple_return_statements_are_elided (self ):
1500
+ def g ():
1501
+ 1 / 0
1502
+
1503
+ def f ():
1504
+ return g ()
1505
+
1506
+ result_lines = self .get_exception (f )
1507
+ expected = ['Traceback (most recent call last):' ,
1508
+ f" File \" { __file__ } \" , line { self .callable_line } , in get_exception" ,
1509
+ " callable()" ,
1510
+ " ~~~~~~~~^^" ,
1511
+ f" File \" { __file__ } \" , line { f .__code__ .co_firstlineno + 1 } , in f" ,
1512
+ " return g()" ,
1513
+ f" File \" { __file__ } \" , line { g .__code__ .co_firstlineno + 1 } , in g" ,
1514
+ " 1/0" ,
1515
+ " ~^~"
1516
+ ]
1517
+ self .assertEqual (result_lines , expected )
1518
+
1519
+ def g ():
1520
+ 1 / 0
1521
+
1522
+ def f ():
1523
+ return g () + 1
1524
+
1525
+ result_lines = self .get_exception (f )
1526
+ expected = ['Traceback (most recent call last):' ,
1527
+ f" File \" { __file__ } \" , line { self .callable_line } , in get_exception" ,
1528
+ " callable()" ,
1529
+ " ~~~~~~~~^^" ,
1530
+ f" File \" { __file__ } \" , line { f .__code__ .co_firstlineno + 1 } , in f" ,
1531
+ " return g() + 1" ,
1532
+ " ~^^" ,
1533
+ f" File \" { __file__ } \" , line { g .__code__ .co_firstlineno + 1 } , in g" ,
1534
+ " 1/0" ,
1535
+ " ~^~"
1536
+ ]
1537
+ self .assertEqual (result_lines , expected )
1538
+
1539
+ def g (* args ):
1540
+ 1 / 0
1541
+
1542
+ def f ():
1543
+ return g (1 ,
1544
+ 2 , 4 ,
1545
+ 5 )
1546
+
1547
+ result_lines = self .get_exception (f )
1548
+ expected = ['Traceback (most recent call last):' ,
1549
+ f" File \" { __file__ } \" , line { self .callable_line } , in get_exception" ,
1550
+ " callable()" ,
1551
+ " ~~~~~~~~^^" ,
1552
+ f" File \" { __file__ } \" , line { f .__code__ .co_firstlineno + 1 } , in f" ,
1553
+ " return g(1," ,
1554
+ " 2, 4," ,
1555
+ " 5)" ,
1556
+ f" File \" { __file__ } \" , line { g .__code__ .co_firstlineno + 1 } , in g" ,
1557
+ " 1/0" ,
1558
+ " ~^~"
1559
+ ]
1560
+ self .assertEqual (result_lines , expected )
1561
+
1562
+ def g (* args ):
1563
+ 1 / 0
1564
+
1565
+ def f ():
1566
+ return g (1 ,
1567
+ 2 , 4 ,
1568
+ 5 ) + 1
1569
+
1570
+ result_lines = self .get_exception (f )
1571
+ expected = ['Traceback (most recent call last):' ,
1572
+ f" File \" { __file__ } \" , line { self .callable_line } , in get_exception" ,
1573
+ " callable()" ,
1574
+ " ~~~~~~~~^^" ,
1575
+ f" File \" { __file__ } \" , line { f .__code__ .co_firstlineno + 1 } , in f" ,
1576
+ " return g(1," ,
1577
+ " ~^^^" ,
1578
+ " 2, 4," ,
1579
+ " ^^^^^" ,
1580
+ " 5) + 1" ,
1581
+ " ^^" ,
1582
+ f" File \" { __file__ } \" , line { g .__code__ .co_firstlineno + 1 } , in g" ,
1583
+ " 1/0" ,
1584
+ " ~^~"
1585
+ ]
1586
+ self .assertEqual (result_lines , expected )
1587
+
1588
+ def test_anchors_for_simple_assign_statements_are_elided (self ):
1589
+ def g ():
1590
+ 1 / 0
1591
+
1592
+ def f ():
1593
+ x = g ()
1594
+
1595
+ result_lines = self .get_exception (f )
1596
+ expected = ['Traceback (most recent call last):' ,
1597
+ f" File \" { __file__ } \" , line { self .callable_line } , in get_exception" ,
1598
+ " callable()" ,
1599
+ " ~~~~~~~~^^" ,
1600
+ f" File \" { __file__ } \" , line { f .__code__ .co_firstlineno + 1 } , in f" ,
1601
+ " x = g()" ,
1602
+ f" File \" { __file__ } \" , line { g .__code__ .co_firstlineno + 1 } , in g" ,
1603
+ " 1/0" ,
1604
+ " ~^~"
1605
+ ]
1606
+ self .assertEqual (result_lines , expected )
1607
+
1608
+ def g (* args ):
1609
+ 1 / 0
1610
+
1611
+ def f ():
1612
+ x = g (1 ,
1613
+ 2 , 3 ,
1614
+ 4 )
1615
+
1616
+ result_lines = self .get_exception (f )
1617
+ expected = ['Traceback (most recent call last):' ,
1618
+ f" File \" { __file__ } \" , line { self .callable_line } , in get_exception" ,
1619
+ " callable()" ,
1620
+ " ~~~~~~~~^^" ,
1621
+ f" File \" { __file__ } \" , line { f .__code__ .co_firstlineno + 1 } , in f" ,
1622
+ " x = g(1," ,
1623
+ " 2, 3," ,
1624
+ " 4)" ,
1625
+ f" File \" { __file__ } \" , line { g .__code__ .co_firstlineno + 1 } , in g" ,
1626
+ " 1/0" ,
1627
+ " ~^~"
1628
+ ]
1629
+ self .assertEqual (result_lines , expected )
1630
+
1631
+ def g ():
1632
+ 1 / 0
1633
+
1634
+ def f ():
1635
+ x = y = g ()
1636
+
1637
+ result_lines = self .get_exception (f )
1638
+ expected = ['Traceback (most recent call last):' ,
1639
+ f" File \" { __file__ } \" , line { self .callable_line } , in get_exception" ,
1640
+ " callable()" ,
1641
+ " ~~~~~~~~^^" ,
1642
+ f" File \" { __file__ } \" , line { f .__code__ .co_firstlineno + 1 } , in f" ,
1643
+ " x = y = g()" ,
1644
+ " ~^^" ,
1645
+ f" File \" { __file__ } \" , line { g .__code__ .co_firstlineno + 1 } , in g" ,
1646
+ " 1/0" ,
1647
+ " ~^~"
1648
+ ]
1649
+ self .assertEqual (result_lines , expected )
1650
+
1651
+ def g (* args ):
1652
+ 1 / 0
1653
+
1654
+ def f ():
1655
+ x = y = g (1 ,
1656
+ 2 , 3 ,
1657
+ 4 )
1658
+
1659
+ result_lines = self .get_exception (f )
1660
+ expected = ['Traceback (most recent call last):' ,
1661
+ f" File \" { __file__ } \" , line { self .callable_line } , in get_exception" ,
1662
+ " callable()" ,
1663
+ " ~~~~~~~~^^" ,
1664
+ f" File \" { __file__ } \" , line { f .__code__ .co_firstlineno + 1 } , in f" ,
1665
+ " x = y = g(1," ,
1666
+ " ~^^^" ,
1667
+ " 2, 3," ,
1668
+ " ^^^^^" ,
1669
+ " 4)" ,
1670
+ " ^^" ,
1671
+ f" File \" { __file__ } \" , line { g .__code__ .co_firstlineno + 1 } , in g" ,
1672
+ " 1/0" ,
1673
+ " ~^~"
1674
+ ]
1675
+ self .assertEqual (result_lines , expected )
1676
+
1500
1677
1501
1678
@requires_debug_ranges ()
1502
1679
class PurePythonTracebackErrorCaretTests (
@@ -1691,7 +1868,7 @@ def f():
1691
1868
# Check a known (limited) number of recursive invocations
1692
1869
def g (count = 10 ):
1693
1870
if count :
1694
- return g (count - 1 )
1871
+ return g (count - 1 ) + 1
1695
1872
raise ValueError
1696
1873
1697
1874
with captured_output ("stderr" ) as stderr_g :
@@ -1705,13 +1882,13 @@ def g(count=10):
1705
1882
lineno_g = g .__code__ .co_firstlineno
1706
1883
result_g = (
1707
1884
f' File "{ __file__ } ", line { lineno_g + 2 } , in g\n '
1708
- ' return g(count-1)\n '
1885
+ ' return g(count-1) + 1 \n '
1709
1886
' ~^^^^^^^^^\n '
1710
1887
f' File "{ __file__ } ", line { lineno_g + 2 } , in g\n '
1711
- ' return g(count-1)\n '
1888
+ ' return g(count-1) + 1 \n '
1712
1889
' ~^^^^^^^^^\n '
1713
1890
f' File "{ __file__ } ", line { lineno_g + 2 } , in g\n '
1714
- ' return g(count-1)\n '
1891
+ ' return g(count-1) + 1 \n '
1715
1892
' ~^^^^^^^^^\n '
1716
1893
' [Previous line repeated 7 more times]\n '
1717
1894
f' File "{ __file__ } ", line { lineno_g + 3 } , in g\n '
@@ -1750,13 +1927,10 @@ def h(count=10):
1750
1927
' ~^^\n '
1751
1928
f' File "{ __file__ } ", line { lineno_h + 2 } , in h\n '
1752
1929
' return h(count-1)\n '
1753
- ' ~^^^^^^^^^\n '
1754
1930
f' File "{ __file__ } ", line { lineno_h + 2 } , in h\n '
1755
1931
' return h(count-1)\n '
1756
- ' ~^^^^^^^^^\n '
1757
1932
f' File "{ __file__ } ", line { lineno_h + 2 } , in h\n '
1758
1933
' return h(count-1)\n '
1759
- ' ~^^^^^^^^^\n '
1760
1934
' [Previous line repeated 7 more times]\n '
1761
1935
f' File "{ __file__ } ", line { lineno_h + 3 } , in h\n '
1762
1936
' g()\n '
@@ -1776,21 +1950,21 @@ def h(count=10):
1776
1950
self .fail ("no error raised" )
1777
1951
result_g = (
1778
1952
f' File "{ __file__ } ", line { lineno_g + 2 } , in g\n '
1779
- ' return g(count-1)\n '
1953
+ ' return g(count-1) + 1 \n '
1780
1954
' ~^^^^^^^^^\n '
1781
1955
f' File "{ __file__ } ", line { lineno_g + 2 } , in g\n '
1782
- ' return g(count-1)\n '
1956
+ ' return g(count-1) + 1 \n '
1783
1957
' ~^^^^^^^^^\n '
1784
1958
f' File "{ __file__ } ", line { lineno_g + 2 } , in g\n '
1785
- ' return g(count-1)\n '
1959
+ ' return g(count-1) + 1 \n '
1786
1960
' ~^^^^^^^^^\n '
1787
1961
f' File "{ __file__ } ", line { lineno_g + 3 } , in g\n '
1788
1962
' raise ValueError\n '
1789
1963
'ValueError\n '
1790
1964
)
1791
1965
tb_line = (
1792
1966
'Traceback (most recent call last):\n '
1793
- f' File "{ __file__ } ", line { lineno_g + 80 } , in _check_recursive_traceback_display\n '
1967
+ f' File "{ __file__ } ", line { lineno_g + 77 } , in _check_recursive_traceback_display\n '
1794
1968
' g(traceback._RECURSIVE_CUTOFF)\n '
1795
1969
' ~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n '
1796
1970
)
@@ -1808,13 +1982,13 @@ def h(count=10):
1808
1982
self .fail ("no error raised" )
1809
1983
result_g = (
1810
1984
f' File "{ __file__ } ", line { lineno_g + 2 } , in g\n '
1811
- ' return g(count-1)\n '
1985
+ ' return g(count-1) + 1 \n '
1812
1986
' ~^^^^^^^^^\n '
1813
1987
f' File "{ __file__ } ", line { lineno_g + 2 } , in g\n '
1814
- ' return g(count-1)\n '
1988
+ ' return g(count-1) + 1 \n '
1815
1989
' ~^^^^^^^^^\n '
1816
1990
f' File "{ __file__ } ", line { lineno_g + 2 } , in g\n '
1817
- ' return g(count-1)\n '
1991
+ ' return g(count-1) + 1 \n '
1818
1992
' ~^^^^^^^^^\n '
1819
1993
' [Previous line repeated 1 more time]\n '
1820
1994
f' File "{ __file__ } ", line { lineno_g + 3 } , in g\n '
@@ -1823,7 +1997,7 @@ def h(count=10):
1823
1997
)
1824
1998
tb_line = (
1825
1999
'Traceback (most recent call last):\n '
1826
- f' File "{ __file__ } ", line { lineno_g + 112 } , in _check_recursive_traceback_display\n '
2000
+ f' File "{ __file__ } ", line { lineno_g + 109 } , in _check_recursive_traceback_display\n '
1827
2001
' g(traceback._RECURSIVE_CUTOFF + 1)\n '
1828
2002
' ~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n '
1829
2003
)
@@ -4260,11 +4434,14 @@ def foo(*args):
4260
4434
x = {'a' :{'b' : None }}
4261
4435
y = x ['a' ]['b' ]['c' ]
4262
4436
4263
- def baz (* args ):
4264
- return foo (1 ,2 ,3 ,4 )
4437
+ def baz2 (* args ):
4438
+ return (lambda * args : foo (* args ))(1 ,2 ,3 ,4 )
4439
+
4440
+ def baz1 (* args ):
4441
+ return baz2 (1 ,2 ,3 ,4 )
4265
4442
4266
4443
def bar ():
4267
- return baz (1 ,
4444
+ return baz1 (1 ,
4268
4445
2 ,3
4269
4446
,4 )
4270
4447
try :
@@ -4278,10 +4455,10 @@ def bar():
4278
4455
boldr = traceback ._ANSIColors .BOLD_RED
4279
4456
reset = traceback ._ANSIColors .RESET
4280
4457
self .assertIn ("y = " + red + "x['a']['b']" + reset + boldr + "['c']" + reset , lines )
4281
- self .assertIn ("return " + red + "foo" + reset + boldr + "(1,2,3,4)" + reset , lines )
4282
- self .assertIn ("return " + red + "baz " + reset + boldr + "(1, " + reset , lines )
4283
- self .assertIn (boldr + " 2,3" + reset , lines )
4284
- self .assertIn (boldr + ", 4)" + reset , lines )
4458
+ self .assertIn ("return " + red + "(lambda *args: foo(*args)) " + reset + boldr + "(1,2,3,4)" + reset , lines )
4459
+ self .assertIn ("return (lambda *args: " + red + "foo " + reset + boldr + "(*args) " + reset + ")(1,2,3,4)" , lines )
4460
+ self .assertIn ("return baz2(1, 2,3,4)" , lines )
4461
+ self .assertIn ("return baz1(1, \n 2,3 \n , 4)" , lines )
4285
4462
self .assertIn (red + "bar" + reset + boldr + "()" + reset , lines )
4286
4463
4287
4464
def test_colorized_syntax_error (self ):
0 commit comments