Skip to content

Commit

Permalink
Fix whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Apr 6, 2024
1 parent d27c0a8 commit bb591b6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,13 @@ def __repr__(self):
# Option constants.

OPTIONFLAGS_BY_NAME = {}


def register_optionflag(name):
# Create a new flag unless `name` is already known.
return OPTIONFLAGS_BY_NAME.setdefault(name, 1 << len(OPTIONFLAGS_BY_NAME))


DONT_ACCEPT_TRUE_FOR_1 = register_optionflag('DONT_ACCEPT_TRUE_FOR_1')
DONT_ACCEPT_BLANKLINE = register_optionflag('DONT_ACCEPT_BLANKLINE')
NORMALIZE_WHITESPACE = register_optionflag('NORMALIZE_WHITESPACE')
Expand Down Expand Up @@ -194,6 +197,7 @@ def register_optionflag(name):
# 8. Debugging Support
# 9. Example Usage


######################################################################
## 1. Utility Functions
######################################################################
Expand All @@ -210,6 +214,7 @@ def _extract_future_flags(globs):
flags |= feature.compiler_flag
return flags


def _normalize_module(module, depth=2):
"""
Return the module specified by `module`. In particular:
Expand All @@ -235,10 +240,12 @@ def _normalize_module(module, depth=2):
else:
raise TypeError("Expected a module, string, or None")


def _newline_convert(data):
# The IO module provides a handy decoder for universal newline conversion
return IncrementalNewlineDecoder(None, True).decode(data, True)


def _load_testfile(filename, package, module_relative, encoding):
if module_relative:
package = _normalize_module(package, 3)
Expand All @@ -257,6 +264,7 @@ def _load_testfile(filename, package, module_relative, encoding):
with open(filename, encoding=encoding) as f:
return f.read(), filename


def _indent(s, indent=4):
"""
Add the given number of space characters to the beginning of
Expand All @@ -265,6 +273,7 @@ def _indent(s, indent=4):
# This regexp matches the start of non-blank lines:
return re.sub('(?m)^(?!$)', indent*' ', s)


def _exception_traceback(exc_info):
"""
Return a string containing a traceback message for the given
Expand All @@ -276,6 +285,7 @@ def _exception_traceback(exc_info):
traceback.print_exception(exc_type, exc_val, exc_tb, file=excout)
return excout.getvalue()


# Override some StringIO methods.
class _SpoofOut(StringIO):
def getvalue(self):
Expand All @@ -291,6 +301,7 @@ def truncate(self, size=None):
self.seek(size)
StringIO.truncate(self)


# Worst-case linear-time ellipsis matching.
def _ellipsis_match(want, got):
"""
Expand Down Expand Up @@ -341,6 +352,7 @@ def _ellipsis_match(want, got):

return True


def _comment_line(line):
"Return a commented form of the given line"
line = line.rstrip()
Expand All @@ -349,6 +361,7 @@ def _comment_line(line):
else:
return '#'


def _strip_exception_details(msg):
# Support for IGNORE_EXCEPTION_DETAIL.
# Get rid of everything except the exception name; in particular, drop
Expand All @@ -375,6 +388,7 @@ def _strip_exception_details(msg):
start = i+1
return msg[start: end]


class _OutputRedirectingPdb(pdb.Pdb):
"""
A specialized version of the python debugger that redirects stdout
Expand Down Expand Up @@ -411,6 +425,7 @@ def trace_dispatch(self, *args):
finally:
sys.stdout = save_stdout


# [XX] Normalize with respect to os.path.pardir?
def _module_relative_path(module, test_path):
if not inspect.ismodule(module):
Expand Down Expand Up @@ -446,6 +461,7 @@ def _module_relative_path(module, test_path):
# Combine the base directory and the test path.
return os.path.join(basedir, test_path)


######################################################################
## 2. Example & DocTest
######################################################################
Expand Down Expand Up @@ -526,6 +542,7 @@ def __hash__(self):
return hash((self.source, self.want, self.lineno, self.indent,
self.exc_msg))


class DocTest:
"""
A collection of doctest examples that should be run in a single
Expand Down Expand Up @@ -599,6 +616,7 @@ def __lt__(self, other):
<
(other.name, other.filename, other_lno, id(other)))


######################################################################
## 3. DocTestParser
######################################################################
Expand Down Expand Up @@ -1164,6 +1182,7 @@ def _find_lineno(self, obj, source_lines):
# We couldn't find the line number.
return None


######################################################################
## 5. DocTest Runner
######################################################################
Expand Down Expand Up @@ -1483,6 +1502,7 @@ def __record_outcome(self, test, failures, tries, skips):
__LINECACHE_FILENAME_RE = re.compile(r'<doctest '
r'(?P<name>.+)'
r'\[(?P<examplenum>\d+)\]>$')

