'Run the examples in `test`.  Write the outcome of each example
with one of the `DocTestRunner.report_*` methods, using the
writer function `out`.  `compileflags` is the set of compiler
flags that should be used to execute examples.  Return a tuple
`(f, t)`, where `t` is the number of examples tried, and `f`
is the number of examples that failed.  The examples are run
in the namespace `test.globs`.'

def __run(self, test, compileflags, out):

    failures = tries = 0
    original_optionflags = self.optionflags
    (SUCCESS, FAILURE, BOOM) = range(3)
    check = self._checker.check_output
    for (examplenum, example) in enumerate(test.examples):
        quiet = ((self.optionflags & REPORT_ONLY_FIRST_FAILURE) and (failures > 0))
        self.optionflags = original_optionflags
        if example.options:
            for (optionflag, val) in example.options.items():
                if val:
                    self.optionflags |= optionflag
                else:
                    self.optionflags &= (~ optionflag)
        if (self.optionflags & SKIP):
            continue
        tries += 1
        if (not quiet):
            self.report_start(out, test, example)
        filename = ('<doctest    %s[%d]>' % (test.name, examplenum))
        original_displayhook = sys.displayhook
        if six.PY3:
            lines = []
            def py3_displayhook(value):
                if (value is None):
                    return original_displayhook(value)
                s = repr(value)
                expected = example.want
                expected = expected.strip()
                s = s.replace('u', '')
                s = s.replace('b', '')
                expected = expected.replace('u', '')
                expected = expected.replace('b', '')
                s = s.replace("'", '"')
                expected = expected.replace("'", '"')
                lines.append(s)
                if (s == expected):
                    sys.stdout.write(('%s\n' % example.want.strip()))
                elif (len(expected.split('\n')) == len(lines)):
                    if ('\n'.join(lines) == expected):
                        sys.stdout.write(('%s\n' % example.want.strip()))
                    else:
                        sys.stdout.write(('%s\n' % repr(value)))
                elif (len(expected.split('\n')) != len(lines)):
                    pass
                else:
                    sys.stdout.write(('%s\n' % repr(value)))
            sys.displayhook = py3_displayhook
        try:
            six.exec_(compile(example.source, filename, 'single', compileflags, 1), test.globs)
            self.debugger.set_continue()
            exception = None
        except KeyboardInterrupt:
            raise
        except:
            exception = sys.exc_info()
            self.debugger.set_continue()
        finally:
            sys.displayhook = original_displayhook
        got = self._fakeout.getvalue()
        self._fakeout.truncate(0)
        self._fakeout.seek(0)
        outcome = FAILURE
        if (exception is None):
            if check(example.want, got, self.optionflags):
                outcome = SUCCESS
        else:
            exc_msg = traceback.format_exception_only(*exception[:2])[(-1)]
            if six.PY3:
                m = re.match('(.*)\\.(\\w+:.+\\s)', exc_msg)
                if (m != None):
                    f_name = m.group(1)
                    if (f_name == exception[0].__module__):
                        exc_msg = m.group(2)
            if (not quiet):
                got += _exception_traceback(exception)
            if (example.exc_msg is None):
                outcome = BOOM
            elif check(example.exc_msg, exc_msg, self.optionflags):
                outcome = SUCCESS
            elif (self.optionflags & IGNORE_EXCEPTION_DETAIL):
                m1 = re.match('[^:]*:', example.exc_msg)
                m2 = re.match('[^:]*:', exc_msg)
                if (m1 and m2 and check(m1.group(0), m2.group(0), self.optionflags)):
                    outcome = SUCCESS
        if (outcome is SUCCESS):
            if (not quiet):
                self.report_success(out, test, example, got)
        elif (outcome is FAILURE):
            if (not quiet):
                self.report_failure(out, test, example, got)
            failures += 1
        elif (outcome is BOOM):
            if (not quiet):
                self.report_unexpected_exception(out, test, example, exception)
            failures += 1
        else:
            assert False, ('unknown    outcome', outcome)
    self.optionflags = original_optionflags
    self.__record_outcome(test, failures, tries)
    return (failures, tries)
