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

Skip to content

Commit 83e51f4

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 3650ea2 commit 83e51f4

2 files changed

Lines changed: 37 additions & 23 deletions

File tree

Doc/library/doctest.rst

Lines changed: 8 additions & 23 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

@@ -674,53 +676,36 @@ above.
674676
An example's doctest directives modify doctest's behavior for that single
675677
example. Use ``+`` to enable the named behavior, or ``-`` to disable it.
676678

677-
.. note::
678-
Due to an `unfortunate limitation`_ of our current documentation
679-
publishing process, syntax highlighting has been disabled in the examples
680-
below in order to ensure the doctest directives are correctly displayed.
681-
682-
.. _unfortunate limitation: http://bugs.python.org/issue12947
679+
For example, this test passes::
683680

684-
For example, this test passes:
685-
686-
.. code-block:: text
687-
688-
>>> print(list(range(20))) #doctest: +NORMALIZE_WHITESPACE
681+
>>> print(list(range(20))) # doctest: +NORMALIZE_WHITESPACE
689682
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
690683
10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
691684

692685
Without the directive it would fail, both because the actual output doesn't have
693686
two blanks before the single-digit list elements, and because the actual output
694687
is on a single line. This test also passes, and also requires a directive to do
695-
so:
696-
697-
.. code-block:: text
688+
so::
698689

699690
>>> print(list(range(20))) # doctest: +ELLIPSIS
700691
[0, 1, ..., 18, 19]
701692

702693
Multiple directives can be used on a single physical line, separated by
703-
commas:
704-
705-
.. code-block:: text
694+
commas::
706695

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

710699
If multiple directive comments are used for a single example, then they are
711-
combined:
712-
713-
.. code-block:: text
700+
combined::
714701

715702
>>> print(list(range(20))) # doctest: +ELLIPSIS
716703
... # doctest: +NORMALIZE_WHITESPACE
717704
[0, 1, ..., 18, 19]
718705

719706
As the previous example shows, you can add ``...`` lines to your example
720707
containing only directives. This can be useful when an example is too long for
721-
a directive to comfortably fit on the same line:
722-
723-
.. code-block:: text
708+
a directive to comfortably fit on the same line::
724709

725710
>>> print(list(range(5)) + list(range(10, 20)) + list(range(30, 40)))
726711
... # doctest: +ELLIPSIS

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)