Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(10)

Side by Side Diff: Lib/doctest.py

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/distutils/tests/test_build_py.py ('k') | Lib/email/errors.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Module doctest. 1 # Module doctest.
2 # Released to the public domain 16-Jan-2001, by Tim Peters (tim@python.org). 2 # Released to the public domain 16-Jan-2001, by Tim Peters (tim@python.org).
3 # Major enhancements and refactoring by: 3 # Major enhancements and refactoring by:
4 # Jim Fulton 4 # Jim Fulton
5 # Edward Loper 5 # Edward Loper
6 6
7 # Provided as-is; use at your own risk; no warranty; no promises; enjoy! 7 # Provided as-is; use at your own risk; no warranty; no promises; enjoy!
8 8
9 r"""Module doctest -- a framework for running examples in docstrings. 9 r"""Module doctest -- a framework for running examples in docstrings.
10 10
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 'DocFileSuite', 85 'DocFileSuite',
86 'set_unittest_reportflags', 86 'set_unittest_reportflags',
87 # 8. Debugging Support 87 # 8. Debugging Support
88 'script_from_examples', 88 'script_from_examples',
89 'testsource', 89 'testsource',
90 'debug_src', 90 'debug_src',
91 'debug', 91 'debug',
92 ] 92 ]
93 93
94 import __future__ 94 import __future__
95 import difflib 95
96 import inspect 96 import sys, traceback, inspect, linecache, os, re
97 import linecache 97 import unittest, difflib, pdb, tempfile
98 import os 98 import warnings
99 import pdb
100 import re
101 import sys
102 import traceback
103 import unittest
104 from io import StringIO 99 from io import StringIO
105 from collections import namedtuple 100 from collections import namedtuple
106 101
107 TestResults = namedtuple('TestResults', 'failed attempted') 102 TestResults = namedtuple('TestResults', 'failed attempted')
108 103
109 # There are 4 basic classes: 104 # There are 4 basic classes:
110 # - Example: a <source, want> pair, plus an intra-docstring line number. 105 # - Example: a <source, want> pair, plus an intra-docstring line number.
111 # - DocTest: a collection of examples, parsed from a docstring, plus 106 # - DocTest: a collection of examples, parsed from a docstring, plus
112 # info about where the docstring came from (name, filename, lineno). 107 # info about where the docstring came from (name, filename, lineno).
113 # - DocTestFinder: extracts DocTests from a given object's docstring and 108 # - DocTestFinder: extracts DocTests from a given object's docstring and
(...skipping 2395 matching lines...) Expand 10 before | Expand all | Expand 10 after
2509 2504
2510 def debug_src(src, pm=False, globs=None): 2505 def debug_src(src, pm=False, globs=None):
2511 """Debug a single doctest docstring, in argument `src`'""" 2506 """Debug a single doctest docstring, in argument `src`'"""
2512 testsrc = script_from_examples(src) 2507 testsrc = script_from_examples(src)
2513 debug_script(testsrc, pm, globs) 2508 debug_script(testsrc, pm, globs)
2514 2509
2515 def debug_script(src, pm=False, globs=None): 2510 def debug_script(src, pm=False, globs=None):
2516 "Debug a test script. `src` is the script, as a string." 2511 "Debug a test script. `src` is the script, as a string."
2517 import pdb 2512 import pdb
2518 2513
2519 if globs: 2514 # Note that tempfile.NameTemporaryFile() cannot be used. As the
2520 globs = globs.copy() 2515 # docs say, a file so created cannot be opened by name a second time
2521 else: 2516 # on modern Windows boxes, and exec() needs to open and read it.
2522 globs = {} 2517 srcfilename = tempfile.mktemp(".py", "doctestdebug")
2518 f = open(srcfilename, 'w')
2519 f.write(src)
2520 f.close()
2523 2521
2524 if pm: 2522 try:
2525 try: 2523 if globs:
2526 exec(src, globs, globs) 2524 globs = globs.copy()
2527 except: 2525 else:
2528 print(sys.exc_info()[1]) 2526 globs = {}
2529 p = pdb.Pdb(nosigint=True) 2527
2530 p.reset() 2528 if pm:
2531 p.interaction(None, sys.exc_info()[2]) 2529 try:
2532 else: 2530 with open(srcfilename) as f:
2533 pdb.Pdb(nosigint=True).run("exec(%r)" % src, globs, globs) 2531 exec(f.read(), globs, globs)
2532 except:
2533 print(sys.exc_info()[1])
2534 p = pdb.Pdb(nosigint=True)
2535 p.reset()
2536 p.interaction(None, sys.exc_info()[2])
2537 else:
2538 fp = open(srcfilename)
2539 try:
2540 script = fp.read()
2541 finally:
2542 fp.close()
2543 pdb.Pdb(nosigint=True).run("exec(%r)" % script, globs, globs)
2544
2545 finally:
2546 os.remove(srcfilename)
2534 2547
2535 def debug(module, name, pm=False): 2548 def debug(module, name, pm=False):
2536 """Debug a single doctest docstring. 2549 """Debug a single doctest docstring.
2537 2550
2538 Provide the module (or dotted name of the module) containing the 2551 Provide the module (or dotted name of the module) containing the
2539 test to be debugged and the name (within the module) of the object 2552 test to be debugged and the name (within the module) of the object
2540 with the docstring with tests to be debugged. 2553 with the docstring with tests to be debugged.
2541 """ 2554 """
2542 module = _normalize_module(module) 2555 module = _normalize_module(module)
2543 testsrc = testsource(module, name) 2556 testsrc = testsource(module, name)
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
2661 failures, _ = testmod(m) 2674 failures, _ = testmod(m)
2662 else: 2675 else:
2663 failures, _ = testfile(filename, module_relative=False) 2676 failures, _ = testfile(filename, module_relative=False)
2664 if failures: 2677 if failures:
2665 return 1 2678 return 1
2666 return 0 2679 return 0
2667 2680
2668 2681
2669 if __name__ == "__main__": 2682 if __name__ == "__main__":
2670 sys.exit(_test()) 2683 sys.exit(_test())
OLDNEW
« no previous file with comments | « Lib/distutils/tests/test_build_py.py ('k') | Lib/email/errors.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+