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