def debug(self, test, pm=False):
self.test = test
# Save the old stdout
self.save_stdout = sys.stdout
# Convert the source docstring to a script.
script = self._script_from_examples(test.docstring)
# Create a debugger.
debugger = _OutputRedirectingPdb(sys.stdout)
# Patch pdb.set_trace to restore sys.stdout during interactive
# debugging (so it's not still redirected to self._fakeout).
save_set_trace = pdb.set_trace
pdb.set_trace = debugger.set_trace
# Write the script to a temporary file. Note that
# tempfile.NameTemporaryFile() cannot be used. As the docs
# say, a file so created cannot be opened by name a second
# time on modern Windows boxes, and execfile() needs to open
# it.
srcfilename = tempfile.mktemp(".py", "doctestdebug_")
f = open(srcfilename, 'w')
f.write(script)
f.close()
# Set up the globals
test.globs['CHECK_OUTPUT'] = self._check_output
test.globs['CHECK_EXCEPTION'] = self._check_exception
test.globs['__print__'] = self._print_if_not_none
test.globs['__set_trace__'] = debugger.set_trace
test.globs['__examples__'] = self.test.examples
try:
if pm is False:
debugger.run("execfile(%r)" % srcfilename,
test.globs, test.globs)
else:
try:
sys.stdout = _SpoofOut()
try:
execfile(srcfilename, test.globs)
except bdb.BdbQuit:
return
except:
sys.stdout = self.save_stdout
exc_info = sys.exc_info()
exc_msg = traceback.format_exception_only(
exc_info[0], exc_info[1])[-1]
self.save_stdout.write(self.runner.DIVIDER+'\n')
self.save_stdout.write('Unexpected exception:\n' +
_indent(exc_msg))
raise
#self.post_mortem(debugger, exc_info[2])
finally:
sys.stdout = self.save_stdout
finally:
sys.set_trace = save_set_trace
os.remove(srcfilename)
评论列表
文章目录