/usr/lib/python2.7/dist-packages/zope/testing/doctest/__init__.py is in python-zope.testing 4.1.2-0ubuntu7.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | __all__ = [
# 0, Option Flags
'register_optionflag',
'DONT_ACCEPT_TRUE_FOR_1',
'DONT_ACCEPT_BLANKLINE',
'NORMALIZE_WHITESPACE',
'ELLIPSIS',
'IGNORE_EXCEPTION_DETAIL',
'COMPARISON_FLAGS',
'REPORT_UDIFF',
'REPORT_CDIFF',
'REPORT_NDIFF',
'REPORT_ONLY_FIRST_FAILURE',
'REPORTING_FLAGS',
# 1. Utility Functions
# 2. Example & DocTest
'Example',
'DocTest',
# 3. Doctest Parser
'DocTestParser',
# 4. Doctest Finder
'DocTestFinder',
# 5. Doctest Runner
'DocTestRunner',
'OutputChecker',
'DocTestFailure',
'UnexpectedException',
'DebugRunner',
# 6. Test Functions
'testmod',
'testfile',
'run_docstring_examples',
# 7. Tester
'Tester',
# 8. Unittest Support
'DocTestSuite',
'DocFileSuite',
'DocFileTest',
'set_unittest_reportflags',
# 9. Debugging Support
'script_from_examples',
'testsource',
'debug_src',
'debug',
]
import sys
if sys.version > '2.5':
__all__.append('SKIP')
# Tell people to use the builtin module instead.
import warnings
warnings.warn('zope.testing.doctest is deprecated in favour of '
'the Python standard library doctest module', DeprecationWarning,
stacklevel=2)
# Patch to fix an error that makes subsequent tests fail after you have
# returned unicode in a test. This is obviously not an issue in Python 3.
# Reported as #8471: http://bugs.python.org/issue8471
import doctest
if sys.version < '3':
_org_SpoofOut = doctest._SpoofOut
class _patched_SpoofOut(_org_SpoofOut):
def truncate(self, size=None):
_org_SpoofOut.truncate(self, size)
if not self.buf:
self.buf = ''
doctest._SpoofOut = _patched_SpoofOut
# Patching a unicode error that has been fixed in Python 2.6.5:
import sys
if sys.version < '2.6.5':
import re
doctest._encoding = getattr(sys.__stdout__, 'encoding', None) or 'utf-8'
def _indent(s, indent=4):
"""
Add the given number of space characters to the beginning of
every non-blank line in `s`, and return the result.
If the string `s` is Unicode, it is encoded using the stdout
encoding and the `backslashreplace` error handler.
"""
if isinstance(s, unicode):
s = s.encode(doctest._encoding, 'backslashreplace')
# This regexp matches the start of non-blank lines:
return re.sub('(?m)^(?!$)', indent*' ', s)
doctest._indent = _indent
# Patch to fix tests that has mixed line endings:
# Reported as #8473: http://bugs.python.org/issue8473
import os
if sys.version < '2.5':
from doctest import DocTestParser, master
def _patched_testfile(filename, module_relative=True, name=None, package=None,
globs=None, verbose=None, report=True, optionflags=0,
extraglobs=None, raise_on_error=False, parser=DocTestParser()):
global master
if package and not module_relative:
raise ValueError("Package may only be specified for module-"
"relative paths.")
# Relativize the path
if module_relative:
package = _normalize_module(package)
filename = _module_relative_path(package, filename)
# If no name was given, then use the file's name.
if name is None:
name = os.path.basename(filename)
# Assemble the globals.
if globs is None:
globs = {}
else:
globs = globs.copy()
if extraglobs is not None:
globs.update(extraglobs)
if raise_on_error:
runner = DebugRunner(verbose=verbose, optionflags=optionflags)
else:
runner = DocTestRunner(verbose=verbose, optionflags=optionflags)
# Read the file, convert it to a test, and run it.
s = open(filename, 'U').read()
test = parser.get_doctest(s, globs, name, filename, 0)
runner.run(test)
if report:
runner.summarize()
if master is None:
master = runner
else:
master.merge(runner)
return runner.failures, runner.tries
doctest.testfile = _patched_testfile
from doctest import _normalize_module, _module_relative_path, DocFileCase
def _patched_DocFileTest(path, module_relative=True, package=None,
globs=None, parser=DocTestParser(), **options):
if globs is None:
globs = {}
if package and not module_relative:
raise ValueError("Package may only be specified for module-"
"relative paths.")
# Relativize the path.
if module_relative:
package = _normalize_module(package)
path = _module_relative_path(package, path)
# Find the file and read it.
name = os.path.basename(path)
doc = open(path, 'U').read()
# Convert it to a test, and wrap it in a DocFileCase.
test = parser.get_doctest(doc, globs, name, path, 0)
return DocFileCase(test, **options)
doctest.DocFileTest = _patched_DocFileTest
else:
def _patched_load_testfile(filename, package, module_relative, encoding=None):
if module_relative:
package = doctest._normalize_module(package, 3)
filename = doctest._module_relative_path(package, filename)
if hasattr(package, '__loader__'):
if hasattr(package.__loader__, 'get_data'):
file_contents = package.__loader__.get_data(filename)
if encoding is not None: # Python 3
file_contents = file_contents.decode(encoding)
# get_data() opens files as 'rb', so one must do the equivalent
# conversion as universal newlines would do.
return file_contents.replace(os.linesep, '\n'), filename
if encoding: # Python 3:
return open(filename, encoding=encoding).read(), filename
else:
return open(filename, 'U').read(), filename
doctest._load_testfile = _patched_load_testfile
# Patch to let the doctest have the globals of the testcase. This is slightly
# evil, but Zopes doctests did this, and if we change it everything breaks.
import unittest
def _patched_setUp(self):
test = self._dt_test
self._dt_globs = test.globs.copy()
if self._dt_setUp is not None:
self._dt_setUp(test)
def _patched_tearDown(self):
test = self._dt_test
if self._dt_tearDown is not None:
self._dt_tearDown(test)
test.globs.clear()
test.globs.update(self._dt_globs)
doctest.DocTestCase.setUp = _patched_setUp
doctest.DocTestCase.tearDown = _patched_tearDown
# Patch so you can set REPORT_ONLY_FIRST_FAILURE even if you have a DIFF flag
# on the test.
import sys
from StringIO import StringIO
def _patched_runTest(self):
test = self._dt_test
old = sys.stdout
new = StringIO()
optionflags = self._dt_optionflags
if not (optionflags & doctest.REPORTING_FLAGS):
# The option flags don't include any reporting flags,
# so add the default reporting flags
optionflags |= doctest._unittest_reportflags
# This should work even if you have a diff flag:
if doctest._unittest_reportflags & doctest.REPORT_ONLY_FIRST_FAILURE:
optionflags |= doctest.REPORT_ONLY_FIRST_FAILURE
runner = DocTestRunner(optionflags=optionflags,
checker=self._dt_checker, verbose=False)
try:
runner.DIVIDER = "-"*70
failures, tries = runner.run(
test, out=new.write, clear_globs=False)
finally:
sys.stdout = old
if failures:
raise self.failureException(self.format_failure(new.getvalue()))
doctest.DocTestCase.runTest = _patched_runTest
from doctest import *
from doctest import DocFileTest # Not in doctests.__all__ for some reason.
|