@@ -1259,27 +1259,30 @@ def run(self, test, compileflags=None, out=None, clear_globs=True):
12591259 if compileflags is None :
12601260 compileflags = _extract_future_flags (test .globs )
12611261
1262+ save_stdout = sys .stdout
12621263 if out is None :
1263- out = sys .stdout .write
1264- saveout = sys .stdout
1265-
1266- # Note that don't save away the previous pdb.set_trace. Rather,
1267- # we safe pdb.set_trace on import (see import section above).
1268- # We then call and restore that original cersion. We do it this
1269- # way to make this feature testable. If we kept and called the
1270- # previous version, we'd end up restoring the original stdout,
1271- # which is not what we want.
1264+ out = save_stdout .write
1265+ sys .stdout = self ._fakeout
1266+
1267+ # Patch pdb.set_trace to restore sys.stdout, so that interactive
1268+ # debugging output is visible (not still redirected to self._fakeout).
1269+ # Note that we run "the real" pdb.set_trace (captured at doctest
1270+ # import time) in our replacement. Because the current run() may
1271+ # run another doctest (and so on), the current pdb.set_trace may be
1272+ # our set_trace function, which changes sys.stdout. If we called
1273+ # a chain of those, we wouldn't be left with the save_stdout
1274+ # *this* run() invocation wants.
12721275 def set_trace ():
1273- sys .stdout = saveout
1276+ sys .stdout = save_stdout
12741277 real_pdb_set_trace ()
12751278
1279+ save_set_trace = pdb .set_trace
1280+ pdb .set_trace = set_trace
12761281 try :
1277- sys .stdout = self ._fakeout
1278- pdb .set_trace = set_trace
12791282 return self .__run (test , compileflags , out )
12801283 finally :
1281- sys .stdout = saveout
1282- pdb .set_trace = real_pdb_set_trace
1284+ sys .stdout = save_stdout
1285+ pdb .set_trace = save_set_trace
12831286 if clear_globs :
12841287 test .globs .clear ()
12851288
0 commit comments