Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b51b234

Browse files
author
Edward Loper
committed
Fixed bug in line-number finding for examples (DocTestParser wasn't
updating line numbers correctly for bare prompts & examples containing only comments).
1 parent f04d8a8 commit b51b234

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

Lib/doctest.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -552,12 +552,10 @@ def get_examples(self, string, name='<string>'):
552552
(source, want) = self._parse_example(m, name, lineno)
553553
# Extract extra options from the source.
554554
options = self._find_options(source, name, lineno)
555-
# If it contains no real source, then ignore it.
556-
if self._IS_BLANK_OR_COMMENT(source):
557-
continue
558555
# Create an Example, and add it to the list.
559-
examples.append( Example(source, want, lineno,
560-
len(m.group('indent')), options) )
556+
if not self._IS_BLANK_OR_COMMENT(source):
557+
examples.append( Example(source, want, lineno,
558+
len(m.group('indent')), options) )
561559
# Update lineno (lines inside this example)
562560
lineno += string.count('\n', m.start(), m.end())
563561
# Update charno.

Lib/test/test_doctest.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ def test_DocTest(): r"""
253253
254254
"""
255255

256-
# [XX] test that it's getting line numbers right.
257256
def test_DocTestFinder(): r"""
258257
Unit tests for the `DocTestFinder` class.
259258
@@ -450,6 +449,30 @@ def test_DocTestFinder(): r"""
450449
>>> for t in tests:
451450
... print '%2s %s' % (len(t.examples), t.name)
452451
1 SampleClass
452+
453+
Line numbers
454+
~~~~~~~~~~~~
455+
DocTestFinder finds the line number of each example:
456+
457+
>>> def f(x):
458+
... '''
459+
... >>> x = 12
460+
...
461+
... some text
462+
...
463+
... >>> # examples are not created for comments & bare prompts.
464+
... >>>
465+
... ...
466+
...
467+
... >>> for x in range(10):
468+
... ... print x,
469+
... 0 1 2 3 4 5 6 7 8 9
470+
... >>> x/2
471+
... 6
472+
... '''
473+
>>> test = doctest.DocTestFinder().find(f)[0]
474+
>>> [e.lineno for e in test.examples]
475+
[1, 9, 12]
453476
"""
454477

455478
class test_DocTestRunner:
@@ -892,7 +915,7 @@ def option_directives(): r"""
892915
... optionflags=doctest.ELLIPSIS).run(test)
893916
**********************************************************************
894917
Failure in example: print range(10) # doctest: -ELLIPSIS
895-
from line #6 of f
918+
from line #5 of f
896919
Expected: [0, 1, ..., 9]
897920
Got: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
898921
(1, 2)

0 commit comments

Comments
 (0)