Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../proc/self/root/lib64/python3....
File: doctest.py
# Module doctest.
[0] Fix | Delete
# Released to the public domain 16-Jan-2001, by Tim Peters (tim@python.org).
[1] Fix | Delete
# Major enhancements and refactoring by:
[2] Fix | Delete
# Jim Fulton
[3] Fix | Delete
# Edward Loper
[4] Fix | Delete
[5] Fix | Delete
# Provided as-is; use at your own risk; no warranty; no promises; enjoy!
[6] Fix | Delete
[7] Fix | Delete
r"""Module doctest -- a framework for running examples in docstrings.
[8] Fix | Delete
[9] Fix | Delete
In simplest use, end each module M to be tested with:
[10] Fix | Delete
[11] Fix | Delete
def _test():
[12] Fix | Delete
import doctest
[13] Fix | Delete
doctest.testmod()
[14] Fix | Delete
[15] Fix | Delete
if __name__ == "__main__":
[16] Fix | Delete
_test()
[17] Fix | Delete
[18] Fix | Delete
Then running the module as a script will cause the examples in the
[19] Fix | Delete
docstrings to get executed and verified:
[20] Fix | Delete
[21] Fix | Delete
python M.py
[22] Fix | Delete
[23] Fix | Delete
This won't display anything unless an example fails, in which case the
[24] Fix | Delete
failing example(s) and the cause(s) of the failure(s) are printed to stdout
[25] Fix | Delete
(why not stderr? because stderr is a lame hack <0.2 wink>), and the final
[26] Fix | Delete
line of output is "Test failed.".
[27] Fix | Delete
[28] Fix | Delete
Run it with the -v switch instead:
[29] Fix | Delete
[30] Fix | Delete
python M.py -v
[31] Fix | Delete
[32] Fix | Delete
and a detailed report of all examples tried is printed to stdout, along
[33] Fix | Delete
with assorted summaries at the end.
[34] Fix | Delete
[35] Fix | Delete
You can force verbose mode by passing "verbose=True" to testmod, or prohibit
[36] Fix | Delete
it by passing "verbose=False". In either of those cases, sys.argv is not
[37] Fix | Delete
examined by testmod.
[38] Fix | Delete
[39] Fix | Delete
There are a variety of other ways to run doctests, including integration
[40] Fix | Delete
with the unittest framework, and support for running non-Python text
[41] Fix | Delete
files containing doctests. There are also many ways to override parts
[42] Fix | Delete
of doctest's default behaviors. See the Library Reference Manual for
[43] Fix | Delete
details.
[44] Fix | Delete
"""
[45] Fix | Delete
[46] Fix | Delete
__docformat__ = 'reStructuredText en'
[47] Fix | Delete
[48] Fix | Delete
__all__ = [
[49] Fix | Delete
# 0, Option Flags
[50] Fix | Delete
'register_optionflag',
[51] Fix | Delete
'DONT_ACCEPT_TRUE_FOR_1',
[52] Fix | Delete
'DONT_ACCEPT_BLANKLINE',
[53] Fix | Delete
'NORMALIZE_WHITESPACE',
[54] Fix | Delete
'ELLIPSIS',
[55] Fix | Delete
'SKIP',
[56] Fix | Delete
'IGNORE_EXCEPTION_DETAIL',
[57] Fix | Delete
'COMPARISON_FLAGS',
[58] Fix | Delete
'REPORT_UDIFF',
[59] Fix | Delete
'REPORT_CDIFF',
[60] Fix | Delete
'REPORT_NDIFF',
[61] Fix | Delete
'REPORT_ONLY_FIRST_FAILURE',
[62] Fix | Delete
'REPORTING_FLAGS',
[63] Fix | Delete
'FAIL_FAST',
[64] Fix | Delete
# 1. Utility Functions
[65] Fix | Delete
# 2. Example & DocTest
[66] Fix | Delete
'Example',
[67] Fix | Delete
'DocTest',
[68] Fix | Delete
# 3. Doctest Parser
[69] Fix | Delete
'DocTestParser',
[70] Fix | Delete
# 4. Doctest Finder
[71] Fix | Delete
'DocTestFinder',
[72] Fix | Delete
# 5. Doctest Runner
[73] Fix | Delete
'DocTestRunner',
[74] Fix | Delete
'OutputChecker',
[75] Fix | Delete
'DocTestFailure',
[76] Fix | Delete
'UnexpectedException',
[77] Fix | Delete
'DebugRunner',
[78] Fix | Delete
# 6. Test Functions
[79] Fix | Delete
'testmod',
[80] Fix | Delete
'testfile',
[81] Fix | Delete
'run_docstring_examples',
[82] Fix | Delete
# 7. Unittest Support
[83] Fix | Delete
'DocTestSuite',
[84] Fix | Delete
'DocFileSuite',
[85] Fix | Delete
'set_unittest_reportflags',
[86] Fix | Delete
# 8. Debugging Support
[87] Fix | Delete
'script_from_examples',
[88] Fix | Delete
'testsource',
[89] Fix | Delete
'debug_src',
[90] Fix | Delete
'debug',
[91] Fix | Delete
]
[92] Fix | Delete
[93] Fix | Delete
import __future__
[94] Fix | Delete
import argparse
[95] Fix | Delete
import difflib
[96] Fix | Delete
import inspect
[97] Fix | Delete
import linecache
[98] Fix | Delete
import os
[99] Fix | Delete
import pdb
[100] Fix | Delete
import re
[101] Fix | Delete
import sys
[102] Fix | Delete
import traceback
[103] Fix | Delete
import unittest
[104] Fix | Delete
from io import StringIO
[105] Fix | Delete
from collections import namedtuple
[106] Fix | Delete
[107] Fix | Delete
TestResults = namedtuple('TestResults', 'failed attempted')
[108] Fix | Delete
[109] Fix | Delete
# There are 4 basic classes:
[110] Fix | Delete
# - Example: a <source, want> pair, plus an intra-docstring line number.
[111] Fix | Delete
# - DocTest: a collection of examples, parsed from a docstring, plus
[112] Fix | Delete
# info about where the docstring came from (name, filename, lineno).
[113] Fix | Delete
# - DocTestFinder: extracts DocTests from a given object's docstring and
[114] Fix | Delete
# its contained objects' docstrings.
[115] Fix | Delete
# - DocTestRunner: runs DocTest cases, and accumulates statistics.
[116] Fix | Delete
#
[117] Fix | Delete
# So the basic picture is:
[118] Fix | Delete
#
[119] Fix | Delete
# list of:
[120] Fix | Delete
# +------+ +---------+ +-------+
[121] Fix | Delete
# |object| --DocTestFinder-> | DocTest | --DocTestRunner-> |results|
[122] Fix | Delete
# +------+ +---------+ +-------+
[123] Fix | Delete
# | Example |
[124] Fix | Delete
# | ... |
[125] Fix | Delete
# | Example |
[126] Fix | Delete
# +---------+
[127] Fix | Delete
[128] Fix | Delete
# Option constants.
[129] Fix | Delete
[130] Fix | Delete
OPTIONFLAGS_BY_NAME = {}
[131] Fix | Delete
def register_optionflag(name):
[132] Fix | Delete
# Create a new flag unless `name` is already known.
[133] Fix | Delete
return OPTIONFLAGS_BY_NAME.setdefault(name, 1 << len(OPTIONFLAGS_BY_NAME))
[134] Fix | Delete
[135] Fix | Delete
DONT_ACCEPT_TRUE_FOR_1 = register_optionflag('DONT_ACCEPT_TRUE_FOR_1')
[136] Fix | Delete
DONT_ACCEPT_BLANKLINE = register_optionflag('DONT_ACCEPT_BLANKLINE')
[137] Fix | Delete
NORMALIZE_WHITESPACE = register_optionflag('NORMALIZE_WHITESPACE')
[138] Fix | Delete
ELLIPSIS = register_optionflag('ELLIPSIS')
[139] Fix | Delete
SKIP = register_optionflag('SKIP')
[140] Fix | Delete
IGNORE_EXCEPTION_DETAIL = register_optionflag('IGNORE_EXCEPTION_DETAIL')
[141] Fix | Delete
[142] Fix | Delete
COMPARISON_FLAGS = (DONT_ACCEPT_TRUE_FOR_1 |
[143] Fix | Delete
DONT_ACCEPT_BLANKLINE |
[144] Fix | Delete
NORMALIZE_WHITESPACE |
[145] Fix | Delete
ELLIPSIS |
[146] Fix | Delete
SKIP |
[147] Fix | Delete
IGNORE_EXCEPTION_DETAIL)
[148] Fix | Delete
[149] Fix | Delete
REPORT_UDIFF = register_optionflag('REPORT_UDIFF')
[150] Fix | Delete
REPORT_CDIFF = register_optionflag('REPORT_CDIFF')
[151] Fix | Delete
REPORT_NDIFF = register_optionflag('REPORT_NDIFF')
[152] Fix | Delete
REPORT_ONLY_FIRST_FAILURE = register_optionflag('REPORT_ONLY_FIRST_FAILURE')
[153] Fix | Delete
FAIL_FAST = register_optionflag('FAIL_FAST')
[154] Fix | Delete
[155] Fix | Delete
REPORTING_FLAGS = (REPORT_UDIFF |
[156] Fix | Delete
REPORT_CDIFF |
[157] Fix | Delete
REPORT_NDIFF |
[158] Fix | Delete
REPORT_ONLY_FIRST_FAILURE |
[159] Fix | Delete
FAIL_FAST)
[160] Fix | Delete
[161] Fix | Delete
# Special string markers for use in `want` strings:
[162] Fix | Delete
BLANKLINE_MARKER = '<BLANKLINE>'
[163] Fix | Delete
ELLIPSIS_MARKER = '...'
[164] Fix | Delete
[165] Fix | Delete
######################################################################
[166] Fix | Delete
## Table of Contents
[167] Fix | Delete
######################################################################
[168] Fix | Delete
# 1. Utility Functions
[169] Fix | Delete
# 2. Example & DocTest -- store test cases
[170] Fix | Delete
# 3. DocTest Parser -- extracts examples from strings
[171] Fix | Delete
# 4. DocTest Finder -- extracts test cases from objects
[172] Fix | Delete
# 5. DocTest Runner -- runs test cases
[173] Fix | Delete
# 6. Test Functions -- convenient wrappers for testing
[174] Fix | Delete
# 7. Unittest Support
[175] Fix | Delete
# 8. Debugging Support
[176] Fix | Delete
# 9. Example Usage
[177] Fix | Delete
[178] Fix | Delete
######################################################################
[179] Fix | Delete
## 1. Utility Functions
[180] Fix | Delete
######################################################################
[181] Fix | Delete
[182] Fix | Delete
def _extract_future_flags(globs):
[183] Fix | Delete
"""
[184] Fix | Delete
Return the compiler-flags associated with the future features that
[185] Fix | Delete
have been imported into the given namespace (globs).
[186] Fix | Delete
"""
[187] Fix | Delete
flags = 0
[188] Fix | Delete
for fname in __future__.all_feature_names:
[189] Fix | Delete
feature = globs.get(fname, None)
[190] Fix | Delete
if feature is getattr(__future__, fname):
[191] Fix | Delete
flags |= feature.compiler_flag
[192] Fix | Delete
return flags
[193] Fix | Delete
[194] Fix | Delete
def _normalize_module(module, depth=2):
[195] Fix | Delete
"""
[196] Fix | Delete
Return the module specified by `module`. In particular:
[197] Fix | Delete
- If `module` is a module, then return module.
[198] Fix | Delete
- If `module` is a string, then import and return the
[199] Fix | Delete
module with that name.
[200] Fix | Delete
- If `module` is None, then return the calling module.
[201] Fix | Delete
The calling module is assumed to be the module of
[202] Fix | Delete
the stack frame at the given depth in the call stack.
[203] Fix | Delete
"""
[204] Fix | Delete
if inspect.ismodule(module):
[205] Fix | Delete
return module
[206] Fix | Delete
elif isinstance(module, str):
[207] Fix | Delete
return __import__(module, globals(), locals(), ["*"])
[208] Fix | Delete
elif module is None:
[209] Fix | Delete
return sys.modules[sys._getframe(depth).f_globals['__name__']]
[210] Fix | Delete
else:
[211] Fix | Delete
raise TypeError("Expected a module, string, or None")
[212] Fix | Delete
[213] Fix | Delete
def _load_testfile(filename, package, module_relative, encoding):
[214] Fix | Delete
if module_relative:
[215] Fix | Delete
package = _normalize_module(package, 3)
[216] Fix | Delete
filename = _module_relative_path(package, filename)
[217] Fix | Delete
if getattr(package, '__loader__', None) is not None:
[218] Fix | Delete
if hasattr(package.__loader__, 'get_data'):
[219] Fix | Delete
file_contents = package.__loader__.get_data(filename)
[220] Fix | Delete
file_contents = file_contents.decode(encoding)
[221] Fix | Delete
# get_data() opens files as 'rb', so one must do the equivalent
[222] Fix | Delete
# conversion as universal newlines would do.
[223] Fix | Delete
return file_contents.replace(os.linesep, '\n'), filename
[224] Fix | Delete
with open(filename, encoding=encoding) as f:
[225] Fix | Delete
return f.read(), filename
[226] Fix | Delete
[227] Fix | Delete
def _indent(s, indent=4):
[228] Fix | Delete
"""
[229] Fix | Delete
Add the given number of space characters to the beginning of
[230] Fix | Delete
every non-blank line in `s`, and return the result.
[231] Fix | Delete
"""
[232] Fix | Delete
# This regexp matches the start of non-blank lines:
[233] Fix | Delete
return re.sub('(?m)^(?!$)', indent*' ', s)
[234] Fix | Delete
[235] Fix | Delete
def _exception_traceback(exc_info):
[236] Fix | Delete
"""
[237] Fix | Delete
Return a string containing a traceback message for the given
[238] Fix | Delete
exc_info tuple (as returned by sys.exc_info()).
[239] Fix | Delete
"""
[240] Fix | Delete
# Get a traceback message.
[241] Fix | Delete
excout = StringIO()
[242] Fix | Delete
exc_type, exc_val, exc_tb = exc_info
[243] Fix | Delete
traceback.print_exception(exc_type, exc_val, exc_tb, file=excout)
[244] Fix | Delete
return excout.getvalue()
[245] Fix | Delete
[246] Fix | Delete
# Override some StringIO methods.
[247] Fix | Delete
class _SpoofOut(StringIO):
[248] Fix | Delete
def getvalue(self):
[249] Fix | Delete
result = StringIO.getvalue(self)
[250] Fix | Delete
# If anything at all was written, make sure there's a trailing
[251] Fix | Delete
# newline. There's no way for the expected output to indicate
[252] Fix | Delete
# that a trailing newline is missing.
[253] Fix | Delete
if result and not result.endswith("\n"):
[254] Fix | Delete
result += "\n"
[255] Fix | Delete
return result
[256] Fix | Delete
[257] Fix | Delete
def truncate(self, size=None):
[258] Fix | Delete
self.seek(size)
[259] Fix | Delete
StringIO.truncate(self)
[260] Fix | Delete
[261] Fix | Delete
# Worst-case linear-time ellipsis matching.
[262] Fix | Delete
def _ellipsis_match(want, got):
[263] Fix | Delete
"""
[264] Fix | Delete
Essentially the only subtle case:
[265] Fix | Delete
>>> _ellipsis_match('aa...aa', 'aaa')
[266] Fix | Delete
False
[267] Fix | Delete
"""
[268] Fix | Delete
if ELLIPSIS_MARKER not in want:
[269] Fix | Delete
return want == got
[270] Fix | Delete
[271] Fix | Delete
# Find "the real" strings.
[272] Fix | Delete
ws = want.split(ELLIPSIS_MARKER)
[273] Fix | Delete
assert len(ws) >= 2
[274] Fix | Delete
[275] Fix | Delete
# Deal with exact matches possibly needed at one or both ends.
[276] Fix | Delete
startpos, endpos = 0, len(got)
[277] Fix | Delete
w = ws[0]
[278] Fix | Delete
if w: # starts with exact match
[279] Fix | Delete
if got.startswith(w):
[280] Fix | Delete
startpos = len(w)
[281] Fix | Delete
del ws[0]
[282] Fix | Delete
else:
[283] Fix | Delete
return False
[284] Fix | Delete
w = ws[-1]
[285] Fix | Delete
if w: # ends with exact match
[286] Fix | Delete
if got.endswith(w):
[287] Fix | Delete
endpos -= len(w)
[288] Fix | Delete
del ws[-1]
[289] Fix | Delete
else:
[290] Fix | Delete
return False
[291] Fix | Delete
[292] Fix | Delete
if startpos > endpos:
[293] Fix | Delete
# Exact end matches required more characters than we have, as in
[294] Fix | Delete
# _ellipsis_match('aa...aa', 'aaa')
[295] Fix | Delete
return False
[296] Fix | Delete
[297] Fix | Delete
# For the rest, we only need to find the leftmost non-overlapping
[298] Fix | Delete
# match for each piece. If there's no overall match that way alone,
[299] Fix | Delete
# there's no overall match period.
[300] Fix | Delete
for w in ws:
[301] Fix | Delete
# w may be '' at times, if there are consecutive ellipses, or
[302] Fix | Delete
# due to an ellipsis at the start or end of `want`. That's OK.
[303] Fix | Delete
# Search for an empty string succeeds, and doesn't change startpos.
[304] Fix | Delete
startpos = got.find(w, startpos, endpos)
[305] Fix | Delete
if startpos < 0:
[306] Fix | Delete
return False
[307] Fix | Delete
startpos += len(w)
[308] Fix | Delete
[309] Fix | Delete
return True
[310] Fix | Delete
[311] Fix | Delete
def _comment_line(line):
[312] Fix | Delete
"Return a commented form of the given line"
[313] Fix | Delete
line = line.rstrip()
[314] Fix | Delete
if line:
[315] Fix | Delete
return '# '+line
[316] Fix | Delete
else:
[317] Fix | Delete
return '#'
[318] Fix | Delete
[319] Fix | Delete
def _strip_exception_details(msg):
[320] Fix | Delete
# Support for IGNORE_EXCEPTION_DETAIL.
[321] Fix | Delete
# Get rid of everything except the exception name; in particular, drop
[322] Fix | Delete
# the possibly dotted module path (if any) and the exception message (if
[323] Fix | Delete
# any). We assume that a colon is never part of a dotted name, or of an
[324] Fix | Delete
# exception name.
[325] Fix | Delete
# E.g., given
[326] Fix | Delete
# "foo.bar.MyError: la di da"
[327] Fix | Delete
# return "MyError"
[328] Fix | Delete
# Or for "abc.def" or "abc.def:\n" return "def".
[329] Fix | Delete
[330] Fix | Delete
start, end = 0, len(msg)
[331] Fix | Delete
# The exception name must appear on the first line.
[332] Fix | Delete
i = msg.find("\n")
[333] Fix | Delete
if i >= 0:
[334] Fix | Delete
end = i
[335] Fix | Delete
# retain up to the first colon (if any)
[336] Fix | Delete
i = msg.find(':', 0, end)
[337] Fix | Delete
if i >= 0:
[338] Fix | Delete
end = i
[339] Fix | Delete
# retain just the exception name
[340] Fix | Delete
i = msg.rfind('.', 0, end)
[341] Fix | Delete
if i >= 0:
[342] Fix | Delete
start = i+1
[343] Fix | Delete
return msg[start: end]
[344] Fix | Delete
[345] Fix | Delete
class _OutputRedirectingPdb(pdb.Pdb):
[346] Fix | Delete
"""
[347] Fix | Delete
A specialized version of the python debugger that redirects stdout
[348] Fix | Delete
to a given stream when interacting with the user. Stdout is *not*
[349] Fix | Delete
redirected when traced code is executed.
[350] Fix | Delete
"""
[351] Fix | Delete
def __init__(self, out):
[352] Fix | Delete
self.__out = out
[353] Fix | Delete
self.__debugger_used = False
[354] Fix | Delete
# do not play signal games in the pdb
[355] Fix | Delete
pdb.Pdb.__init__(self, stdout=out, nosigint=True)
[356] Fix | Delete
# still use input() to get user input
[357] Fix | Delete
self.use_rawinput = 1
[358] Fix | Delete
[359] Fix | Delete
def set_trace(self, frame=None):
[360] Fix | Delete
self.__debugger_used = True
[361] Fix | Delete
if frame is None:
[362] Fix | Delete
frame = sys._getframe().f_back
[363] Fix | Delete
pdb.Pdb.set_trace(self, frame)
[364] Fix | Delete
[365] Fix | Delete
def set_continue(self):
[366] Fix | Delete
# Calling set_continue unconditionally would break unit test
[367] Fix | Delete
# coverage reporting, as Bdb.set_continue calls sys.settrace(None).
[368] Fix | Delete
if self.__debugger_used:
[369] Fix | Delete
pdb.Pdb.set_continue(self)
[370] Fix | Delete
[371] Fix | Delete
def trace_dispatch(self, *args):
[372] Fix | Delete
# Redirect stdout to the given stream.
[373] Fix | Delete
save_stdout = sys.stdout
[374] Fix | Delete
sys.stdout = self.__out
[375] Fix | Delete
# Call Pdb's trace dispatch method.
[376] Fix | Delete
try:
[377] Fix | Delete
return pdb.Pdb.trace_dispatch(self, *args)
[378] Fix | Delete
finally:
[379] Fix | Delete
sys.stdout = save_stdout
[380] Fix | Delete
[381] Fix | Delete
# [XX] Normalize with respect to os.path.pardir?
[382] Fix | Delete
def _module_relative_path(module, test_path):
[383] Fix | Delete
if not inspect.ismodule(module):
[384] Fix | Delete
raise TypeError('Expected a module: %r' % module)
[385] Fix | Delete
if test_path.startswith('/'):
[386] Fix | Delete
raise ValueError('Module-relative files may not have absolute paths')
[387] Fix | Delete
[388] Fix | Delete
# Normalize the path. On Windows, replace "/" with "\".
[389] Fix | Delete
test_path = os.path.join(*(test_path.split('/')))
[390] Fix | Delete
[391] Fix | Delete
# Find the base directory for the path.
[392] Fix | Delete
if hasattr(module, '__file__'):
[393] Fix | Delete
# A normal module/package
[394] Fix | Delete
basedir = os.path.split(module.__file__)[0]
[395] Fix | Delete
elif module.__name__ == '__main__':
[396] Fix | Delete
# An interactive session.
[397] Fix | Delete
if len(sys.argv)>0 and sys.argv[0] != '':
[398] Fix | Delete
basedir = os.path.split(sys.argv[0])[0]
[399] Fix | Delete
else:
[400] Fix | Delete
basedir = os.curdir
[401] Fix | Delete
else:
[402] Fix | Delete
if hasattr(module, '__path__'):
[403] Fix | Delete
for directory in module.__path__:
[404] Fix | Delete
fullpath = os.path.join(directory, test_path)
[405] Fix | Delete
if os.path.exists(fullpath):
[406] Fix | Delete
return fullpath
[407] Fix | Delete
[408] Fix | Delete
# A module w/o __file__ (this includes builtins)
[409] Fix | Delete
raise ValueError("Can't resolve paths relative to the module "
[410] Fix | Delete
"%r (it has no __file__)"
[411] Fix | Delete
% module.__name__)
[412] Fix | Delete
[413] Fix | Delete
# Combine the base directory and the test path.
[414] Fix | Delete
return os.path.join(basedir, test_path)
[415] Fix | Delete
[416] Fix | Delete
######################################################################
[417] Fix | Delete
## 2. Example & DocTest
[418] Fix | Delete
######################################################################
[419] Fix | Delete
## - An "example" is a <source, want> pair, where "source" is a
[420] Fix | Delete
## fragment of source code, and "want" is the expected output for
[421] Fix | Delete
## "source." The Example class also includes information about
[422] Fix | Delete
## where the example was extracted from.
[423] Fix | Delete
##
[424] Fix | Delete
## - A "doctest" is a collection of examples, typically extracted from
[425] Fix | Delete
## a string (such as an object's docstring). The DocTest class also
[426] Fix | Delete
## includes information about where the string was extracted from.
[427] Fix | Delete
[428] Fix | Delete
class Example:
[429] Fix | Delete
"""
[430] Fix | Delete
A single doctest example, consisting of source code and expected
[431] Fix | Delete
output. `Example` defines the following attributes:
[432] Fix | Delete
[433] Fix | Delete
- source: A single Python statement, always ending with a newline.
[434] Fix | Delete
The constructor adds a newline if needed.
[435] Fix | Delete
[436] Fix | Delete
- want: The expected output from running the source code (either
[437] Fix | Delete
from stdout, or a traceback in case of exception). `want` ends
[438] Fix | Delete
with a newline unless it's empty, in which case it's an empty
[439] Fix | Delete
string. The constructor adds a newline if needed.
[440] Fix | Delete
[441] Fix | Delete
- exc_msg: The exception message generated by the example, if
[442] Fix | Delete
the example is expected to generate an exception; or `None` if
[443] Fix | Delete
it is not expected to generate an exception. This exception
[444] Fix | Delete
message is compared against the return value of
[445] Fix | Delete
`traceback.format_exception_only()`. `exc_msg` ends with a
[446] Fix | Delete
newline unless it's `None`. The constructor adds a newline
[447] Fix | Delete
if needed.
[448] Fix | Delete
[449] Fix | Delete
- lineno: The line number within the DocTest string containing
[450] Fix | Delete
this Example where the Example begins. This line number is
[451] Fix | Delete
zero-based, with respect to the beginning of the DocTest.
[452] Fix | Delete
[453] Fix | Delete
- indent: The example's indentation in the DocTest string.
[454] Fix | Delete
I.e., the number of space characters that precede the
[455] Fix | Delete
example's first prompt.
[456] Fix | Delete
[457] Fix | Delete
- options: A dictionary mapping from option flags to True or
[458] Fix | Delete
False, which is used to override default options for this
[459] Fix | Delete
example. Any option flags not contained in this dictionary
[460] Fix | Delete
are left at their default value (as specified by the
[461] Fix | Delete
DocTestRunner's optionflags). By default, no options are set.
[462] Fix | Delete
"""
[463] Fix | Delete
def __init__(self, source, want, exc_msg=None, lineno=0, indent=0,
[464] Fix | Delete
options=None):
[465] Fix | Delete
# Normalize inputs.
[466] Fix | Delete
if not source.endswith('\n'):
[467] Fix | Delete
source += '\n'
[468] Fix | Delete
if want and not want.endswith('\n'):
[469] Fix | Delete
want += '\n'
[470] Fix | Delete
if exc_msg is not None and not exc_msg.endswith('\n'):
[471] Fix | Delete
exc_msg += '\n'
[472] Fix | Delete
# Store properties.
[473] Fix | Delete
self.source = source
[474] Fix | Delete
self.want = want
[475] Fix | Delete
self.lineno = lineno
[476] Fix | Delete
self.indent = indent
[477] Fix | Delete
if options is None: options = {}
[478] Fix | Delete
self.options = options
[479] Fix | Delete
self.exc_msg = exc_msg
[480] Fix | Delete
[481] Fix | Delete
def __eq__(self, other):
[482] Fix | Delete
if type(self) is not type(other):
[483] Fix | Delete
return NotImplemented
[484] Fix | Delete
[485] Fix | Delete
return self.source == other.source and \
[486] Fix | Delete
self.want == other.want and \
[487] Fix | Delete
self.lineno == other.lineno and \
[488] Fix | Delete
self.indent == other.indent and \
[489] Fix | Delete
self.options == other.options and \
[490] Fix | Delete
self.exc_msg == other.exc_msg
[491] Fix | Delete
[492] Fix | Delete
def __hash__(self):
[493] Fix | Delete
return hash((self.source, self.want, self.lineno, self.indent,
[494] Fix | Delete
self.exc_msg))
[495] Fix | Delete
[496] Fix | Delete
class DocTest:
[497] Fix | Delete
"""
[498] Fix | Delete
A collection of doctest examples that should be run in a single
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function