Skip to content

Trailing white space stripped from doctests #9849

@mwri

Description

@mwri

Describe the bug

This doctest fails, and I don't think it should:

>>> text = '''foo
... bar 
...  baz
... ba t
... '''
>>> print(repr(text))
'foo\nbar \n baz\nba t\n'
>>> text.count(' ')
3

The reason it fails, is there is a trailing space after "bar" (on line 2), but this is eliminated before the source is passed to doctest, thus text is assigned the value foo\nbar\n baz\nba t\n instead of foo\nbar \n baz\nba t\n. Whilst I generally really hate trailing white space, if the space matters... then it matters... So report is:

**********************************************************************
File "index.rst", line 9, in default
Failed example:
    print(repr(text))
Expected:
    'foo\nbar \n baz\nba t\n'
Got:
    'foo\nbar\n baz\nba t\n'
**********************************************************************
File "index.rst", line 11, in default
Failed example:
    text.count(' ')
Expected:
    3
Got:
    2
**********************************************************************

Invoking doctest on the rst directly the test above passes no problem.

How to Reproduce

$ mkdir foobar
$ cd foobar
$ python3 -m venv venv
$ . venv/bin/activate
$ pip install sphinx
$ sphinx-quickstart --sep --project foobar --author 'Does Not Matter' --release 0.0.1 --language en --ext-doctest
$ cat > source/index.rst 
foobar
======

.. doctest::

   >>> text = '''foo
   ... bar 
   ...  baz
   ... ba t
   ... '''
   >>> print(repr(text))
   'foo\nbar \n baz\nba t\n'
   >>> text.count(' ')
   3
^D
$ make doctest

Expected behavior

Doc test illustrated above passes.

Your project

N/A

Screenshots

No response

OS

Linux

Python version

3.8.10

Sphinx version

4.3.0

Sphinx extensions

No response

Extra tools

No response

Additional context

I had a little look into it and I can see the source is serialised with pickle in build/doctrees (in this above case), with the space missing. Or at least getting it back out the space is missing by that point anyway.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions