Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../usr/lib64/python3....
File: doctest.py
if not isinstance(valname, str):
[1000] Fix | Delete
raise ValueError("DocTestFinder.find: __test__ keys "
[1001] Fix | Delete
"must be strings: %r" %
[1002] Fix | Delete
(type(valname),))
[1003] Fix | Delete
if not (inspect.isroutine(val) or inspect.isclass(val) or
[1004] Fix | Delete
inspect.ismodule(val) or isinstance(val, str)):
[1005] Fix | Delete
raise ValueError("DocTestFinder.find: __test__ values "
[1006] Fix | Delete
"must be strings, functions, methods, "
[1007] Fix | Delete
"classes, or modules: %r" %
[1008] Fix | Delete
(type(val),))
[1009] Fix | Delete
valname = '%s.__test__.%s' % (name, valname)
[1010] Fix | Delete
self._find(tests, val, valname, module, source_lines,
[1011] Fix | Delete
globs, seen)
[1012] Fix | Delete
[1013] Fix | Delete
# Look for tests in a class's contained objects.
[1014] Fix | Delete
if inspect.isclass(obj) and self._recurse:
[1015] Fix | Delete
for valname, val in obj.__dict__.items():
[1016] Fix | Delete
# Special handling for staticmethod/classmethod.
[1017] Fix | Delete
if isinstance(val, staticmethod):
[1018] Fix | Delete
val = getattr(obj, valname)
[1019] Fix | Delete
if isinstance(val, classmethod):
[1020] Fix | Delete
val = getattr(obj, valname).__func__
[1021] Fix | Delete
[1022] Fix | Delete
# Recurse to methods, properties, and nested classes.
[1023] Fix | Delete
if ((inspect.isroutine(val) or inspect.isclass(val) or
[1024] Fix | Delete
isinstance(val, property)) and
[1025] Fix | Delete
self._from_module(module, val)):
[1026] Fix | Delete
valname = '%s.%s' % (name, valname)
[1027] Fix | Delete
self._find(tests, val, valname, module, source_lines,
[1028] Fix | Delete
globs, seen)
[1029] Fix | Delete
[1030] Fix | Delete
def _get_test(self, obj, name, module, globs, source_lines):
[1031] Fix | Delete
"""
[1032] Fix | Delete
Return a DocTest for the given object, if it defines a docstring;
[1033] Fix | Delete
otherwise, return None.
[1034] Fix | Delete
"""
[1035] Fix | Delete
# Extract the object's docstring. If it doesn't have one,
[1036] Fix | Delete
# then return None (no test for this object).
[1037] Fix | Delete
if isinstance(obj, str):
[1038] Fix | Delete
docstring = obj
[1039] Fix | Delete
else:
[1040] Fix | Delete
try:
[1041] Fix | Delete
if obj.__doc__ is None:
[1042] Fix | Delete
docstring = ''
[1043] Fix | Delete
else:
[1044] Fix | Delete
docstring = obj.__doc__
[1045] Fix | Delete
if not isinstance(docstring, str):
[1046] Fix | Delete
docstring = str(docstring)
[1047] Fix | Delete
except (TypeError, AttributeError):
[1048] Fix | Delete
docstring = ''
[1049] Fix | Delete
[1050] Fix | Delete
# Find the docstring's location in the file.
[1051] Fix | Delete
lineno = self._find_lineno(obj, source_lines)
[1052] Fix | Delete
[1053] Fix | Delete
# Don't bother if the docstring is empty.
[1054] Fix | Delete
if self._exclude_empty and not docstring:
[1055] Fix | Delete
return None
[1056] Fix | Delete
[1057] Fix | Delete
# Return a DocTest for this object.
[1058] Fix | Delete
if module is None:
[1059] Fix | Delete
filename = None
[1060] Fix | Delete
else:
[1061] Fix | Delete
filename = getattr(module, '__file__', module.__name__)
[1062] Fix | Delete
if filename[-4:] == ".pyc":
[1063] Fix | Delete
filename = filename[:-1]
[1064] Fix | Delete
return self._parser.get_doctest(docstring, globs, name,
[1065] Fix | Delete
filename, lineno)
[1066] Fix | Delete
[1067] Fix | Delete
def _find_lineno(self, obj, source_lines):
[1068] Fix | Delete
"""
[1069] Fix | Delete
Return a line number of the given object's docstring. Note:
[1070] Fix | Delete
this method assumes that the object has a docstring.
[1071] Fix | Delete
"""
[1072] Fix | Delete
lineno = None
[1073] Fix | Delete
[1074] Fix | Delete
# Find the line number for modules.
[1075] Fix | Delete
if inspect.ismodule(obj):
[1076] Fix | Delete
lineno = 0
[1077] Fix | Delete
[1078] Fix | Delete
# Find the line number for classes.
[1079] Fix | Delete
# Note: this could be fooled if a class is defined multiple
[1080] Fix | Delete
# times in a single file.
[1081] Fix | Delete
if inspect.isclass(obj):
[1082] Fix | Delete
if source_lines is None:
[1083] Fix | Delete
return None
[1084] Fix | Delete
pat = re.compile(r'^\s*class\s*%s\b' %
[1085] Fix | Delete
getattr(obj, '__name__', '-'))
[1086] Fix | Delete
for i, line in enumerate(source_lines):
[1087] Fix | Delete
if pat.match(line):
[1088] Fix | Delete
lineno = i
[1089] Fix | Delete
break
[1090] Fix | Delete
[1091] Fix | Delete
# Find the line number for functions & methods.
[1092] Fix | Delete
if inspect.ismethod(obj): obj = obj.__func__
[1093] Fix | Delete
if inspect.isfunction(obj): obj = obj.__code__
[1094] Fix | Delete
if inspect.istraceback(obj): obj = obj.tb_frame
[1095] Fix | Delete
if inspect.isframe(obj): obj = obj.f_code
[1096] Fix | Delete
if inspect.iscode(obj):
[1097] Fix | Delete
lineno = getattr(obj, 'co_firstlineno', None)-1
[1098] Fix | Delete
[1099] Fix | Delete
# Find the line number where the docstring starts. Assume
[1100] Fix | Delete
# that it's the first line that begins with a quote mark.
[1101] Fix | Delete
# Note: this could be fooled by a multiline function
[1102] Fix | Delete
# signature, where a continuation line begins with a quote
[1103] Fix | Delete
# mark.
[1104] Fix | Delete
if lineno is not None:
[1105] Fix | Delete
if source_lines is None:
[1106] Fix | Delete
return lineno+1
[1107] Fix | Delete
pat = re.compile(r'(^|.*:)\s*\w*("|\')')
[1108] Fix | Delete
for lineno in range(lineno, len(source_lines)):
[1109] Fix | Delete
if pat.match(source_lines[lineno]):
[1110] Fix | Delete
return lineno
[1111] Fix | Delete
[1112] Fix | Delete
# We couldn't find the line number.
[1113] Fix | Delete
return None
[1114] Fix | Delete
[1115] Fix | Delete
######################################################################
[1116] Fix | Delete
## 5. DocTest Runner
[1117] Fix | Delete
######################################################################
[1118] Fix | Delete
[1119] Fix | Delete
class DocTestRunner:
[1120] Fix | Delete
"""
[1121] Fix | Delete
A class used to run DocTest test cases, and accumulate statistics.
[1122] Fix | Delete
The `run` method is used to process a single DocTest case. It
[1123] Fix | Delete
returns a tuple `(f, t)`, where `t` is the number of test cases
[1124] Fix | Delete
tried, and `f` is the number of test cases that failed.
[1125] Fix | Delete
[1126] Fix | Delete
>>> tests = DocTestFinder().find(_TestClass)
[1127] Fix | Delete
>>> runner = DocTestRunner(verbose=False)
[1128] Fix | Delete
>>> tests.sort(key = lambda test: test.name)
[1129] Fix | Delete
>>> for test in tests:
[1130] Fix | Delete
... print(test.name, '->', runner.run(test))
[1131] Fix | Delete
_TestClass -> TestResults(failed=0, attempted=2)
[1132] Fix | Delete
_TestClass.__init__ -> TestResults(failed=0, attempted=2)
[1133] Fix | Delete
_TestClass.get -> TestResults(failed=0, attempted=2)
[1134] Fix | Delete
_TestClass.square -> TestResults(failed=0, attempted=1)
[1135] Fix | Delete
[1136] Fix | Delete
The `summarize` method prints a summary of all the test cases that
[1137] Fix | Delete
have been run by the runner, and returns an aggregated `(f, t)`
[1138] Fix | Delete
tuple:
[1139] Fix | Delete
[1140] Fix | Delete
>>> runner.summarize(verbose=1)
[1141] Fix | Delete
4 items passed all tests:
[1142] Fix | Delete
2 tests in _TestClass
[1143] Fix | Delete
2 tests in _TestClass.__init__
[1144] Fix | Delete
2 tests in _TestClass.get
[1145] Fix | Delete
1 tests in _TestClass.square
[1146] Fix | Delete
7 tests in 4 items.
[1147] Fix | Delete
7 passed and 0 failed.
[1148] Fix | Delete
Test passed.
[1149] Fix | Delete
TestResults(failed=0, attempted=7)
[1150] Fix | Delete
[1151] Fix | Delete
The aggregated number of tried examples and failed examples is
[1152] Fix | Delete
also available via the `tries` and `failures` attributes:
[1153] Fix | Delete
[1154] Fix | Delete
>>> runner.tries
[1155] Fix | Delete
7
[1156] Fix | Delete
>>> runner.failures
[1157] Fix | Delete
0
[1158] Fix | Delete
[1159] Fix | Delete
The comparison between expected outputs and actual outputs is done
[1160] Fix | Delete
by an `OutputChecker`. This comparison may be customized with a
[1161] Fix | Delete
number of option flags; see the documentation for `testmod` for
[1162] Fix | Delete
more information. If the option flags are insufficient, then the
[1163] Fix | Delete
comparison may also be customized by passing a subclass of
[1164] Fix | Delete
`OutputChecker` to the constructor.
[1165] Fix | Delete
[1166] Fix | Delete
The test runner's display output can be controlled in two ways.
[1167] Fix | Delete
First, an output function (`out) can be passed to
[1168] Fix | Delete
`TestRunner.run`; this function will be called with strings that
[1169] Fix | Delete
should be displayed. It defaults to `sys.stdout.write`. If
[1170] Fix | Delete
capturing the output is not sufficient, then the display output
[1171] Fix | Delete
can be also customized by subclassing DocTestRunner, and
[1172] Fix | Delete
overriding the methods `report_start`, `report_success`,
[1173] Fix | Delete
`report_unexpected_exception`, and `report_failure`.
[1174] Fix | Delete
"""
[1175] Fix | Delete
# This divider string is used to separate failure messages, and to
[1176] Fix | Delete
# separate sections of the summary.
[1177] Fix | Delete
DIVIDER = "*" * 70
[1178] Fix | Delete
[1179] Fix | Delete
def __init__(self, checker=None, verbose=None, optionflags=0):
[1180] Fix | Delete
"""
[1181] Fix | Delete
Create a new test runner.
[1182] Fix | Delete
[1183] Fix | Delete
Optional keyword arg `checker` is the `OutputChecker` that
[1184] Fix | Delete
should be used to compare the expected outputs and actual
[1185] Fix | Delete
outputs of doctest examples.
[1186] Fix | Delete
[1187] Fix | Delete
Optional keyword arg 'verbose' prints lots of stuff if true,
[1188] Fix | Delete
only failures if false; by default, it's true iff '-v' is in
[1189] Fix | Delete
sys.argv.
[1190] Fix | Delete
[1191] Fix | Delete
Optional argument `optionflags` can be used to control how the
[1192] Fix | Delete
test runner compares expected output to actual output, and how
[1193] Fix | Delete
it displays failures. See the documentation for `testmod` for
[1194] Fix | Delete
more information.
[1195] Fix | Delete
"""
[1196] Fix | Delete
self._checker = checker or OutputChecker()
[1197] Fix | Delete
if verbose is None:
[1198] Fix | Delete
verbose = '-v' in sys.argv
[1199] Fix | Delete
self._verbose = verbose
[1200] Fix | Delete
self.optionflags = optionflags
[1201] Fix | Delete
self.original_optionflags = optionflags
[1202] Fix | Delete
[1203] Fix | Delete
# Keep track of the examples we've run.
[1204] Fix | Delete
self.tries = 0
[1205] Fix | Delete
self.failures = 0
[1206] Fix | Delete
self._name2ft = {}
[1207] Fix | Delete
[1208] Fix | Delete
# Create a fake output target for capturing doctest output.
[1209] Fix | Delete
self._fakeout = _SpoofOut()
[1210] Fix | Delete
[1211] Fix | Delete
#/////////////////////////////////////////////////////////////////
[1212] Fix | Delete
# Reporting methods
[1213] Fix | Delete
#/////////////////////////////////////////////////////////////////
[1214] Fix | Delete
[1215] Fix | Delete
def report_start(self, out, test, example):
[1216] Fix | Delete
"""
[1217] Fix | Delete
Report that the test runner is about to process the given
[1218] Fix | Delete
example. (Only displays a message if verbose=True)
[1219] Fix | Delete
"""
[1220] Fix | Delete
if self._verbose:
[1221] Fix | Delete
if example.want:
[1222] Fix | Delete
out('Trying:\n' + _indent(example.source) +
[1223] Fix | Delete
'Expecting:\n' + _indent(example.want))
[1224] Fix | Delete
else:
[1225] Fix | Delete
out('Trying:\n' + _indent(example.source) +
[1226] Fix | Delete
'Expecting nothing\n')
[1227] Fix | Delete
[1228] Fix | Delete
def report_success(self, out, test, example, got):
[1229] Fix | Delete
"""
[1230] Fix | Delete
Report that the given example ran successfully. (Only
[1231] Fix | Delete
displays a message if verbose=True)
[1232] Fix | Delete
"""
[1233] Fix | Delete
if self._verbose:
[1234] Fix | Delete
out("ok\n")
[1235] Fix | Delete
[1236] Fix | Delete
def report_failure(self, out, test, example, got):
[1237] Fix | Delete
"""
[1238] Fix | Delete
Report that the given example failed.
[1239] Fix | Delete
"""
[1240] Fix | Delete
out(self._failure_header(test, example) +
[1241] Fix | Delete
self._checker.output_difference(example, got, self.optionflags))
[1242] Fix | Delete
[1243] Fix | Delete
def report_unexpected_exception(self, out, test, example, exc_info):
[1244] Fix | Delete
"""
[1245] Fix | Delete
Report that the given example raised an unexpected exception.
[1246] Fix | Delete
"""
[1247] Fix | Delete
out(self._failure_header(test, example) +
[1248] Fix | Delete
'Exception raised:\n' + _indent(_exception_traceback(exc_info)))
[1249] Fix | Delete
[1250] Fix | Delete
def _failure_header(self, test, example):
[1251] Fix | Delete
out = [self.DIVIDER]
[1252] Fix | Delete
if test.filename:
[1253] Fix | Delete
if test.lineno is not None and example.lineno is not None:
[1254] Fix | Delete
lineno = test.lineno + example.lineno + 1
[1255] Fix | Delete
else:
[1256] Fix | Delete
lineno = '?'
[1257] Fix | Delete
out.append('File "%s", line %s, in %s' %
[1258] Fix | Delete
(test.filename, lineno, test.name))
[1259] Fix | Delete
else:
[1260] Fix | Delete
out.append('Line %s, in %s' % (example.lineno+1, test.name))
[1261] Fix | Delete
out.append('Failed example:')
[1262] Fix | Delete
source = example.source
[1263] Fix | Delete
out.append(_indent(source))
[1264] Fix | Delete
return '\n'.join(out)
[1265] Fix | Delete
[1266] Fix | Delete
#/////////////////////////////////////////////////////////////////
[1267] Fix | Delete
# DocTest Running
[1268] Fix | Delete
#/////////////////////////////////////////////////////////////////
[1269] Fix | Delete
[1270] Fix | Delete
def __run(self, test, compileflags, out):
[1271] Fix | Delete
"""
[1272] Fix | Delete
Run the examples in `test`. Write the outcome of each example
[1273] Fix | Delete
with one of the `DocTestRunner.report_*` methods, using the
[1274] Fix | Delete
writer function `out`. `compileflags` is the set of compiler
[1275] Fix | Delete
flags that should be used to execute examples. Return a tuple
[1276] Fix | Delete
`(f, t)`, where `t` is the number of examples tried, and `f`
[1277] Fix | Delete
is the number of examples that failed. The examples are run
[1278] Fix | Delete
in the namespace `test.globs`.
[1279] Fix | Delete
"""
[1280] Fix | Delete
# Keep track of the number of failures and tries.
[1281] Fix | Delete
failures = tries = 0
[1282] Fix | Delete
[1283] Fix | Delete
# Save the option flags (since option directives can be used
[1284] Fix | Delete
# to modify them).
[1285] Fix | Delete
original_optionflags = self.optionflags
[1286] Fix | Delete
[1287] Fix | Delete
SUCCESS, FAILURE, BOOM = range(3) # `outcome` state
[1288] Fix | Delete
[1289] Fix | Delete
check = self._checker.check_output
[1290] Fix | Delete
[1291] Fix | Delete
# Process each example.
[1292] Fix | Delete
for examplenum, example in enumerate(test.examples):
[1293] Fix | Delete
[1294] Fix | Delete
# If REPORT_ONLY_FIRST_FAILURE is set, then suppress
[1295] Fix | Delete
# reporting after the first failure.
[1296] Fix | Delete
quiet = (self.optionflags & REPORT_ONLY_FIRST_FAILURE and
[1297] Fix | Delete
failures > 0)
[1298] Fix | Delete
[1299] Fix | Delete
# Merge in the example's options.
[1300] Fix | Delete
self.optionflags = original_optionflags
[1301] Fix | Delete
if example.options:
[1302] Fix | Delete
for (optionflag, val) in example.options.items():
[1303] Fix | Delete
if val:
[1304] Fix | Delete
self.optionflags |= optionflag
[1305] Fix | Delete
else:
[1306] Fix | Delete
self.optionflags &= ~optionflag
[1307] Fix | Delete
[1308] Fix | Delete
# If 'SKIP' is set, then skip this example.
[1309] Fix | Delete
if self.optionflags & SKIP:
[1310] Fix | Delete
continue
[1311] Fix | Delete
[1312] Fix | Delete
# Record that we started this example.
[1313] Fix | Delete
tries += 1
[1314] Fix | Delete
if not quiet:
[1315] Fix | Delete
self.report_start(out, test, example)
[1316] Fix | Delete
[1317] Fix | Delete
# Use a special filename for compile(), so we can retrieve
[1318] Fix | Delete
# the source code during interactive debugging (see
[1319] Fix | Delete
# __patched_linecache_getlines).
[1320] Fix | Delete
filename = '<doctest %s[%d]>' % (test.name, examplenum)
[1321] Fix | Delete
[1322] Fix | Delete
# Run the example in the given context (globs), and record
[1323] Fix | Delete
# any exception that gets raised. (But don't intercept
[1324] Fix | Delete
# keyboard interrupts.)
[1325] Fix | Delete
try:
[1326] Fix | Delete
# Don't blink! This is where the user's code gets run.
[1327] Fix | Delete
exec(compile(example.source, filename, "single",
[1328] Fix | Delete
compileflags, 1), test.globs)
[1329] Fix | Delete
self.debugger.set_continue() # ==== Example Finished ====
[1330] Fix | Delete
exception = None
[1331] Fix | Delete
except KeyboardInterrupt:
[1332] Fix | Delete
raise
[1333] Fix | Delete
except:
[1334] Fix | Delete
exception = sys.exc_info()
[1335] Fix | Delete
self.debugger.set_continue() # ==== Example Finished ====
[1336] Fix | Delete
[1337] Fix | Delete
got = self._fakeout.getvalue() # the actual output
[1338] Fix | Delete
self._fakeout.truncate(0)
[1339] Fix | Delete
outcome = FAILURE # guilty until proved innocent or insane
[1340] Fix | Delete
[1341] Fix | Delete
# If the example executed without raising any exceptions,
[1342] Fix | Delete
# verify its output.
[1343] Fix | Delete
if exception is None:
[1344] Fix | Delete
if check(example.want, got, self.optionflags):
[1345] Fix | Delete
outcome = SUCCESS
[1346] Fix | Delete
[1347] Fix | Delete
# The example raised an exception: check if it was expected.
[1348] Fix | Delete
else:
[1349] Fix | Delete
exc_msg = traceback.format_exception_only(*exception[:2])[-1]
[1350] Fix | Delete
if not quiet:
[1351] Fix | Delete
got += _exception_traceback(exception)
[1352] Fix | Delete
[1353] Fix | Delete
# If `example.exc_msg` is None, then we weren't expecting
[1354] Fix | Delete
# an exception.
[1355] Fix | Delete
if example.exc_msg is None:
[1356] Fix | Delete
outcome = BOOM
[1357] Fix | Delete
[1358] Fix | Delete
# We expected an exception: see whether it matches.
[1359] Fix | Delete
elif check(example.exc_msg, exc_msg, self.optionflags):
[1360] Fix | Delete
outcome = SUCCESS
[1361] Fix | Delete
[1362] Fix | Delete
# Another chance if they didn't care about the detail.
[1363] Fix | Delete
elif self.optionflags & IGNORE_EXCEPTION_DETAIL:
[1364] Fix | Delete
if check(_strip_exception_details(example.exc_msg),
[1365] Fix | Delete
_strip_exception_details(exc_msg),
[1366] Fix | Delete
self.optionflags):
[1367] Fix | Delete
outcome = SUCCESS
[1368] Fix | Delete
[1369] Fix | Delete
# Report the outcome.
[1370] Fix | Delete
if outcome is SUCCESS:
[1371] Fix | Delete
if not quiet:
[1372] Fix | Delete
self.report_success(out, test, example, got)
[1373] Fix | Delete
elif outcome is FAILURE:
[1374] Fix | Delete
if not quiet:
[1375] Fix | Delete
self.report_failure(out, test, example, got)
[1376] Fix | Delete
failures += 1
[1377] Fix | Delete
elif outcome is BOOM:
[1378] Fix | Delete
if not quiet:
[1379] Fix | Delete
self.report_unexpected_exception(out, test, example,
[1380] Fix | Delete
exception)
[1381] Fix | Delete
failures += 1
[1382] Fix | Delete
else:
[1383] Fix | Delete
assert False, ("unknown outcome", outcome)
[1384] Fix | Delete
[1385] Fix | Delete
if failures and self.optionflags & FAIL_FAST:
[1386] Fix | Delete
break
[1387] Fix | Delete
[1388] Fix | Delete
# Restore the option flags (in case they were modified)
[1389] Fix | Delete
self.optionflags = original_optionflags
[1390] Fix | Delete
[1391] Fix | Delete
# Record and return the number of failures and tries.
[1392] Fix | Delete
self.__record_outcome(test, failures, tries)
[1393] Fix | Delete
return TestResults(failures, tries)
[1394] Fix | Delete
[1395] Fix | Delete
def __record_outcome(self, test, f, t):
[1396] Fix | Delete
"""
[1397] Fix | Delete
Record the fact that the given DocTest (`test`) generated `f`
[1398] Fix | Delete
failures out of `t` tried examples.
[1399] Fix | Delete
"""
[1400] Fix | Delete
f2, t2 = self._name2ft.get(test.name, (0,0))
[1401] Fix | Delete
self._name2ft[test.name] = (f+f2, t+t2)
[1402] Fix | Delete
self.failures += f
[1403] Fix | Delete
self.tries += t
[1404] Fix | Delete
[1405] Fix | Delete
__LINECACHE_FILENAME_RE = re.compile(r'<doctest '
[1406] Fix | Delete
r'(?P<name>.+)'
[1407] Fix | Delete
r'\[(?P<examplenum>\d+)\]>$')
[1408] Fix | Delete
def __patched_linecache_getlines(self, filename, module_globals=None):
[1409] Fix | Delete
m = self.__LINECACHE_FILENAME_RE.match(filename)
[1410] Fix | Delete
if m and m.group('name') == self.test.name:
[1411] Fix | Delete
example = self.test.examples[int(m.group('examplenum'))]
[1412] Fix | Delete
return example.source.splitlines(keepends=True)
[1413] Fix | Delete
else:
[1414] Fix | Delete
return self.save_linecache_getlines(filename, module_globals)
[1415] Fix | Delete
[1416] Fix | Delete
def run(self, test, compileflags=None, out=None, clear_globs=True):
[1417] Fix | Delete
"""
[1418] Fix | Delete
Run the examples in `test`, and display the results using the
[1419] Fix | Delete
writer function `out`.
[1420] Fix | Delete
[1421] Fix | Delete
The examples are run in the namespace `test.globs`. If
[1422] Fix | Delete
`clear_globs` is true (the default), then this namespace will
[1423] Fix | Delete
be cleared after the test runs, to help with garbage
[1424] Fix | Delete
collection. If you would like to examine the namespace after
[1425] Fix | Delete
the test completes, then use `clear_globs=False`.
[1426] Fix | Delete
[1427] Fix | Delete
`compileflags` gives the set of flags that should be used by
[1428] Fix | Delete
the Python compiler when running the examples. If not
[1429] Fix | Delete
specified, then it will default to the set of future-import
[1430] Fix | Delete
flags that apply to `globs`.
[1431] Fix | Delete
[1432] Fix | Delete
The output of each example is checked using
[1433] Fix | Delete
`DocTestRunner.check_output`, and the results are formatted by
[1434] Fix | Delete
the `DocTestRunner.report_*` methods.
[1435] Fix | Delete
"""
[1436] Fix | Delete
self.test = test
[1437] Fix | Delete
[1438] Fix | Delete
if compileflags is None:
[1439] Fix | Delete
compileflags = _extract_future_flags(test.globs)
[1440] Fix | Delete
[1441] Fix | Delete
save_stdout = sys.stdout
[1442] Fix | Delete
if out is None:
[1443] Fix | Delete
encoding = save_stdout.encoding
[1444] Fix | Delete
if encoding is None or encoding.lower() == 'utf-8':
[1445] Fix | Delete
out = save_stdout.write
[1446] Fix | Delete
else:
[1447] Fix | Delete
# Use backslashreplace error handling on write
[1448] Fix | Delete
def out(s):
[1449] Fix | Delete
s = str(s.encode(encoding, 'backslashreplace'), encoding)
[1450] Fix | Delete
save_stdout.write(s)
[1451] Fix | Delete
sys.stdout = self._fakeout
[1452] Fix | Delete
[1453] Fix | Delete
# Patch pdb.set_trace to restore sys.stdout during interactive
[1454] Fix | Delete
# debugging (so it's not still redirected to self._fakeout).
[1455] Fix | Delete
# Note that the interactive output will go to *our*
[1456] Fix | Delete
# save_stdout, even if that's not the real sys.stdout; this
[1457] Fix | Delete
# allows us to write test cases for the set_trace behavior.
[1458] Fix | Delete
save_trace = sys.gettrace()
[1459] Fix | Delete
save_set_trace = pdb.set_trace
[1460] Fix | Delete
self.debugger = _OutputRedirectingPdb(save_stdout)
[1461] Fix | Delete
self.debugger.reset()
[1462] Fix | Delete
pdb.set_trace = self.debugger.set_trace
[1463] Fix | Delete
[1464] Fix | Delete
# Patch linecache.getlines, so we can see the example's source
[1465] Fix | Delete
# when we're inside the debugger.
[1466] Fix | Delete
self.save_linecache_getlines = linecache.getlines
[1467] Fix | Delete
linecache.getlines = self.__patched_linecache_getlines
[1468] Fix | Delete
[1469] Fix | Delete
# Make sure sys.displayhook just prints the value to stdout
[1470] Fix | Delete
save_displayhook = sys.displayhook
[1471] Fix | Delete
sys.displayhook = sys.__displayhook__
[1472] Fix | Delete
[1473] Fix | Delete
try:
[1474] Fix | Delete
return self.__run(test, compileflags, out)
[1475] Fix | Delete
finally:
[1476] Fix | Delete
sys.stdout = save_stdout
[1477] Fix | Delete
pdb.set_trace = save_set_trace
[1478] Fix | Delete
sys.settrace(save_trace)
[1479] Fix | Delete
linecache.getlines = self.save_linecache_getlines
[1480] Fix | Delete
sys.displayhook = save_displayhook
[1481] Fix | Delete
if clear_globs:
[1482] Fix | Delete
test.globs.clear()
[1483] Fix | Delete
import builtins
[1484] Fix | Delete
builtins._ = None
[1485] Fix | Delete
[1486] Fix | Delete
#/////////////////////////////////////////////////////////////////
[1487] Fix | Delete
# Summarization
[1488] Fix | Delete
#/////////////////////////////////////////////////////////////////
[1489] Fix | Delete
def summarize(self, verbose=None):
[1490] Fix | Delete
"""
[1491] Fix | Delete
Print a summary of all the test cases that have been run by
[1492] Fix | Delete
this DocTestRunner, and return a tuple `(f, t)`, where `f` is
[1493] Fix | Delete
the total number of failed examples, and `t` is the total
[1494] Fix | Delete
number of tried examples.
[1495] Fix | Delete
[1496] Fix | Delete
The optional `verbose` argument controls how detailed the
[1497] Fix | Delete
summary is. If the verbosity is not specified, then the
[1498] Fix | Delete
DocTestRunner's verbosity is used.
[1499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function