summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2019-02-28 01:05:37 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2019-02-28 20:02:11 +0900
commit0377adb82e7b848c4c50989606772cbd3abec113 (patch)
tree13e9a39795c6e69d2189d2851942aae2fc11eaaf
parent6f973d410be58c5fb2aca5fb6337dff61abc92fe (diff)
downloadsphinx-git-0377adb82e7b848c4c50989606772cbd3abec113.tar.gz
Fix #6068: doctest: ``skipif`` option may remove the code block from documentation
-rw-r--r--CHANGES1
-rw-r--r--sphinx/ext/doctest.py29
2 files changed, 20 insertions, 10 deletions
diff --git a/CHANGES b/CHANGES
index 11e9c6137..e4b1dd3c0 100644
--- a/CHANGES
+++ b/CHANGES
@@ -22,6 +22,7 @@ Bugs fixed
* #6019: imgconverter: Including multipage PDF fails
* #6047: autodoc: ``autofunction`` emits a warning for method objects
* #6028: graphviz: Ensure the graphviz filenames are reproducible
+* #6068: doctest: ``skipif`` option may remove the code block from documentation
Testing
--------
diff --git a/sphinx/ext/doctest.py b/sphinx/ext/doctest.py
index f0c418ccf..52f286c02 100644
--- a/sphinx/ext/doctest.py
+++ b/sphinx/ext/doctest.py
@@ -90,16 +90,6 @@ class TestDirective(SphinxDirective):
def run(self):
# type: () -> List[nodes.Node]
- if 'skipif' in self.options:
- condition = self.options['skipif']
- context = {} # type: Dict[str, Any]
- if self.config.doctest_global_setup:
- exec(self.config.doctest_global_setup, context)
- should_skip = eval(condition, context)
- if self.config.doctest_global_cleanup:
- exec(self.config.doctest_global_cleanup, context)
- if should_skip:
- return []
# use ordinary docutils nodes for test code: they get special attributes
# so that our builder recognizes them, and the other builders are happy.
code = '\n'.join(self.content)
@@ -161,6 +151,8 @@ class TestDirective(SphinxDirective):
self.state.document.reporter.warning(
__("'%s' is not a valid pyversion option") % spec,
line=self.lineno)
+ if 'skipif' in self.options:
+ node['skipif'] = self.options['skipif']
return [node]
@@ -411,6 +403,20 @@ Doctest summary
return node.line - 1
return None
+ def skipped(self, node):
+ # type: (nodes.Element) -> bool
+ if 'skipif' not in node:
+ return False
+ else:
+ condition = node['skipif']
+ context = {} # type: Dict[str, Any]
+ if self.config.doctest_global_setup:
+ exec(self.config.doctest_global_setup, context)
+ should_skip = eval(condition, context)
+ if self.config.doctest_global_cleanup:
+ exec(self.config.doctest_global_cleanup, context)
+ return should_skip
+
def test_doc(self, docname, doctree):
# type: (unicode, nodes.Node) -> None
groups = {} # type: Dict[unicode, TestGroup]
@@ -437,6 +443,9 @@ Doctest summary
return isinstance(node, (nodes.literal_block, nodes.comment)) \
and 'testnodetype' in node
for node in doctree.traverse(condition):
+ if self.skipped(node):
+ continue
+
source = node['test'] if 'test' in node else node.astext()
filename = self.get_filename_for_node(node, docname)
line_number = self.get_line_number(node)