@@ -963,7 +963,9 @@ class CPythonTracebackLegacyErrorCaretTests(
963
963
Same set of tests as above but with Python's legacy internal traceback printing.
964
964
"""
965
965
966
- class TracebackFormatTests (unittest .TestCase ):
966
+
967
+ class TracebackFormatMixin :
968
+ DEBUG_RANGES = True
967
969
968
970
def some_exception (self ):
969
971
raise KeyError ('blah' )
@@ -1137,6 +1139,8 @@ def g(count=10):
1137
1139
)
1138
1140
expected = (tb_line + result_g ).splitlines ()
1139
1141
actual = stderr_g .getvalue ().splitlines ()
1142
+ if not self .DEBUG_RANGES :
1143
+ expected = [line for line in expected if not set (line .strip ()) == {"^" }]
1140
1144
self .assertEqual (actual , expected )
1141
1145
1142
1146
# Check 2 different repetitive sections
@@ -1173,6 +1177,8 @@ def h(count=10):
1173
1177
)
1174
1178
expected = (result_h + result_g ).splitlines ()
1175
1179
actual = stderr_h .getvalue ().splitlines ()
1180
+ if not self .DEBUG_RANGES :
1181
+ expected = [line for line in expected if not set (line .strip ()) == {"^" }]
1176
1182
self .assertEqual (actual , expected )
1177
1183
1178
1184
# Check the boundary conditions. First, test just below the cutoff.
@@ -1199,11 +1205,13 @@ def h(count=10):
1199
1205
)
1200
1206
tb_line = (
1201
1207
'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 '
1203
1209
' g(traceback._RECURSIVE_CUTOFF)\n '
1204
1210
)
1205
1211
expected = (tb_line + result_g ).splitlines ()
1206
1212
actual = stderr_g .getvalue ().splitlines ()
1213
+ if not self .DEBUG_RANGES :
1214
+ expected = [line for line in expected if not set (line .strip ()) == {"^" }]
1207
1215
self .assertEqual (actual , expected )
1208
1216
1209
1217
# Second, test just above the cutoff.
@@ -1231,24 +1239,24 @@ def h(count=10):
1231
1239
)
1232
1240
tb_line = (
1233
1241
'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 '
1235
1243
' g(traceback._RECURSIVE_CUTOFF + 1)\n '
1236
1244
)
1237
1245
expected = (tb_line + result_g ).splitlines ()
1238
1246
actual = stderr_g .getvalue ().splitlines ()
1247
+ if not self .DEBUG_RANGES :
1248
+ expected = [line for line in expected if not set (line .strip ()) == {"^" }]
1239
1249
self .assertEqual (actual , expected )
1240
1250
1241
1251
@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 )
1252
1260
1253
1261
def test_format_stack (self ):
1254
1262
def fmt ():
@@ -1321,7 +1329,8 @@ def test_exception_group_deep_recursion_traceback(self):
1321
1329
def test_print_exception_bad_type_capi (self ):
1322
1330
from _testcapi import exception_print
1323
1331
with captured_output ("stderr" ) as stderr :
1324
- exception_print (42 )
1332
+ with support .catch_unraisable_exception ():
1333
+ exception_print (42 )
1325
1334
self .assertEqual (
1326
1335
stderr .getvalue (),
1327
1336
('TypeError: print_exception(): '
@@ -1345,6 +1354,24 @@ def test_print_exception_bad_type_python(self):
1345
1354
boundaries = re .compile (
1346
1355
'(%s|%s)' % (re .escape (cause_message ), re .escape (context_message )))
1347
1356
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
+
1348
1375
class BaseExceptionReportingTests :
1349
1376
1350
1377
def get_exception (self , exception_or_callable ):
0 commit comments