@@ -109,6 +109,8 @@ def _test():
109109from _colorize import ANSIColors , can_colorize
110110
111111
112+ __unittest = True
113+
112114class TestResults (namedtuple ('TestResults' , 'failed attempted' )):
113115 def __new__ (cls , failed , attempted , * , skipped = 0 ):
114116 results = super ().__new__ (cls , failed , attempted )
@@ -390,11 +392,11 @@ def __init__(self, out):
390392 # still use input() to get user input
391393 self .use_rawinput = 1
392394
393- def set_trace (self , frame = None ):
395+ def set_trace (self , frame = None , * , commands = None ):
394396 self .__debugger_used = True
395397 if frame is None :
396398 frame = sys ._getframe ().f_back
397- pdb .Pdb .set_trace (self , frame )
399+ pdb .Pdb .set_trace (self , frame , commands = commands )
398400
399401 def set_continue (self ):
400402 # Calling set_continue unconditionally would break unit test
@@ -1230,7 +1232,7 @@ class DocTestRunner:
12301232 `OutputChecker` to the constructor.
12311233
12321234 The test runner's display output can be controlled in two ways.
1233- First, an output function (`out) can be passed to
1235+ First, an output function (`out` ) can be passed to
12341236 `TestRunner.run`; this function will be called with strings that
12351237 should be displayed. It defaults to `sys.stdout.write`. If
12361238 capturing the output is not sufficient, then the display output
@@ -1398,11 +1400,11 @@ def __run(self, test, compileflags, out):
13981400 exec (compile (example .source , filename , "single" ,
13991401 compileflags , True ), test .globs )
14001402 self .debugger .set_continue () # ==== Example Finished ====
1401- exception = None
1403+ exc_info = None
14021404 except KeyboardInterrupt :
14031405 raise
1404- except :
1405- exception = sys . exc_info ()
1406+ except BaseException as exc :
1407+ exc_info = type ( exc ), exc , exc . __traceback__ . tb_next
14061408 self .debugger .set_continue () # ==== Example Finished ====
14071409
14081410 got = self ._fakeout .getvalue () # the actual output
@@ -1411,21 +1413,21 @@ def __run(self, test, compileflags, out):
14111413
14121414 # If the example executed without raising any exceptions,
14131415 # verify its output.
1414- if exception is None :
1416+ if exc_info is None :
14151417 if check (example .want , got , self .optionflags ):
14161418 outcome = SUCCESS
14171419
14181420 # The example raised an exception: check if it was expected.
14191421 else :
1420- formatted_ex = traceback .format_exception_only (* exception [:2 ])
1421- if issubclass (exception [0 ], SyntaxError ):
1422+ formatted_ex = traceback .format_exception_only (* exc_info [:2 ])
1423+ if issubclass (exc_info [0 ], SyntaxError ):
14221424 # SyntaxError / IndentationError is special:
14231425 # we don't care about the carets / suggestions / etc
14241426 # We only care about the error message and notes.
14251427 # They start with `SyntaxError:` (or any other class name)
14261428 exception_line_prefixes = (
1427- f"{ exception [0 ].__qualname__ } :" ,
1428- f"{ exception [0 ].__module__ } .{ exception [0 ].__qualname__ } :" ,
1429+ f"{ exc_info [0 ].__qualname__ } :" ,
1430+ f"{ exc_info [0 ].__module__ } .{ exc_info [0 ].__qualname__ } :" ,
14291431 )
14301432 exc_msg_index = next (
14311433 index
@@ -1436,7 +1438,7 @@ def __run(self, test, compileflags, out):
14361438
14371439 exc_msg = "" .join (formatted_ex )
14381440 if not quiet :
1439- got += _exception_traceback (exception )
1441+ got += _exception_traceback (exc_info )
14401442
14411443 # If `example.exc_msg` is None, then we weren't expecting
14421444 # an exception.
@@ -1465,7 +1467,7 @@ def __run(self, test, compileflags, out):
14651467 elif outcome is BOOM :
14661468 if not quiet :
14671469 self .report_unexpected_exception (out , test , example ,
1468- exception )
1470+ exc_info )
14691471 failures += 1
14701472 else :
14711473 assert False , ("unknown outcome" , outcome )
@@ -2327,7 +2329,7 @@ def runTest(self):
23272329 sys .stdout = old
23282330
23292331 if results .failed :
2330- raise self .failureException (self .format_failure (new .getvalue ()))
2332+ raise self .failureException (self .format_failure (new .getvalue (). rstrip ( ' \n ' ) ))
23312333
23322334 def format_failure (self , err ):
23332335 test = self ._dt_test
@@ -2737,7 +2739,7 @@ def testsource(module, name):
27372739 return testsrc
27382740
27392741def debug_src (src , pm = False , globs = None ):
2740- """Debug a single doctest docstring, in argument `src`' """
2742+ """Debug a single doctest docstring, in argument `src`"""
27412743 testsrc = script_from_examples (src )
27422744 debug_script (testsrc , pm , globs )
27432745
@@ -2873,7 +2875,7 @@ def get(self):
28732875def _test ():
28742876 import argparse
28752877
2876- parser = argparse .ArgumentParser (description = "doctest runner" )
2878+ parser = argparse .ArgumentParser (description = "doctest runner" , color = True )
28772879 parser .add_argument ('-v' , '--verbose' , action = 'store_true' , default = False ,
28782880 help = 'print very verbose output for all tests' )
28792881 parser .add_argument ('-o' , '--option' , action = 'append' ,
0 commit comments