'Run the examples in `test`, and display the results using the
writer function `out`.
The examples are run in the namespace `test.globs`.  If
`clear_globs` is true (the default), then this namespace will
be cleared after the test runs, to help with garbage
collection.  If you would like to examine the namespace after
the test completes, then use `clear_globs=False`.
`compileflags` gives the set of flags that should be used by
the Python compiler when running the examples.  If not
specified, then it will default to the set of future-import
flags that apply to `globs`.
The output of each example is checked using
`DocTestRunner.check_output`, and the results are formatted by
the `DocTestRunner.report_*` methods.'

def run(self, test, compileflags=None, out=None, clear_globs=True):

    self.test = test
    if (compileflags is None):
        compileflags = _extract_future_flags(test.globs)
    save_stdout = sys.stdout
    if (out is None):
        out = save_stdout.write
    sys.stdout = self._fakeout
    save_set_trace = pdb.set_trace
    self.debugger = _OutputRedirectingPdb(save_stdout)
    self.debugger.reset()
    pdb.set_trace = self.debugger.set_trace
    self.save_linecache_getlines = linecache.getlines
    linecache.getlines = self.__patched_linecache_getlines
    save_displayhook = sys.displayhook
    sys.displayhook = sys.__displayhook__
    try:
        return self.__run(test, compileflags, out)
    finally:
        sys.stdout = save_stdout
        pdb.set_trace = save_set_trace
        linecache.getlines = self.save_linecache_getlines
        sys.displayhook = save_displayhook
        if clear_globs:
            test.globs.clear()
    return
