Python doctest ValueError:“之后缺少空白”

时间:2018-08-27 19:11:29

标签: python doctest

我具有以下功能和文档字符串:

def vflip(self):
    """
    Vertically flips the characters on this canvas.

    >>> canvas = Canvas(4, 4)
    >>> canvas.fill('.')
    >>> canvas[0, 0] = 'A'
    >>> canvas[1, 1] = 'B'
    >>> canvas[2, 2] = 'C'
    >>> canvas[3, 3] = 'D'
    >>> print(canvas)
    A...
    .B..
    ..C.
    ...D
    >>> canvas.vflip()
    >>> print(canvas)
    ...D
    ..C.
    .B..
    A...
    """
    for y in range(0, self.height // 2):
        for x in range(0, self.width):
            self._chars[x][y], self._chars[x][self.height - 1 - y] = self._chars[x][self.height - 1 - y], self._chars[x][y]
    self._strDirty = True

哪个会产生以下错误:

Traceback (most recent call last):
  File "__init__.py", line 1332, in <module>
    print(doctest.testmod())
  File "C:\Users\Al\AppData\Local\Programs\Python\Python37\lib\doctest.py", line 1949, in testmod
    for test in finder.find(m, name, globs=globs, extraglobs=extraglobs):
  File "C:\Users\Al\AppData\Local\Programs\Python\Python37\lib\doctest.py", line 932, in find
    self._find(tests, obj, name, module, source_lines, globs, {})
  File "C:\Users\Al\AppData\Local\Programs\Python\Python37\lib\doctest.py", line 995, in _find
    globs, seen)
  File "C:\Users\Al\AppData\Local\Programs\Python\Python37\lib\doctest.py", line 1029, in _find
    globs, seen)
  File "C:\Users\Al\AppData\Local\Programs\Python\Python37\lib\doctest.py", line 982, in _find
    test = self._get_test(obj, name, module, globs, source_lines)
  File "C:\Users\Al\AppData\Local\Programs\Python\Python37\lib\doctest.py", line 1066, in _get_test
    filename, lineno)
  File "C:\Users\Al\AppData\Local\Programs\Python\Python37\lib\doctest.py", line 668, in get_doctest
    return DocTest(self.get_examples(string, name), globs,
  File "C:\Users\Al\AppData\Local\Programs\Python\Python37\lib\doctest.py", line 682, in get_examples
    return [x for x in self.parse(string, name)
  File "C:\Users\Al\AppData\Local\Programs\Python\Python37\lib\doctest.py", line 644, in parse
    self._parse_example(m, name, lineno)
  File "C:\Users\Al\AppData\Local\Programs\Python\Python37\lib\doctest.py", line 702, in _parse_example
    self._check_prompt_blank(source_lines, indent, name, lineno)
  File "C:\Users\Al\AppData\Local\Programs\Python\Python37\lib\doctest.py", line 789, in _check_prompt_blank
    line[indent:indent+3], line))
ValueError: line 17 of the docstring for __main__.Canvas.vflip lacks blank after ...: '...D'

我不确定为什么会出现此错误。其他StackOverflow帖子通常表明>>>与该行上的代码之间没有空格,但是这里不是这种情况。为什么会出现此错误?

1 个答案:

答案 0 :(得分:0)

这是一种特殊情况,其中输出文本'...D'模仿另一个Python提示符。交互式外壳程序将>>>用作普通提示符,但是多行语句中的任何其他行都将...用作辅助提示符。 '...D'令人困惑的doctest并使其认为这是一个提示。

例如,请注意以下基本代码中的两个提示:

>>> for i in range(5):
...     print('Hello, world!')