@@ -963,7 +963,9 @@ class CPythonTracebackLegacyErrorCaretTests(
963963 Same set of tests as above but with Python's legacy internal traceback printing.
964964 """
965965
966- class TracebackFormatTests (unittest .TestCase ):
966+
967+ class TracebackFormatMixin :
968+ DEBUG_RANGES = True
967969
968970 def some_exception (self ):
969971 raise KeyError ('blah' )
@@ -1137,6 +1139,8 @@ def g(count=10):
11371139 )
11381140 expected = (tb_line + result_g ).splitlines ()
11391141 actual = stderr_g .getvalue ().splitlines ()
1142+ if not self .DEBUG_RANGES :
1143+ expected = [line for line in expected if not set (line .strip ()) == {"^" }]
11401144 self .assertEqual (actual , expected )
11411145
11421146 # Check 2 different repetitive sections
@@ -1173,6 +1177,8 @@ def h(count=10):
11731177 )
11741178 expected = (result_h + result_g ).splitlines ()
11751179 actual = stderr_h .getvalue ().splitlines ()
1180+ if not self .DEBUG_RANGES :
1181+ expected = [line for line in expected if not set (line .strip ()) == {"^" }]
11761182 self .assertEqual (actual , expected )
11771183
11781184 # Check the boundary conditions. First, test just below the cutoff.
@@ -1199,11 +1205,13 @@ def h(count=10):
11991205 )
12001206 tb_line = (
12011207 'Traceback (most recent call last):\n '
1202- f' File "{ __file__ } ", line { lineno_g + 77 } , in _check_recursive_traceback_display\n '
1208+ f' File "{ __file__ } ", line { lineno_g + 81 } , in _check_recursive_traceback_display\n '
12031209 ' g(traceback._RECURSIVE_CUTOFF)\n '
12041210 )
12051211 expected = (tb_line + result_g ).splitlines ()
12061212 actual = stderr_g .getvalue ().splitlines ()
1213+ if not self .DEBUG_RANGES :
1214+ expected = [line for line in expected if not set (line .strip ()) == {"^" }]
12071215 self .assertEqual (actual , expected )
12081216
12091217 # Second, test just above the cutoff.
@@ -1231,24 +1239,24 @@ def h(count=10):
12311239 )
12321240 tb_line = (
12331241 'Traceback (most recent call last):\n '
1234- f' File "{ __file__ } ", line { lineno_g + 108 } , in _check_recursive_traceback_display\n '
1242+ f' File "{ __file__ } ", line { lineno_g + 114 } , in _check_recursive_traceback_display\n '
12351243 ' g(traceback._RECURSIVE_CUTOFF + 1)\n '
12361244 )
12371245 expected = (tb_line + result_g ).splitlines ()
12381246 actual = stderr_g .getvalue ().splitlines ()
1247+ if not self .DEBUG_RANGES :
1248+ expected = [line for line in expected if not set (line .strip ()) == {"^" }]
12391249 self .assertEqual (actual , expected )
12401250
12411251 @requires_debug_ranges ()
1242- def test_recursive_traceback_python (self ):
1243- self ._check_recursive_traceback_display (traceback .print_exc )
1244-
1245- @cpython_only
1246- @requires_debug_ranges ()
1247- def test_recursive_traceback_cpython_internal (self ):
1248- from _testcapi import exception_print
1249- def render_exc ():
1250- exception_print (sys .exception ())
1251- self ._check_recursive_traceback_display (render_exc )
1252+ def test_recursive_traceback (self ):
1253+ if self .DEBUG_RANGES :
1254+ self ._check_recursive_traceback_display (traceback .print_exc )
1255+ else :
1256+ from _testcapi import exception_print
1257+ def render_exc ():
1258+ exception_print (sys .exception ())
1259+ self ._check_recursive_traceback_display (render_exc )
12521260
12531261 def test_format_stack (self ):
12541262 def fmt ():
@@ -1321,7 +1329,8 @@ def test_exception_group_deep_recursion_traceback(self):
13211329 def test_print_exception_bad_type_capi (self ):
13221330 from _testcapi import exception_print
13231331 with captured_output ("stderr" ) as stderr :
1324- exception_print (42 )
1332+ with support .catch_unraisable_exception ():
1333+ exception_print (42 )
13251334 self .assertEqual (
13261335 stderr .getvalue (),
13271336 ('TypeError: print_exception(): '
@@ -1345,6 +1354,24 @@ def test_print_exception_bad_type_python(self):
13451354boundaries = re .compile (
13461355 '(%s|%s)' % (re .escape (cause_message ), re .escape (context_message )))
13471356
1357+ class TestTracebackFormat (unittest .TestCase , TracebackFormatMixin ):
1358+ pass
1359+
1360+ @cpython_only
1361+ class TestFallbackTracebackFormat (unittest .TestCase , TracebackFormatMixin ):
1362+ DEBUG_RANGES = False
1363+ def setUp (self ) -> None :
1364+ self .original_unraisable_hook = sys .unraisablehook
1365+ sys .unraisablehook = lambda * args : None
1366+ self .original_hook = traceback ._print_exception_bltin
1367+ traceback ._print_exception_bltin = lambda * args : 1 / 0
1368+ return super ().setUp ()
1369+
1370+ def tearDown (self ) -> None :
1371+ traceback ._print_exception_bltin = self .original_hook
1372+ sys .unraisablehook = self .original_unraisable_hook
1373+ return super ().tearDown ()
1374+
13481375class BaseExceptionReportingTests :
13491376
13501377 def get_exception (self , exception_or_callable ):
0 commit comments