Changeset 388 for python/vendor/current/Lib/doctest.py
- Timestamp:
- Mar 19, 2014, 11:11:30 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/vendor/current/Lib/doctest.py
r2 r388 217 217 # conversion as universal newlines would do. 218 218 return file_contents.replace(os.linesep, '\n'), filename 219 return open(filename).read(), filename 219 with open(filename) as f: 220 return f.read(), filename 220 221 221 222 # Use sys.stdout encoding for ouput. … … 264 265 if hasattr(self, "softspace"): 265 266 del self.softspace 267 if not self.buf: 268 # Reset it to an empty string, to make sure it's not unicode. 269 self.buf = '' 266 270 267 271 # Worst-case linear-time ellipsis matching. … … 333 337 self.__debugger_used = False 334 338 pdb.Pdb.__init__(self, stdout=out) 339 # still use input() to get user input 340 self.use_rawinput = 1 335 341 336 342 def set_trace(self, frame=None): … … 419 425 420 426 - indent: The example's indentation in the DocTest string. 421 I.e., the number of space characters that prece edthe427 I.e., the number of space characters that precede the 422 428 example's first prompt. 423 429 … … 446 452 self.exc_msg = exc_msg 447 453 454 def __eq__(self, other): 455 if type(self) is not type(other): 456 return NotImplemented 457 458 return self.source == other.source and \ 459 self.want == other.want and \ 460 self.lineno == other.lineno and \ 461 self.indent == other.indent and \ 462 self.options == other.options and \ 463 self.exc_msg == other.exc_msg 464 465 def __ne__(self, other): 466 return not self == other 467 468 def __hash__(self): 469 return hash((self.source, self.want, self.lineno, self.indent, 470 self.exc_msg)) 471 472 448 473 class DocTest: 449 474 """ … … 494 519 (self.name, self.filename, self.lineno, examples)) 495 520 521 def __eq__(self, other): 522 if type(self) is not type(other): 523 return NotImplemented 524 525 return self.examples == other.examples and \ 526 self.docstring == other.docstring and \ 527 self.globs == other.globs and \ 528 self.name == other.name and \ 529 self.filename == other.filename and \ 530 self.lineno == other.lineno 531 532 def __ne__(self, other): 533 return not self == other 534 535 def __hash__(self): 536 return hash((self.docstring, self.name, self.filename, self.lineno)) 496 537 497 538 # This lets us sort tests by name: … … 524 565 (?P<want> (?:(?![ ]*$) # Not a blank line 525 566 (?![ ]*>>>) # Not a line starting with PS1 526 . *$\n? # But any other line567 .+$\n? # But any other line 527 568 )*) 528 569 ''', re.MULTILINE | re.VERBOSE) … … 855 896 globs['__name__'] = '__main__' # provide a default module name 856 897 857 # Recursively exp ore `obj`, extracting DocTests.898 # Recursively explore `obj`, extracting DocTests. 858 899 tests = [] 859 900 self._find(tests, obj, name, module, source_lines, globs, {}) … … 1212 1253 for examplenum, example in enumerate(test.examples): 1213 1254 1214 # If REPORT_ONLY_FIRST_FAILURE is set, then sup ress1255 # If REPORT_ONLY_FIRST_FAILURE is set, then suppress 1215 1256 # reporting after the first failure. 1216 1257 quiet = (self.optionflags & REPORT_ONLY_FIRST_FAILURE and … … 1283 1324 # Another chance if they didn't care about the detail. 1284 1325 elif self.optionflags & IGNORE_EXCEPTION_DETAIL: 1285 m1 = re.match(r' [^:]*:', example.exc_msg)1286 m2 = re.match(r' [^:]*:', exc_msg)1287 if m1 and m2 and check(m1.group( 0), m2.group(0),1326 m1 = re.match(r'(?:[^:]*\.)?([^:]*:)', example.exc_msg) 1327 m2 = re.match(r'(?:[^:]*\.)?([^:]*:)', exc_msg) 1328 if m1 and m2 and check(m1.group(1), m2.group(1), 1288 1329 self.optionflags): 1289 1330 outcome = SUCCESS … … 1323 1364 1324 1365 __LINECACHE_FILENAME_RE = re.compile(r'<doctest ' 1325 r'(?P<name> [\w\.]+)'1366 r'(?P<name>.+)' 1326 1367 r'\[(?P<examplenum>\d+)\]>$') 1327 1368 def __patched_linecache_getlines(self, filename, module_globals=None): … … 1329 1370 if m and m.group('name') == self.test.name: 1330 1371 example = self.test.examples[int(m.group('examplenum'))] 1331 return example.source.splitlines(True) 1372 source = example.source 1373 if isinstance(source, unicode): 1374 source = source.encode('ascii', 'backslashreplace') 1375 return source.splitlines(True) 1332 1376 else: 1333 1377 return self.save_linecache_getlines(filename, module_globals) … … 1378 1422 linecache.getlines = self.__patched_linecache_getlines 1379 1423 1424 # Make sure sys.displayhook just prints the value to stdout 1425 save_displayhook = sys.displayhook 1426 sys.displayhook = sys.__displayhook__ 1427 1380 1428 try: 1381 1429 return self.__run(test, compileflags, out) … … 1384 1432 pdb.set_trace = save_set_trace 1385 1433 linecache.getlines = self.save_linecache_getlines 1434 sys.displayhook = save_displayhook 1386 1435 if clear_globs: 1387 1436 test.globs.clear() … … 1761 1810 Return (#failures, #tests). 1762 1811 1763 See doctest.__doc__for an overview.1812 See help(doctest) for an overview. 1764 1813 1765 1814 Optional keyword arg "name" gives the name of the module; by default … … 2173 2222 2174 2223 The DocTestCase provides a debug method that raises 2175 UnexpectedException errors if there is an unex epcted2224 UnexpectedException errors if there is an unexpected 2176 2225 exception: 2177 2226 … … 2239 2288 return self._dt_test.name 2240 2289 2290 def __eq__(self, other): 2291 if type(self) is not type(other): 2292 return NotImplemented 2293 2294 return self._dt_test == other._dt_test and \ 2295 self._dt_optionflags == other._dt_optionflags and \ 2296 self._dt_setUp == other._dt_setUp and \ 2297 self._dt_tearDown == other._dt_tearDown and \ 2298 self._dt_checker == other._dt_checker 2299 2300 def __ne__(self, other): 2301 return not self == other 2302 2303 def __hash__(self): 2304 return hash((self._dt_optionflags, self._dt_setUp, self._dt_tearDown, 2305 self._dt_checker)) 2306 2241 2307 def __repr__(self): 2242 2308 name = self._dt_test.name.split('.') … … 2247 2313 def shortDescription(self): 2248 2314 return "Doctest: " + self._dt_test.name 2315 2316 class SkipDocTestCase(DocTestCase): 2317 def __init__(self, module): 2318 self.module = module 2319 DocTestCase.__init__(self, None) 2320 2321 def setUp(self): 2322 self.skipTest("DocTestSuite will not work with -O2 and above") 2323 2324 def test_skip(self): 2325 pass 2326 2327 def shortDescription(self): 2328 return "Skipping tests from %s" % self.module.__name__ 2329 2330 __str__ = shortDescription 2331 2249 2332 2250 2333 def DocTestSuite(module=None, globs=None, extraglobs=None, test_finder=None, … … 2290 2373 module = _normalize_module(module) 2291 2374 tests = test_finder.find(module, globs=globs, extraglobs=extraglobs) 2292 if not tests: 2375 2376 if not tests and sys.flags.optimize >=2: 2377 # Skip doctests when running with -O2 2378 suite = unittest.TestSuite() 2379 suite.addTest(SkipDocTestCase(module)) 2380 return suite 2381 elif not tests: 2293 2382 # Why do we want to do this? Because it reveals a bug that might 2294 2383 # otherwise be hidden. 2295 raise ValueError(module, "has no tests") 2384 # It is probably a bug that this exception is not also raised if the 2385 # number of doctest examples in tests is zero (i.e. if no doctest 2386 # examples were found). However, we should probably not be raising 2387 # an exception at all here, though it is too late to make this change 2388 # for a maintenance release. See also issue #14649. 2389 raise ValueError(module, "has no docstrings") 2296 2390 2297 2391 tests.sort() 2298 2392 suite = unittest.TestSuite() 2393 2299 2394 for test in tests: 2300 2395 if len(test.examples) == 0: … … 2668 2763 } 2669 2764 2765 2670 2766 def _test(): 2671 2767 testfiles = [arg for arg in sys.argv[1:] if arg and arg[0] != '-'] 2672 if testfiles: 2673 for filename in testfiles: 2674 if filename.endswith(".py"): 2675 # It is a module -- insert its dir into sys.path and try to 2676 # import it. If it is part of a package, that possibly won't work 2677 # because of package imports. 2678 dirname, filename = os.path.split(filename) 2679 sys.path.insert(0, dirname) 2680 m = __import__(filename[:-3]) 2681 del sys.path[0] 2682 failures, _ = testmod(m) 2683 else: 2684 failures, _ = testfile(filename, module_relative=False) 2685 if failures: 2686 return 1 2687 else: 2688 r = unittest.TextTestRunner() 2689 r.run(DocTestSuite()) 2768 if not testfiles: 2769 name = os.path.basename(sys.argv[0]) 2770 if '__loader__' in globals(): # python -m 2771 name, _ = os.path.splitext(name) 2772 print("usage: {0} [-v] file ...".format(name)) 2773 return 2 2774 for filename in testfiles: 2775 if filename.endswith(".py"): 2776 # It is a module -- insert its dir into sys.path and try to 2777 # import it. If it is part of a package, that possibly 2778 # won't work because of package imports. 2779 dirname, filename = os.path.split(filename) 2780 sys.path.insert(0, dirname) 2781 m = __import__(filename[:-3]) 2782 del sys.path[0] 2783 failures, _ = testmod(m) 2784 else: 2785 failures, _ = testfile(filename, module_relative=False) 2786 if failures: 2787 return 1 2690 2788 return 0 2789 2691 2790 2692 2791 if __name__ == "__main__":
Note:
See TracChangeset
for help on using the changeset viewer.