Revert change to django/test/_doctest.py
This commit is contained in:
parent
5732bc52f1
commit
776ee92f00
|
@ -144,8 +144,6 @@ if sys.platform.startswith('java'):
|
||||||
# Option constants.
|
# Option constants.
|
||||||
|
|
||||||
OPTIONFLAGS_BY_NAME = {}
|
OPTIONFLAGS_BY_NAME = {}
|
||||||
|
|
||||||
|
|
||||||
def register_optionflag(name):
|
def register_optionflag(name):
|
||||||
# Create a new flag unless `name` is already known.
|
# Create a new flag unless `name` is already known.
|
||||||
return OPTIONFLAGS_BY_NAME.setdefault(name, 1 << len(OPTIONFLAGS_BY_NAME))
|
return OPTIONFLAGS_BY_NAME.setdefault(name, 1 << len(OPTIONFLAGS_BY_NAME))
|
||||||
|
@ -196,7 +194,6 @@ ELLIPSIS_MARKER = '...'
|
||||||
## 1. Utility Functions
|
## 1. Utility Functions
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
|
|
||||||
def _extract_future_flags(globs):
|
def _extract_future_flags(globs):
|
||||||
"""
|
"""
|
||||||
Return the compiler-flags associated with the future features that
|
Return the compiler-flags associated with the future features that
|
||||||
|
@ -209,7 +206,6 @@ def _extract_future_flags(globs):
|
||||||
flags |= feature.compiler_flag
|
flags |= feature.compiler_flag
|
||||||
return flags
|
return flags
|
||||||
|
|
||||||
|
|
||||||
def _normalize_module(module, depth=2):
|
def _normalize_module(module, depth=2):
|
||||||
"""
|
"""
|
||||||
Return the module specified by `module`. In particular:
|
Return the module specified by `module`. In particular:
|
||||||
|
@ -229,7 +225,6 @@ def _normalize_module(module, depth=2):
|
||||||
else:
|
else:
|
||||||
raise TypeError("Expected a module, string, or None")
|
raise TypeError("Expected a module, string, or None")
|
||||||
|
|
||||||
|
|
||||||
def _load_testfile(filename, package, module_relative):
|
def _load_testfile(filename, package, module_relative):
|
||||||
if module_relative:
|
if module_relative:
|
||||||
package = _normalize_module(package, 3)
|
package = _normalize_module(package, 3)
|
||||||
|
@ -243,7 +238,6 @@ def _load_testfile(filename, package, module_relative):
|
||||||
with open(filename) as fp:
|
with open(filename) as fp:
|
||||||
return fp.read(), filename
|
return fp.read(), filename
|
||||||
|
|
||||||
|
|
||||||
def _indent(s, indent=4):
|
def _indent(s, indent=4):
|
||||||
"""
|
"""
|
||||||
Add the given number of space characters to the beginning every
|
Add the given number of space characters to the beginning every
|
||||||
|
@ -252,7 +246,6 @@ def _indent(s, indent=4):
|
||||||
# This regexp matches the start of non-blank lines:
|
# This regexp matches the start of non-blank lines:
|
||||||
return re.sub('(?m)^(?!$)', indent*' ', s)
|
return re.sub('(?m)^(?!$)', indent*' ', s)
|
||||||
|
|
||||||
|
|
||||||
def _exception_traceback(exc_info):
|
def _exception_traceback(exc_info):
|
||||||
"""
|
"""
|
||||||
Return a string containing a traceback message for the given
|
Return a string containing a traceback message for the given
|
||||||
|
@ -264,7 +257,6 @@ def _exception_traceback(exc_info):
|
||||||
traceback.print_exception(exc_type, exc_val, exc_tb, file=excout)
|
traceback.print_exception(exc_type, exc_val, exc_tb, file=excout)
|
||||||
return excout.getvalue()
|
return excout.getvalue()
|
||||||
|
|
||||||
|
|
||||||
# Override some StringIO methods.
|
# Override some StringIO methods.
|
||||||
class _SpoofOut(StringIO):
|
class _SpoofOut(StringIO):
|
||||||
def getvalue(self):
|
def getvalue(self):
|
||||||
|
@ -285,7 +277,6 @@ class _SpoofOut(StringIO):
|
||||||
if hasattr(self, "softspace"):
|
if hasattr(self, "softspace"):
|
||||||
del self.softspace
|
del self.softspace
|
||||||
|
|
||||||
|
|
||||||
# Worst-case linear-time ellipsis matching.
|
# Worst-case linear-time ellipsis matching.
|
||||||
def _ellipsis_match(want, got):
|
def _ellipsis_match(want, got):
|
||||||
"""
|
"""
|
||||||
|
@ -336,7 +327,6 @@ def _ellipsis_match(want, got):
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def _comment_line(line):
|
def _comment_line(line):
|
||||||
"Return a commented form of the given line"
|
"Return a commented form of the given line"
|
||||||
line = line.rstrip()
|
line = line.rstrip()
|
||||||
|
@ -345,7 +335,6 @@ def _comment_line(line):
|
||||||
else:
|
else:
|
||||||
return '#'
|
return '#'
|
||||||
|
|
||||||
|
|
||||||
class _OutputRedirectingPdb(pdb.Pdb):
|
class _OutputRedirectingPdb(pdb.Pdb):
|
||||||
"""
|
"""
|
||||||
A specialized version of the python debugger that redirects stdout
|
A specialized version of the python debugger that redirects stdout
|
||||||
|
@ -379,7 +368,6 @@ class _OutputRedirectingPdb(pdb.Pdb):
|
||||||
finally:
|
finally:
|
||||||
sys.stdout = save_stdout
|
sys.stdout = save_stdout
|
||||||
|
|
||||||
|
|
||||||
# [XX] Normalize with respect to os.path.pardir?
|
# [XX] Normalize with respect to os.path.pardir?
|
||||||
def _module_relative_path(module, path):
|
def _module_relative_path(module, path):
|
||||||
if not inspect.ismodule(module):
|
if not inspect.ismodule(module):
|
||||||
|
@ -417,7 +405,6 @@ def _module_relative_path(module, path):
|
||||||
## a string (such as an object's docstring). The DocTest class also
|
## a string (such as an object's docstring). The DocTest class also
|
||||||
## includes information about where the string was extracted from.
|
## includes information about where the string was extracted from.
|
||||||
|
|
||||||
|
|
||||||
class Example:
|
class Example:
|
||||||
"""
|
"""
|
||||||
A single doctest example, consisting of source code and expected
|
A single doctest example, consisting of source code and expected
|
||||||
|
@ -471,7 +458,6 @@ class Example:
|
||||||
self.options = options
|
self.options = options
|
||||||
self.exc_msg = exc_msg
|
self.exc_msg = exc_msg
|
||||||
|
|
||||||
|
|
||||||
class DocTest:
|
class DocTest:
|
||||||
"""
|
"""
|
||||||
A collection of doctest examples that should be run in a single
|
A collection of doctest examples that should be run in a single
|
||||||
|
@ -520,10 +506,10 @@ class DocTest:
|
||||||
return ('<DocTest %s from %s:%s (%s)>' %
|
return ('<DocTest %s from %s:%s (%s)>' %
|
||||||
(self.name, self.filename, self.lineno, examples))
|
(self.name, self.filename, self.lineno, examples))
|
||||||
|
|
||||||
|
|
||||||
# This lets us sort tests by name:
|
# This lets us sort tests by name:
|
||||||
def _cmpkey(self):
|
def _cmpkey(self):
|
||||||
return (self.name, self.filename, self.lineno, id(self))
|
return (self.name, self.filename, self.lineno, id(self))
|
||||||
|
|
||||||
def __cmp__(self, other):
|
def __cmp__(self, other):
|
||||||
if not isinstance(other, DocTest):
|
if not isinstance(other, DocTest):
|
||||||
return -1
|
return -1
|
||||||
|
@ -912,7 +898,7 @@ class DocTestFinder:
|
||||||
elif hasattr(object, '__module__'):
|
elif hasattr(object, '__module__'):
|
||||||
return module.__name__ == object.__module__
|
return module.__name__ == object.__module__
|
||||||
elif isinstance(object, property):
|
elif isinstance(object, property):
|
||||||
return True # [XX] no way not be sure.
|
return True # [XX] no way not be sure.
|
||||||
else:
|
else:
|
||||||
raise ValueError("object must be a class or function")
|
raise ValueError("object must be a class or function")
|
||||||
|
|
||||||
|
@ -1068,7 +1054,6 @@ class DocTestFinder:
|
||||||
## 5. DocTest Runner
|
## 5. DocTest Runner
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
|
|
||||||
class DocTestRunner:
|
class DocTestRunner:
|
||||||
"""
|
"""
|
||||||
A class used to run DocTest test cases, and accumulate statistics.
|
A class used to run DocTest test cases, and accumulate statistics.
|
||||||
|
@ -1236,7 +1221,7 @@ class DocTestRunner:
|
||||||
# to modify them).
|
# to modify them).
|
||||||
original_optionflags = self.optionflags
|
original_optionflags = self.optionflags
|
||||||
|
|
||||||
SUCCESS, FAILURE, BOOM = range(3) # `outcome` state
|
SUCCESS, FAILURE, BOOM = range(3) # `outcome` state
|
||||||
|
|
||||||
check = self._checker.check_output
|
check = self._checker.check_output
|
||||||
|
|
||||||
|
@ -1289,7 +1274,7 @@ class DocTestRunner:
|
||||||
# Strip b"" and u"" prefixes from the repr and expected output
|
# Strip b"" and u"" prefixes from the repr and expected output
|
||||||
# TODO: better way of stripping the prefixes?
|
# TODO: better way of stripping the prefixes?
|
||||||
expected = example.want
|
expected = example.want
|
||||||
expected = expected.strip() # be wary of newlines
|
expected = expected.strip() # be wary of newlines
|
||||||
s = s.replace("u", "")
|
s = s.replace("u", "")
|
||||||
s = s.replace("b", "")
|
s = s.replace("b", "")
|
||||||
expected = expected.replace("u", "")
|
expected = expected.replace("u", "")
|
||||||
|
@ -1303,7 +1288,7 @@ class DocTestRunner:
|
||||||
lines.append(s)
|
lines.append(s)
|
||||||
|
|
||||||
# let them match
|
# let them match
|
||||||
if s == expected: # be wary of false positives here
|
if s == expected: # be wary of false positives here
|
||||||
# they should be the same, print expected value
|
# they should be the same, print expected value
|
||||||
sys.stdout.write("%s\n" % example.want.strip())
|
sys.stdout.write("%s\n" % example.want.strip())
|
||||||
|
|
||||||
|
@ -1329,13 +1314,13 @@ class DocTestRunner:
|
||||||
# Don't blink! This is where the user's code gets run.
|
# Don't blink! This is where the user's code gets run.
|
||||||
six.exec_(compile(example.source, filename, "single",
|
six.exec_(compile(example.source, filename, "single",
|
||||||
compileflags, 1), test.globs)
|
compileflags, 1), test.globs)
|
||||||
self.debugger.set_continue() # ==== Example Finished ====
|
self.debugger.set_continue() # ==== Example Finished ====
|
||||||
exception = None
|
exception = None
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
raise
|
raise
|
||||||
except:
|
except:
|
||||||
exception = sys.exc_info()
|
exception = sys.exc_info()
|
||||||
self.debugger.set_continue() # ==== Example Finished ====
|
self.debugger.set_continue() # ==== Example Finished ====
|
||||||
finally:
|
finally:
|
||||||
# restore the original displayhook
|
# restore the original displayhook
|
||||||
sys.displayhook = original_displayhook
|
sys.displayhook = original_displayhook
|
||||||
|
@ -1423,7 +1408,6 @@ class DocTestRunner:
|
||||||
__LINECACHE_FILENAME_RE = re.compile(r'<doctest '
|
__LINECACHE_FILENAME_RE = re.compile(r'<doctest '
|
||||||
r'(?P<name>[\w\.]+)'
|
r'(?P<name>[\w\.]+)'
|
||||||
r'\[(?P<examplenum>\d+)\]>$')
|
r'\[(?P<examplenum>\d+)\]>$')
|
||||||
|
|
||||||
def __patched_linecache_getlines(self, filename, module_globals=None):
|
def __patched_linecache_getlines(self, filename, module_globals=None):
|
||||||
m = self.__LINECACHE_FILENAME_RE.match(filename)
|
m = self.__LINECACHE_FILENAME_RE.match(filename)
|
||||||
if m and m.group('name') == self.test.name:
|
if m and m.group('name') == self.test.name:
|
||||||
|
@ -1557,7 +1541,6 @@ class DocTestRunner:
|
||||||
t = t + t2
|
t = t + t2
|
||||||
d[name] = f, t
|
d[name] = f, t
|
||||||
|
|
||||||
|
|
||||||
class OutputChecker:
|
class OutputChecker:
|
||||||
"""
|
"""
|
||||||
A class used to check the whether the actual output from a doctest
|
A class used to check the whether the actual output from a doctest
|
||||||
|
@ -1664,11 +1647,11 @@ class OutputChecker:
|
||||||
# Use difflib to find their differences.
|
# Use difflib to find their differences.
|
||||||
if optionflags & REPORT_UDIFF:
|
if optionflags & REPORT_UDIFF:
|
||||||
diff = difflib.unified_diff(want_lines, got_lines, n=2)
|
diff = difflib.unified_diff(want_lines, got_lines, n=2)
|
||||||
diff = list(diff)[2:] # strip the diff header
|
diff = list(diff)[2:] # strip the diff header
|
||||||
kind = 'unified diff with -expected +actual'
|
kind = 'unified diff with -expected +actual'
|
||||||
elif optionflags & REPORT_CDIFF:
|
elif optionflags & REPORT_CDIFF:
|
||||||
diff = difflib.context_diff(want_lines, got_lines, n=2)
|
diff = difflib.context_diff(want_lines, got_lines, n=2)
|
||||||
diff = list(diff)[2:] # strip the diff header
|
diff = list(diff)[2:] # strip the diff header
|
||||||
kind = 'context diff with expected followed by actual'
|
kind = 'context diff with expected followed by actual'
|
||||||
elif optionflags & REPORT_NDIFF:
|
elif optionflags & REPORT_NDIFF:
|
||||||
engine = difflib.Differ(charjunk=difflib.IS_CHARACTER_JUNK)
|
engine = difflib.Differ(charjunk=difflib.IS_CHARACTER_JUNK)
|
||||||
|
@ -1691,7 +1674,6 @@ class OutputChecker:
|
||||||
else:
|
else:
|
||||||
return 'Expected nothing\nGot nothing\n'
|
return 'Expected nothing\nGot nothing\n'
|
||||||
|
|
||||||
|
|
||||||
class DocTestFailure(Exception):
|
class DocTestFailure(Exception):
|
||||||
"""A DocTest example has failed in debugging mode.
|
"""A DocTest example has failed in debugging mode.
|
||||||
|
|
||||||
|
@ -1711,7 +1693,6 @@ class DocTestFailure(Exception):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.test)
|
return str(self.test)
|
||||||
|
|
||||||
|
|
||||||
class UnexpectedException(Exception):
|
class UnexpectedException(Exception):
|
||||||
"""A DocTest example has encountered an unexpected exception
|
"""A DocTest example has encountered an unexpected exception
|
||||||
|
|
||||||
|
@ -1731,7 +1712,6 @@ class UnexpectedException(Exception):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.test)
|
return str(self.test)
|
||||||
|
|
||||||
|
|
||||||
class DebugRunner(DocTestRunner):
|
class DebugRunner(DocTestRunner):
|
||||||
r"""Run doc tests but raise an exception as soon as there is a failure.
|
r"""Run doc tests but raise an exception as soon as there is a failure.
|
||||||
|
|
||||||
|
@ -1844,7 +1824,6 @@ class DebugRunner(DocTestRunner):
|
||||||
# class, updated by testmod.
|
# class, updated by testmod.
|
||||||
master = None
|
master = None
|
||||||
|
|
||||||
|
|
||||||
def testmod(m=None, name=None, globs=None, verbose=None,
|
def testmod(m=None, name=None, globs=None, verbose=None,
|
||||||
report=True, optionflags=0, extraglobs=None,
|
report=True, optionflags=0, extraglobs=None,
|
||||||
raise_on_error=False, exclude_empty=False):
|
raise_on_error=False, exclude_empty=False):
|
||||||
|
@ -1949,7 +1928,6 @@ def testmod(m=None, name=None, globs=None, verbose=None,
|
||||||
|
|
||||||
return runner.failures, runner.tries
|
return runner.failures, runner.tries
|
||||||
|
|
||||||
|
|
||||||
def testfile(filename, module_relative=True, name=None, package=None,
|
def testfile(filename, module_relative=True, name=None, package=None,
|
||||||
globs=None, verbose=None, report=True, optionflags=0,
|
globs=None, verbose=None, report=True, optionflags=0,
|
||||||
extraglobs=None, raise_on_error=False, parser=DocTestParser(),
|
extraglobs=None, raise_on_error=False, parser=DocTestParser(),
|
||||||
|
@ -2073,7 +2051,6 @@ def testfile(filename, module_relative=True, name=None, package=None,
|
||||||
|
|
||||||
return runner.failures, runner.tries
|
return runner.failures, runner.tries
|
||||||
|
|
||||||
|
|
||||||
def run_docstring_examples(f, globs, verbose=False, name="NoName",
|
def run_docstring_examples(f, globs, verbose=False, name="NoName",
|
||||||
compileflags=None, optionflags=0):
|
compileflags=None, optionflags=0):
|
||||||
"""
|
"""
|
||||||
|
@ -2103,7 +2080,6 @@ def run_docstring_examples(f, globs, verbose=False, name="NoName",
|
||||||
# This is provided only for backwards compatibility. It's not
|
# This is provided only for backwards compatibility. It's not
|
||||||
# actually used in any way.
|
# actually used in any way.
|
||||||
|
|
||||||
|
|
||||||
class Tester:
|
class Tester:
|
||||||
def __init__(self, mod=None, globs=None, verbose=None, optionflags=0):
|
def __init__(self, mod=None, globs=None, verbose=None, optionflags=0):
|
||||||
|
|
||||||
|
@ -2169,7 +2145,6 @@ class Tester:
|
||||||
|
|
||||||
_unittest_reportflags = 0
|
_unittest_reportflags = 0
|
||||||
|
|
||||||
|
|
||||||
def set_unittest_reportflags(flags):
|
def set_unittest_reportflags(flags):
|
||||||
"""Sets the unittest option flags.
|
"""Sets the unittest option flags.
|
||||||
|
|
||||||
|
@ -2353,7 +2328,6 @@ class DocTestCase(unittest.TestCase):
|
||||||
def shortDescription(self):
|
def shortDescription(self):
|
||||||
return "Doctest: " + self._dt_test.name
|
return "Doctest: " + self._dt_test.name
|
||||||
|
|
||||||
|
|
||||||
def DocTestSuite(module=None, globs=None, extraglobs=None, test_finder=None,
|
def DocTestSuite(module=None, globs=None, extraglobs=None, test_finder=None,
|
||||||
test_class=DocTestCase, **options):
|
test_class=DocTestCase, **options):
|
||||||
"""
|
"""
|
||||||
|
@ -2417,7 +2391,6 @@ def DocTestSuite(module=None, globs=None, extraglobs=None, test_finder=None,
|
||||||
|
|
||||||
return suite
|
return suite
|
||||||
|
|
||||||
|
|
||||||
class DocFileCase(DocTestCase):
|
class DocFileCase(DocTestCase):
|
||||||
|
|
||||||
def id(self):
|
def id(self):
|
||||||
|
@ -2432,7 +2405,6 @@ class DocFileCase(DocTestCase):
|
||||||
% (self._dt_test.name, self._dt_test.filename, err)
|
% (self._dt_test.name, self._dt_test.filename, err)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def DocFileTest(path, module_relative=True, package=None,
|
def DocFileTest(path, module_relative=True, package=None,
|
||||||
globs=None, parser=DocTestParser(),
|
globs=None, parser=DocTestParser(),
|
||||||
encoding=None, **options):
|
encoding=None, **options):
|
||||||
|
@ -2462,7 +2434,6 @@ def DocFileTest(path, module_relative=True, package=None,
|
||||||
test = parser.get_doctest(doc, globs, name, path, 0)
|
test = parser.get_doctest(doc, globs, name, path, 0)
|
||||||
return DocFileCase(test, **options)
|
return DocFileCase(test, **options)
|
||||||
|
|
||||||
|
|
||||||
def DocFileSuite(*paths, **kw):
|
def DocFileSuite(*paths, **kw):
|
||||||
"""A unittest suite for one or more doctest files.
|
"""A unittest suite for one or more doctest files.
|
||||||
|
|
||||||
|
@ -2536,7 +2507,6 @@ def DocFileSuite(*paths, **kw):
|
||||||
## 9. Debugging Support
|
## 9. Debugging Support
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
|
|
||||||
def script_from_examples(s):
|
def script_from_examples(s):
|
||||||
r"""Extract script from text with examples.
|
r"""Extract script from text with examples.
|
||||||
|
|
||||||
|
@ -2617,7 +2587,6 @@ def script_from_examples(s):
|
||||||
# Combine the output, and return it.
|
# Combine the output, and return it.
|
||||||
return '\n'.join(output)
|
return '\n'.join(output)
|
||||||
|
|
||||||
|
|
||||||
def testsource(module, name):
|
def testsource(module, name):
|
||||||
"""Extract the test sources from a doctest docstring as a script.
|
"""Extract the test sources from a doctest docstring as a script.
|
||||||
|
|
||||||
|
@ -2634,13 +2603,11 @@ def testsource(module, name):
|
||||||
testsrc = script_from_examples(test.docstring)
|
testsrc = script_from_examples(test.docstring)
|
||||||
return testsrc
|
return testsrc
|
||||||
|
|
||||||
|
|
||||||
def debug_src(src, pm=False, globs=None):
|
def debug_src(src, pm=False, globs=None):
|
||||||
"""Debug a single doctest docstring, in argument `src`'"""
|
"""Debug a single doctest docstring, in argument `src`'"""
|
||||||
testsrc = script_from_examples(src)
|
testsrc = script_from_examples(src)
|
||||||
debug_script(testsrc, pm, globs)
|
debug_script(testsrc, pm, globs)
|
||||||
|
|
||||||
|
|
||||||
def debug_script(src, pm=False, globs=None):
|
def debug_script(src, pm=False, globs=None):
|
||||||
"Debug a test script. `src` is the script, as a string."
|
"Debug a test script. `src` is the script, as a string."
|
||||||
import pdb
|
import pdb
|
||||||
|
@ -2672,7 +2639,6 @@ def debug_script(src, pm=False, globs=None):
|
||||||
finally:
|
finally:
|
||||||
os.remove(srcfilename)
|
os.remove(srcfilename)
|
||||||
|
|
||||||
|
|
||||||
def debug(module, name, pm=False):
|
def debug(module, name, pm=False):
|
||||||
"""Debug a single doctest docstring.
|
"""Debug a single doctest docstring.
|
||||||
|
|
||||||
|
@ -2687,8 +2653,6 @@ def debug(module, name, pm=False):
|
||||||
######################################################################
|
######################################################################
|
||||||
## 10. Example Usage
|
## 10. Example Usage
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
|
|
||||||
class _TestClass:
|
class _TestClass:
|
||||||
"""
|
"""
|
||||||
A pointless class, for sanity-checking of docstring testing.
|
A pointless class, for sanity-checking of docstring testing.
|
||||||
|
@ -2783,7 +2747,6 @@ __test__ = {"_TestClass": _TestClass,
|
||||||
""",
|
""",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def _test():
|
def _test():
|
||||||
r = unittest.TextTestRunner()
|
r = unittest.TextTestRunner()
|
||||||
r.run(DocTestSuite())
|
r.run(DocTestSuite())
|
||||||
|
|
Loading…
Reference in New Issue