@@ -147,10 +147,13 @@ def __repr__(self):
147
147
# Option constants.
148
148
149
149
OPTIONFLAGS_BY_NAME = {}
150
+
151
+
150
152
def register_optionflag (name ):
151
153
# Create a new flag unless `name` is already known.
152
154
return OPTIONFLAGS_BY_NAME .setdefault (name , 1 << len (OPTIONFLAGS_BY_NAME ))
153
155
156
+
154
157
DONT_ACCEPT_TRUE_FOR_1 = register_optionflag ('DONT_ACCEPT_TRUE_FOR_1' )
155
158
DONT_ACCEPT_BLANKLINE = register_optionflag ('DONT_ACCEPT_BLANKLINE' )
156
159
NORMALIZE_WHITESPACE = register_optionflag ('NORMALIZE_WHITESPACE' )
@@ -194,6 +197,7 @@ def register_optionflag(name):
194
197
# 8. Debugging Support
195
198
# 9. Example Usage
196
199
200
+
197
201
######################################################################
198
202
## 1. Utility Functions
199
203
######################################################################
@@ -210,6 +214,7 @@ def _extract_future_flags(globs):
210
214
flags |= feature .compiler_flag
211
215
return flags
212
216
217
+
213
218
def _normalize_module (module , depth = 2 ):
214
219
"""
215
220
Return the module specified by `module`. In particular:
@@ -235,10 +240,12 @@ def _normalize_module(module, depth=2):
235
240
else :
236
241
raise TypeError ("Expected a module, string, or None" )
237
242
243
+
238
244
def _newline_convert (data ):
239
245
# The IO module provides a handy decoder for universal newline conversion
240
246
return IncrementalNewlineDecoder (None , True ).decode (data , True )
241
247
248
+
242
249
def _load_testfile (filename , package , module_relative , encoding ):
243
250
if module_relative :
244
251
package = _normalize_module (package , 3 )
@@ -257,6 +264,7 @@ def _load_testfile(filename, package, module_relative, encoding):
257
264
with open (filename , encoding = encoding ) as f :
258
265
return f .read (), filename
259
266
267
+
260
268
def _indent (s , indent = 4 ):
261
269
"""
262
270
Add the given number of space characters to the beginning of
@@ -265,6 +273,7 @@ def _indent(s, indent=4):
265
273
# This regexp matches the start of non-blank lines:
266
274
return re .sub ('(?m)^(?!$)' , indent * ' ' , s )
267
275
276
+
268
277
def _exception_traceback (exc_info ):
269
278
"""
270
279
Return a string containing a traceback message for the given
@@ -276,6 +285,7 @@ def _exception_traceback(exc_info):
276
285
traceback .print_exception (exc_type , exc_val , exc_tb , file = excout )
277
286
return excout .getvalue ()
278
287
288
+
279
289
# Override some StringIO methods.
280
290
class _SpoofOut (StringIO ):
281
291
def getvalue (self ):
@@ -291,6 +301,7 @@ def truncate(self, size=None):
291
301
self .seek (size )
292
302
StringIO .truncate (self )
293
303
304
+
294
305
# Worst-case linear-time ellipsis matching.
295
306
def _ellipsis_match (want , got ):
296
307
"""
@@ -341,6 +352,7 @@ def _ellipsis_match(want, got):
341
352
342
353
return True
343
354
355
+
344
356
def _comment_line (line ):
345
357
"Return a commented form of the given line"
346
358
line = line .rstrip ()
@@ -349,6 +361,7 @@ def _comment_line(line):
349
361
else :
350
362
return '#'
351
363
364
+
352
365
def _strip_exception_details (msg ):
353
366
# Support for IGNORE_EXCEPTION_DETAIL.
354
367
# Get rid of everything except the exception name; in particular, drop
@@ -375,6 +388,7 @@ def _strip_exception_details(msg):
375
388
start = i + 1
376
389
return msg [start : end ]
377
390
391
+
378
392
class _OutputRedirectingPdb (pdb .Pdb ):
379
393
"""
380
394
A specialized version of the python debugger that redirects stdout
@@ -411,6 +425,7 @@ def trace_dispatch(self, *args):
411
425
finally :
412
426
sys .stdout = save_stdout
413
427
428
+
414
429
# [XX] Normalize with respect to os.path.pardir?
415
430
def _module_relative_path (module , test_path ):
416
431
if not inspect .ismodule (module ):
@@ -446,6 +461,7 @@ def _module_relative_path(module, test_path):
446
461
# Combine the base directory and the test path.
447
462
return os .path .join (basedir , test_path )
448
463
464
+
449
465
######################################################################
450
466
## 2. Example & DocTest
451
467
######################################################################
@@ -526,6 +542,7 @@ def __hash__(self):
526
542
return hash ((self .source , self .want , self .lineno , self .indent ,
527
543
self .exc_msg ))
528
544
545
+
529
546
class DocTest :
530
547
"""
531
548
A collection of doctest examples that should be run in a single
@@ -599,6 +616,7 @@ def __lt__(self, other):
599
616
<
600
617
(other .name , other .filename , other_lno , id (other )))
601
618
619
+
602
620
######################################################################
603
621
## 3. DocTestParser
604
622
######################################################################
@@ -1164,6 +1182,7 @@ def _find_lineno(self, obj, source_lines):
1164
1182
# We couldn't find the line number.
1165
1183
return None
1166
1184
1185
+
1167
1186
######################################################################
1168
1187
## 5. DocTest Runner
1169
1188
######################################################################
@@ -1483,6 +1502,7 @@ def __record_outcome(self, test, failures, tries, skips):
1483
1502
__LINECACHE_FILENAME_RE = re .compile (r'<doctest '
1484
1503
r'(?P<name>.+)'
1485
1504
r'\[(?P<examplenum>\d+)\]>$' )
1505
+
1486
1506
def __patched_linecache_getlines (self , filename , module_globals = None ):
1487
1507
m = self .__LINECACHE_FILENAME_RE .match (filename )
1488
1508
if m and m .group ('name' ) == self .test .name :
@@ -1819,6 +1839,7 @@ def output_difference(self, example, got, optionflags):
1819
1839
else :
1820
1840
return 'Expected nothing\n Got nothing\n '
1821
1841
1842
+
1822
1843
class DocTestFailure (Exception ):
1823
1844
"""A DocTest example has failed in debugging mode.
1824
1845
@@ -1838,6 +1859,7 @@ def __init__(self, test, example, got):
1838
1859
def __str__ (self ):
1839
1860
return str (self .test )
1840
1861
1862
+
1841
1863
class UnexpectedException (Exception ):
1842
1864
"""A DocTest example has encountered an unexpected exception
1843
1865
@@ -1857,6 +1879,7 @@ def __init__(self, test, example, exc_info):
1857
1879
def __str__ (self ):
1858
1880
return str (self .test )
1859
1881
1882
+
1860
1883
class DebugRunner (DocTestRunner ):
1861
1884
r"""Run doc tests but raise an exception as soon as there is a failure.
1862
1885
@@ -1960,6 +1983,7 @@ def report_unexpected_exception(self, out, test, example, exc_info):
1960
1983
def report_failure (self , out , test , example , got ):
1961
1984
raise DocTestFailure (test , example , got )
1962
1985
1986
+
1963
1987
######################################################################
1964
1988
## 6. Test Functions
1965
1989
######################################################################
@@ -1969,6 +1993,7 @@ def report_failure(self, out, test, example, got):
1969
1993
# class, updated by testmod.
1970
1994
master = None
1971
1995
1996
+
1972
1997
def testmod (m = None , name = None , globs = None , verbose = None ,
1973
1998
report = True , optionflags = 0 , extraglobs = None ,
1974
1999
raise_on_error = False , exclude_empty = False ):
@@ -2221,12 +2246,14 @@ def run_docstring_examples(f, globs, verbose=False, name="NoName",
2221
2246
for test in finder .find (f , name , globs = globs ):
2222
2247
runner .run (test , compileflags = compileflags )
2223
2248
2249
+
2224
2250
######################################################################
2225
2251
## 7. Unittest Support
2226
2252
######################################################################
2227
2253
2228
2254
_unittest_reportflags = 0
2229
2255
2256
+
2230
2257
def set_unittest_reportflags (flags ):
2231
2258
"""Sets the unittest option flags.
2232
2259
@@ -2427,6 +2454,7 @@ def __repr__(self):
2427
2454
def shortDescription (self ):
2428
2455
return "Doctest: " + self ._dt_test .name
2429
2456
2457
+
2430
2458
class SkipDocTestCase (DocTestCase ):
2431
2459
def __init__ (self , module ):
2432
2460
self .module = module
@@ -2514,6 +2542,7 @@ def DocTestSuite(module=None, globs=None, extraglobs=None, test_finder=None,
2514
2542
2515
2543
return suite
2516
2544
2545
+
2517
2546
class DocFileCase (DocTestCase ):
2518
2547
2519
2548
def id (self ):
@@ -2527,6 +2556,7 @@ def format_failure(self, err):
2527
2556
% (self ._dt_test .name , self ._dt_test .filename , err )
2528
2557
)
2529
2558
2559
+
2530
2560
def DocFileTest (path , module_relative = True , package = None ,
2531
2561
globs = None , parser = DocTestParser (),
2532
2562
encoding = None , ** options ):
@@ -2553,6 +2583,7 @@ def DocFileTest(path, module_relative=True, package=None,
2553
2583
test = parser .get_doctest (doc , globs , name , path , 0 )
2554
2584
return DocFileCase (test , ** options )
2555
2585
2586
+
2556
2587
def DocFileSuite (* paths , ** kw ):
2557
2588
"""A unittest suite for one or more doctest files.
2558
2589
@@ -2622,6 +2653,7 @@ def DocFileSuite(*paths, **kw):
2622
2653
2623
2654
return suite
2624
2655
2656
+
2625
2657
######################################################################
2626
2658
## 8. Debugging Support
2627
2659
######################################################################
@@ -2708,6 +2740,7 @@ def script_from_examples(s):
2708
2740
# Add a courtesy newline to prevent exec from choking (see bug #1172785)
2709
2741
return '\n ' .join (output ) + '\n '
2710
2742
2743
+
2711
2744
def testsource (module , name ):
2712
2745
"""Extract the test sources from a doctest docstring as a script.
2713
2746
@@ -2724,11 +2757,13 @@ def testsource(module, name):
2724
2757
testsrc = script_from_examples (test .docstring )
2725
2758
return testsrc
2726
2759
2760
+
2727
2761
def debug_src (src , pm = False , globs = None ):
2728
2762
"""Debug a single doctest docstring, in argument `src`'"""
2729
2763
testsrc = script_from_examples (src )
2730
2764
debug_script (testsrc , pm , globs )
2731
2765
2766
+
2732
2767
def debug_script (src , pm = False , globs = None ):
2733
2768
"Debug a test script. `src` is the script, as a string."
2734
2769
import pdb
@@ -2749,6 +2784,7 @@ def debug_script(src, pm=False, globs=None):
2749
2784
else :
2750
2785
pdb .Pdb (nosigint = True ).run ("exec(%r)" % src , globs , globs )
2751
2786
2787
+
2752
2788
def debug (module , name , pm = False ):
2753
2789
"""Debug a single doctest docstring.
2754
2790
@@ -2760,6 +2796,7 @@ def debug(module, name, pm=False):
2760
2796
testsrc = testsource (module , name )
2761
2797
debug_script (testsrc , pm , module .__dict__ )
2762
2798
2799
+
2763
2800
######################################################################
2764
2801
## 9. Example Usage
2765
2802
######################################################################
@@ -2807,6 +2844,7 @@ def get(self):
2807
2844
2808
2845
return self .val
2809
2846
2847
+
2810
2848
__test__ = {"_TestClass" : _TestClass ,
2811
2849
"string" : r"""
2812
2850
Example of a string object, searched as-is.
0 commit comments