summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorEdward Loper <edloper@gradient.cis.upenn.edu>2004-08-12 02:41:30 (GMT)
committerEdward Loper <edloper@gradient.cis.upenn.edu>2004-08-12 02:41:30 (GMT)
commita5db6009fe195274760e0fb79aff67cb7f97e00e (patch)
tree745a3b70f82e3969b90cba55a7bc9636744d3bb1 /Lib
parent8e4a34ba090a4ad9753ea8d9ccf95703c85fdbb4 (diff)
downloadcpython-a5db6009fe195274760e0fb79aff67cb7f97e00e.zip
cpython-a5db6009fe195274760e0fb79aff67cb7f97e00e.tar.gz
cpython-a5db6009fe195274760e0fb79aff67cb7f97e00e.tar.bz2
- Changed output of DocTestParser.get_program() to make it easier to
visually distinguish the expected output from the comments (use "##" to mark expected outputs, and "#" to mark comments). - If the string given to DocTestParser.get_program() is indented, then strip its indentation. (In particular, find the min indentation of non-blank lines, and strip that indentation from all lines.)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/doctest.py44
-rw-r--r--Lib/test/test_doctest.py16
2 files changed, 37 insertions, 23 deletions
diff --git a/Lib/doctest.py b/Lib/doctest.py
index aad9dfe..9b91277 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -595,14 +595,20 @@ class DocTestParser:
print x
print y
# Expected:
- # 2
- # 3
+ ## 2
+ ## 3
#
- # Some text.
+ # Some text.
x+y
# Expected:
- # 5
+ ## 5
"""
+ string = string.expandtabs()
+ # If all lines begin with the same indentation, then strip it.
+ min_indent = self._min_indent(string)
+ if min_indent > 0:
+ string = '\n'.join([l[min_indent:] for l in string.split('\n')])
+
output = []
charnum, lineno = 0, 0
# Find all doctest examples in the string:
@@ -620,7 +626,7 @@ class DocTestParser:
# Display the expected output, if any
if want:
output.append('# Expected:')
- output.extend(['# '+l for l in want.split('\n')])
+ output.extend(['## '+l for l in want.split('\n')])
# Update the line number & char number.
lineno += string.count('\n', m.start(), m.end())
@@ -702,11 +708,19 @@ class DocTestParser:
(lineno, name, source))
return options
+ # This regular expression finds the indentation of every non-blank
+ # line in a string.
+ _INDENT_RE = re.compile('^([ ]+)(?=\S)', re.MULTILINE)
+
+ def _min_indent(self, s):
+ "Return the minimum indentation of any non-blank line in `s`"
+ return min([len(indent) for indent in self._INDENT_RE.findall(s)])
+
def _comment_line(self, line):
"Return a commented form of the given line"
line = line.rstrip()
if line:
- return '# '+line
+ return '# '+line
else:
return '#'
@@ -2179,30 +2193,30 @@ def script_from_examples(s):
... '''
>>> print script_from_examples(text)
- # Here are examples of simple math.
+ # Here are examples of simple math.
#
- # Python has super accurate integer addition
+ # Python has super accurate integer addition
#
2 + 2
# Expected:
- # 5
+ ## 5
#
- # And very friendly error messages:
+ # And very friendly error messages:
#
1/0
# Expected:
- # To Infinity
- # And
- # Beyond
+ ## To Infinity
+ ## And
+ ## Beyond
#
- # You can use logic if you want:
+ # You can use logic if you want:
#
if 0:
blah
blah
<BLANKLINE>
#
- # Ho hum
+ # Ho hum
"""
return DocTestParser().get_program(s)
diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py
index c734cae..8e651d0 100644
--- a/Lib/test/test_doctest.py
+++ b/Lib/test/test_doctest.py
@@ -1053,30 +1053,30 @@ words and expected output are converted to comments:
>>> import test.test_doctest
>>> name = 'test.test_doctest.sample_func'
>>> print doctest.testsource(test.test_doctest, name)
- # Blah blah
+ # Blah blah
#
print sample_func(22)
# Expected:
- # 44
+ ## 44
#
- # Yee ha!
+ # Yee ha!
>>> name = 'test.test_doctest.SampleNewStyleClass'
>>> print doctest.testsource(test.test_doctest, name)
print '1\n2\n3'
# Expected:
- # 1
- # 2
- # 3
+ ## 1
+ ## 2
+ ## 3
>>> name = 'test.test_doctest.SampleClass.a_classmethod'
>>> print doctest.testsource(test.test_doctest, name)
print SampleClass.a_classmethod(10)
# Expected:
- # 12
+ ## 12
print SampleClass(0).a_classmethod(10)
# Expected:
- # 12
+ ## 12
"""
def test_debug(): r"""