@@ -108,6 +108,8 @@ def _test():
108
108
from _colorize import ANSIColors , can_colorize
109
109
110
110
111
+ __unittest = True
112
+
111
113
class TestResults (namedtuple ('TestResults' , 'failed attempted' )):
112
114
def __new__ (cls , failed , attempted , * , skipped = 0 ):
113
115
results = super ().__new__ (cls , failed , attempted )
@@ -1395,11 +1397,11 @@ def __run(self, test, compileflags, out):
1395
1397
exec (compile (example .source , filename , "single" ,
1396
1398
compileflags , True ), test .globs )
1397
1399
self .debugger .set_continue () # ==== Example Finished ====
1398
- exception = None
1400
+ exc_info = None
1399
1401
except KeyboardInterrupt :
1400
1402
raise
1401
- except :
1402
- exception = sys . exc_info ()
1403
+ except BaseException as exc :
1404
+ exc_info = type ( exc ), exc , exc . __traceback__ . tb_next
1403
1405
self .debugger .set_continue () # ==== Example Finished ====
1404
1406
1405
1407
got = self ._fakeout .getvalue () # the actual output
@@ -1408,21 +1410,21 @@ def __run(self, test, compileflags, out):
1408
1410
1409
1411
# If the example executed without raising any exceptions,
1410
1412
# verify its output.
1411
- if exception is None :
1413
+ if exc_info is None :
1412
1414
if check (example .want , got , self .optionflags ):
1413
1415
outcome = SUCCESS
1414
1416
1415
1417
# The example raised an exception: check if it was expected.
1416
1418
else :
1417
- formatted_ex = traceback .format_exception_only (* exception [:2 ])
1418
- if issubclass (exception [0 ], SyntaxError ):
1419
+ formatted_ex = traceback .format_exception_only (* exc_info [:2 ])
1420
+ if issubclass (exc_info [0 ], SyntaxError ):
1419
1421
# SyntaxError / IndentationError is special:
1420
1422
# we don't care about the carets / suggestions / etc
1421
1423
# We only care about the error message and notes.
1422
1424
# They start with `SyntaxError:` (or any other class name)
1423
1425
exception_line_prefixes = (
1424
- f"{ exception [0 ].__qualname__ } :" ,
1425
- f"{ exception [0 ].__module__ } .{ exception [0 ].__qualname__ } :" ,
1426
+ f"{ exc_info [0 ].__qualname__ } :" ,
1427
+ f"{ exc_info [0 ].__module__ } .{ exc_info [0 ].__qualname__ } :" ,
1426
1428
)
1427
1429
exc_msg_index = next (
1428
1430
index
@@ -1433,7 +1435,7 @@ def __run(self, test, compileflags, out):
1433
1435
1434
1436
exc_msg = "" .join (formatted_ex )
1435
1437
if not quiet :
1436
- got += _exception_traceback (exception )
1438
+ got += _exception_traceback (exc_info )
1437
1439
1438
1440
# If `example.exc_msg` is None, then we weren't expecting
1439
1441
# an exception.
@@ -1462,7 +1464,7 @@ def __run(self, test, compileflags, out):
1462
1464
elif outcome is BOOM :
1463
1465
if not quiet :
1464
1466
self .report_unexpected_exception (out , test , example ,
1465
- exception )
1467
+ exc_info )
1466
1468
failures += 1
1467
1469
else :
1468
1470
assert False , ("unknown outcome" , outcome )
@@ -2324,7 +2326,7 @@ def runTest(self):
2324
2326
sys .stdout = old
2325
2327
2326
2328
if results .failed :
2327
- raise self .failureException (self .format_failure (new .getvalue ()))
2329
+ raise self .failureException (self .format_failure (new .getvalue (). rstrip ( ' \n ' ) ))
2328
2330
2329
2331
def format_failure (self , err ):
2330
2332
test = self ._dt_test
0 commit comments