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

Skip to content

Commit 3a2e101

Browse files
committed
Issue #12947: revert earlier workaround and use a monkey-patch to enable showing doctest directives only in the doctest docs.
1 parent 4614cc4 commit 3a2e101

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

Doc/library/doctest.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
:keepdoctest:
2+
13
:mod:`doctest` --- Test interactive Python examples
24
===================================================
35

@@ -652,7 +654,7 @@ example. Use ``+`` to enable the named behavior, or ``-`` to disable it.
652654

653655
For example, this test passes::
654656

655-
>>> print(list(range(20))) #doctest: +NORMALIZE_WHITESPACE
657+
>>> print(list(range(20))) # doctest: +NORMALIZE_WHITESPACE
656658
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
657659
10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
658660

@@ -664,7 +666,8 @@ so::
664666
>>> print(list(range(20))) # doctest: +ELLIPSIS
665667
[0, 1, ..., 18, 19]
666668

667-
Multiple directives can be used on a single physical line, separated by commas::
669+
Multiple directives can be used on a single physical line, separated by
670+
commas::
668671

669672
>>> print(list(range(20))) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
670673
[0, 1, ..., 18, 19]

Doc/tools/sphinxext/pyspecific.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,38 @@ def new_visit_versionmodified(self, node):
3333
self.body.append('<span class="versionmodified">%s</span> ' % text)
3434

3535
from sphinx.writers.html import HTMLTranslator
36+
from sphinx.writers.latex import LaTeXTranslator
3637
from sphinx.locale import versionlabels
3738
HTMLTranslator.visit_versionmodified = new_visit_versionmodified
39+
HTMLTranslator.visit_versionmodified = new_visit_versionmodified
3840

41+
# monkey-patch HTML and LaTeX translators to keep doctest blocks in the
42+
# doctest docs themselves
43+
orig_visit_literal_block = HTMLTranslator.visit_literal_block
44+
def new_visit_literal_block(self, node):
45+
meta = self.builder.env.metadata[self.builder.current_docname]
46+
old_trim_doctest_flags = self.highlighter.trim_doctest_flags
47+
if 'keepdoctest' in meta:
48+
self.highlighter.trim_doctest_flags = False
49+
try:
50+
orig_visit_literal_block(self, node)
51+
finally:
52+
self.highlighter.trim_doctest_flags = old_trim_doctest_flags
53+
54+
HTMLTranslator.visit_literal_block = new_visit_literal_block
55+
56+
orig_depart_literal_block = LaTeXTranslator.depart_literal_block
57+
def new_depart_literal_block(self, node):
58+
meta = self.builder.env.metadata[self.curfilestack[-1]]
59+
old_trim_doctest_flags = self.highlighter.trim_doctest_flags
60+
if 'keepdoctest' in meta:
61+
self.highlighter.trim_doctest_flags = False
62+
try:
63+
orig_depart_literal_block(self, node)
64+
finally:
65+
self.highlighter.trim_doctest_flags = old_trim_doctest_flags
66+
67+
LaTeXTranslator.depart_literal_block = new_depart_literal_block
3968

4069
# Support for marking up and linking to bugs.python.org issues
4170

0 commit comments

Comments
 (0)