def __patched_linecache_getlines(self, filename, module_globals=None):
m = self.__LINECACHE_FILENAME_RE.match(filename)
if m and m.group('name') == self.test.name:
Expand Down Expand Up @@ -1819,6 +1839,7 @@ def output_difference(self, example, got, optionflags):
else:
return 'Expected nothing\nGot nothing\n'


class DocTestFailure(Exception):
"""A DocTest example has failed in debugging mode.
Expand All @@ -1838,6 +1859,7 @@ def __init__(self, test, example, got):
def __str__(self):
return str(self.test)


class UnexpectedException(Exception):
"""A DocTest example has encountered an unexpected exception
Expand All @@ -1857,6 +1879,7 @@ def __init__(self, test, example, exc_info):
def __str__(self):
return str(self.test)


class DebugRunner(DocTestRunner):
r"""Run doc tests but raise an exception as soon as there is a failure.
Expand Down Expand Up @@ -1960,6 +1983,7 @@ def report_unexpected_exception(self, out, test, example, exc_info):
def report_failure(self, out, test, example, got):
raise DocTestFailure(test, example, got)


######################################################################
## 6. Test Functions
######################################################################
Expand All @@ -1969,6 +1993,7 @@ def report_failure(self, out, test, example, got):
# class, updated by testmod.
master = None


def testmod(m=None, name=None, globs=None, verbose=None,
report=True, optionflags=0, extraglobs=None,
raise_on_error=False, exclude_empty=False):
Expand Down Expand Up @@ -2221,12 +2246,14 @@ def run_docstring_examples(f, globs, verbose=False, name="NoName",
for test in finder.find(f, name, globs=globs):
runner.run(test, compileflags=compileflags)


######################################################################
## 7. Unittest Support
######################################################################

_unittest_reportflags = 0


def set_unittest_reportflags(flags):
"""Sets the unittest option flags.
Expand Down Expand Up @@ -2427,6 +2454,7 @@ def __repr__(self):
def shortDescription(self):
return "Doctest: " + self._dt_test.name


class SkipDocTestCase(DocTestCase):
def __init__(self, module):
self.module = module
Expand Down Expand Up @@ -2514,6 +2542,7 @@ def DocTestSuite(module=None, globs=None, extraglobs=None, test_finder=None,

return suite


class DocFileCase(DocTestCase):

def id(self):
Expand All @@ -2527,6 +2556,7 @@ def format_failure(self, err):
% (self._dt_test.name, self._dt_test.filename, err)
)


def DocFileTest(path, module_relative=True, package=None,
globs=None, parser=DocTestParser(),
encoding=None, **options):
Expand All @@ -2553,6 +2583,7 @@ def DocFileTest(path, module_relative=True, package=None,
test = parser.get_doctest(doc, globs, name, path, 0)
return DocFileCase(test, **options)


def DocFileSuite(*paths, **kw):
"""A unittest suite for one or more doctest files.
Expand Down Expand Up @@ -2622,6 +2653,7 @@ def DocFileSuite(*paths, **kw):

return suite


######################################################################
## 8. Debugging Support
######################################################################
Expand Down Expand Up @@ -2708,6 +2740,7 @@ def script_from_examples(s):
# Add a courtesy newline to prevent exec from choking (see bug #1172785)
return '\n'.join(output) + '\n'


def testsource(module, name):
"""Extract the test sources from a doctest docstring as a script.
Expand All @@ -2724,11 +2757,13 @@ def testsource(module, name):
testsrc = script_from_examples(test.docstring)
return testsrc


def debug_src(src, pm=False, globs=None):
"""Debug a single doctest docstring, in argument `src`'"""
testsrc = script_from_examples(src)
debug_script(testsrc, pm, globs)


def debug_script(src, pm=False, globs=None):
"Debug a test script. `src` is the script, as a string."
import pdb
Expand All @@ -2749,6 +2784,7 @@ def debug_script(src, pm=False, globs=None):
else:
pdb.Pdb(nosigint=True).run("exec(%r)" % src, globs, globs)


def debug(module, name, pm=False):
"""Debug a single doctest docstring.
Expand All @@ -2760,6 +2796,7 @@ def debug(module, name, pm=False):
testsrc = testsource(module, name)
debug_script(testsrc, pm, module.__dict__)


######################################################################
## 9. Example Usage
######################################################################
Expand Down Expand Up @@ -2807,6 +2844,7 @@ def get(self):

return self.val


__test__ = {"_TestClass": _TestClass,
"string": r"""
Example of a string object, searched as-is.
Expand Down
Loading

0 comments on commit bb591b6

Please sign in to comment.