@@ -147,10 +147,13 @@ def __repr__(self):
147147# Option constants.
148148
149149OPTIONFLAGS_BY_NAME = {}
150+
151+
150152def register_optionflag (name ):
151153 # Create a new flag unless `name` is already known.
152154 return OPTIONFLAGS_BY_NAME .setdefault (name , 1 << len (OPTIONFLAGS_BY_NAME ))
153155
156+
154157DONT_ACCEPT_TRUE_FOR_1 = register_optionflag ('DONT_ACCEPT_TRUE_FOR_1' )
155158DONT_ACCEPT_BLANKLINE = register_optionflag ('DONT_ACCEPT_BLANKLINE' )
156159NORMALIZE_WHITESPACE = register_optionflag ('NORMALIZE_WHITESPACE' )
@@ -194,6 +197,7 @@ def register_optionflag(name):
194197# 8. Debugging Support
195198# 9. Example Usage
196199
200+
197201######################################################################
198202## 1. Utility Functions
199203######################################################################
@@ -210,6 +214,7 @@ def _extract_future_flags(globs):
210214 flags |= feature .compiler_flag
211215 return flags
212216
217+
213218def _normalize_module (module , depth = 2 ):
214219 """
215220 Return the module specified by `module`. In particular:
@@ -235,10 +240,12 @@ def _normalize_module(module, depth=2):
235240 else :
236241 raise TypeError ("Expected a module, string, or None" )
237242
243+
238244def _newline_convert (data ):
239245 # The IO module provides a handy decoder for universal newline conversion
240246 return IncrementalNewlineDecoder (None , True ).decode (data , True )
241247
248+
242249def _load_testfile (filename , package , module_relative , encoding ):
243250 if module_relative :
244251 package = _normalize_module (package , 3 )
@@ -257,6 +264,7 @@ def _load_testfile(filename, package, module_relative, encoding):
257264 with open (filename , encoding = encoding ) as f :
258265 return f .read (), filename
259266
267+
260268def _indent (s , indent = 4 ):
261269 """
262270 Add the given number of space characters to the beginning of
@@ -265,6 +273,7 @@ def _indent(s, indent=4):
265273 # This regexp matches the start of non-blank lines:
266274 return re .sub ('(?m)^(?!$)' , indent * ' ' , s )
267275
276+
268277def _exception_traceback (exc_info ):
269278 """
270279 Return a string containing a traceback message for the given
@@ -276,6 +285,7 @@ def _exception_traceback(exc_info):
276285 traceback .print_exception (exc_type , exc_val , exc_tb , file = excout )
277286 return excout .getvalue ()
278287
288+
279289# Override some StringIO methods.
280290class _SpoofOut (StringIO ):
281291 def getvalue (self ):
@@ -291,6 +301,7 @@ def truncate(self, size=None):
291301 self .seek (size )
292302 StringIO .truncate (self )
293303
304+
294305# Worst-case linear-time ellipsis matching.
295306def _ellipsis_match (want , got ):
296307 """
@@ -341,6 +352,7 @@ def _ellipsis_match(want, got):
341352
342353 return True
343354
355+
344356def _comment_line (line ):
345357 "Return a commented form of the given line"
346358 line = line .rstrip ()
@@ -349,6 +361,7 @@ def _comment_line(line):
349361 else :
350362 return '#'
351363
364+
352365def _strip_exception_details (msg ):
353366 # Support for IGNORE_EXCEPTION_DETAIL.
354367 # Get rid of everything except the exception name; in particular, drop
@@ -375,6 +388,7 @@ def _strip_exception_details(msg):
375388 start = i + 1
376389 return msg [start : end ]
377390
391+
378392class _OutputRedirectingPdb (pdb .Pdb ):
379393 """
380394 A specialized version of the python debugger that redirects stdout
@@ -411,6 +425,7 @@ def trace_dispatch(self, *args):
411425 finally :
412426 sys .stdout = save_stdout
413427
428+
414429# [XX] Normalize with respect to os.path.pardir?
415430def _module_relative_path (module , test_path ):
416431 if not inspect .ismodule (module ):
@@ -446,6 +461,7 @@ def _module_relative_path(module, test_path):
446461 # Combine the base directory and the test path.
447462 return os .path .join (basedir , test_path )
448463
464+
449465######################################################################
450466## 2. Example & DocTest
451467######################################################################
@@ -526,6 +542,7 @@ def __hash__(self):
526542 return hash ((self .source , self .want , self .lineno , self .indent ,
527543 self .exc_msg ))
528544
545+
529546class DocTest :
530547 """
531548 A collection of doctest examples that should be run in a single
@@ -599,6 +616,7 @@ def __lt__(self, other):
599616 <
600617 (other .name , other .filename , other_lno , id (other )))
601618
619+
602620######################################################################
603621## 3. DocTestParser
604622######################################################################
@@ -1164,6 +1182,7 @@ def _find_lineno(self, obj, source_lines):
11641182 # We couldn't find the line number.
11651183 return None
11661184
1185+
11671186######################################################################
11681187## 5. DocTest Runner
11691188######################################################################
@@ -1483,6 +1502,7 @@ def __record_outcome(self, test, failures, tries, skips):
14831502 __LINECACHE_FILENAME_RE = re .compile (r'<doctest '
14841503 r'(?P<name>.+)'
14851504 r'\[(?P<examplenum>\d+)\]>$' )
1505+
14861506 def __patched_linecache_getlines (self , filename , module_globals = None ):
14871507 m = self .__LINECACHE_FILENAME_RE .match (filename )
14881508 if m and m .group ('name' ) == self .test .name :
@@ -1819,6 +1839,7 @@ def output_difference(self, example, got, optionflags):
18191839 else :
18201840 return 'Expected nothing\n Got nothing\n '
18211841
1842+
18221843class DocTestFailure (Exception ):
18231844 """A DocTest example has failed in debugging mode.
18241845
@@ -1838,6 +1859,7 @@ def __init__(self, test, example, got):
18381859 def __str__ (self ):
18391860 return str (self .test )
18401861
1862+
18411863class UnexpectedException (Exception ):
18421864 """A DocTest example has encountered an unexpected exception
18431865
@@ -1857,6 +1879,7 @@ def __init__(self, test, example, exc_info):
18571879 def __str__ (self ):
18581880 return str (self .test )
18591881
1882+
18601883class DebugRunner (DocTestRunner ):
18611884 r"""Run doc tests but raise an exception as soon as there is a failure.
18621885
@@ -1960,6 +1983,7 @@ def report_unexpected_exception(self, out, test, example, exc_info):
19601983 def report_failure (self , out , test , example , got ):
19611984 raise DocTestFailure (test , example , got )
19621985
1986+
19631987######################################################################
19641988## 6. Test Functions
19651989######################################################################
@@ -1969,6 +1993,7 @@ def report_failure(self, out, test, example, got):
19691993# class, updated by testmod.
19701994master = None
19711995
1996+
19721997def testmod (m = None , name = None , globs = None , verbose = None ,
19731998 report = True , optionflags = 0 , extraglobs = None ,
19741999 raise_on_error = False , exclude_empty = False ):
@@ -2221,12 +2246,14 @@ def run_docstring_examples(f, globs, verbose=False, name="NoName",
22212246 for test in finder .find (f , name , globs = globs ):
22222247 runner .run (test , compileflags = compileflags )
22232248
2249+
22242250######################################################################
22252251## 7. Unittest Support
22262252######################################################################
22272253
22282254_unittest_reportflags = 0
22292255
2256+
22302257def set_unittest_reportflags (flags ):
22312258 """Sets the unittest option flags.
22322259
@@ -2427,6 +2454,7 @@ def __repr__(self):
24272454 def shortDescription (self ):
24282455 return "Doctest: " + self ._dt_test .name
24292456
2457+
24302458class SkipDocTestCase (DocTestCase ):
24312459 def __init__ (self , module ):
24322460 self .module = module
@@ -2514,6 +2542,7 @@ def DocTestSuite(module=None, globs=None, extraglobs=None, test_finder=None,
25142542
25152543 return suite
25162544
2545+
25172546class DocFileCase (DocTestCase ):
25182547
25192548 def id (self ):
@@ -2527,6 +2556,7 @@ def format_failure(self, err):
25272556 % (self ._dt_test .name , self ._dt_test .filename , err )
25282557 )
25292558
2559+
25302560def DocFileTest (path , module_relative = True , package = None ,
25312561 globs = None , parser = DocTestParser (),
25322562 encoding = None , ** options ):
@@ -2553,6 +2583,7 @@ def DocFileTest(path, module_relative=True, package=None,
25532583 test = parser .get_doctest (doc , globs , name , path , 0 )
25542584 return DocFileCase (test , ** options )
25552585
2586+
25562587def DocFileSuite (* paths , ** kw ):
25572588 """A unittest suite for one or more doctest files.
25582589
@@ -2622,6 +2653,7 @@ def DocFileSuite(*paths, **kw):
26222653
26232654 return suite
26242655
2656+
26252657######################################################################
26262658## 8. Debugging Support
26272659######################################################################
@@ -2708,6 +2740,7 @@ def script_from_examples(s):
27082740 # Add a courtesy newline to prevent exec from choking (see bug #1172785)
27092741 return '\n ' .join (output ) + '\n '
27102742
2743+
27112744def testsource (module , name ):
27122745 """Extract the test sources from a doctest docstring as a script.
27132746
@@ -2724,11 +2757,13 @@ def testsource(module, name):
27242757 testsrc = script_from_examples (test .docstring )
27252758 return testsrc
27262759
2760+
27272761def debug_src (src , pm = False , globs = None ):
27282762 """Debug a single doctest docstring, in argument `src`'"""
27292763 testsrc = script_from_examples (src )
27302764 debug_script (testsrc , pm , globs )
27312765
2766+
27322767def debug_script (src , pm = False , globs = None ):
27332768 "Debug a test script. `src` is the script, as a string."
27342769 import pdb
@@ -2749,6 +2784,7 @@ def debug_script(src, pm=False, globs=None):
27492784 else :
27502785 pdb .Pdb (nosigint = True ).run ("exec(%r)" % src , globs , globs )
27512786
2787+
27522788def debug (module , name , pm = False ):
27532789 """Debug a single doctest docstring.
27542790
@@ -2760,6 +2796,7 @@ def debug(module, name, pm=False):
27602796 testsrc = testsource (module , name )
27612797 debug_script (testsrc , pm , module .__dict__ )
27622798
2799+
27632800######################################################################
27642801## 9. Example Usage
27652802######################################################################
@@ -2807,6 +2844,7 @@ def get(self):
28072844
28082845 return self .val
28092846
2847+
28102848__test__ = {"_TestClass" : _TestClass ,
28112849 "string" : r"""
28122850 Example of a string object, searched as-is.
0 commit comments