Skip to content

Commit

Permalink
Fix sphinx-doc#6545. Strip doctests for doctest_node blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoldbaum committed Jul 7, 2019
1 parent ef2f105 commit a570ea6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
24 changes: 16 additions & 8 deletions sphinx/transforms/post_transforms/code.py
Expand Up @@ -9,7 +9,7 @@
"""

import sys
from typing import NamedTuple
from typing import NamedTuple, Union

from docutils import nodes
from pygments.lexers import PythonConsoleLexer, guess_lexer
Expand Down Expand Up @@ -110,13 +110,21 @@ def apply(self, **kwargs):
if not self.config.trim_doctest_flags:
return

for node in self.document.traverse(nodes.literal_block):
if self.is_pyconsole(node):
source = node.rawsource
source = doctest.blankline_re.sub('', source)
source = doctest.doctestopt_re.sub('', source)
node.rawsource = source
node[:] = [nodes.Text(source)]
for lbnode in self.document.traverse(nodes.literal_block): # type: nodes.literal_block
if self.is_pyconsole(lbnode):
self.strip_doctest_flags(lbnode)

for dbnode in self.document.traverse(nodes.doctest_block): # type: nodes.doctest_block
self.strip_doctest_flags(dbnode)

@staticmethod
def strip_doctest_flags(node):
# type: (Union[nodes.literal_block, nodes.doctest_block]) -> None
source = node.rawsource
source = doctest.blankline_re.sub('', source)
source = doctest.doctestopt_re.sub('', source)
node.rawsource = source
node[:] = [nodes.Text(source)]

@staticmethod
def is_pyconsole(node):
Expand Down
5 changes: 5 additions & 0 deletions tests/roots/test-trim_doctest_flags/index.rst
Expand Up @@ -21,3 +21,8 @@ test-trim_doctest_flags

>>> datetime.date.now() # doctest: +QUX
datetime.date(2008, 1, 1)

.. doctest_block::

>>> datetime.date.now() # doctest: +QUUX
datetime.date(2008, 1, 1)
2 changes: 2 additions & 0 deletions tests/test_transforms_post_transforms_code.py
Expand Up @@ -18,6 +18,7 @@ def test_trim_doctest_flags_html(app, status, warning):
assert 'BAR' in result
assert 'BAZ' not in result
assert 'QUX' not in result
assert 'QUUX' not in result


@pytest.mark.sphinx('latex', testroot='trim_doctest_flags')
Expand All @@ -29,3 +30,4 @@ def test_trim_doctest_flags_latex(app, status, warning):
assert 'BAR' in result
assert 'BAZ' not in result
assert 'QUX' not in result
assert 'QUUX' not in result

0 comments on commit a570ea6

Please sign in to comment.