diff --git a/CHANGES b/CHANGES index cb0c562fa..664b6e38f 100644 --- a/CHANGES +++ b/CHANGES @@ -31,6 +31,8 @@ Incompatible changes will use the text font not the math font. The ``LGR`` font encoding must be added to the ``'fontenc'`` key of :confval:`latex_elements` for this to work (only if it is needed by the document, of course). +* #5770: doctest: Follow :confval:`highlight_language` on highlighting doctest + block. As a result, they are highlighted as python3 by default. Deprecated ---------- diff --git a/sphinx/ext/doctest.py b/sphinx/ext/doctest.py index fd4f7025d..a4f7ef731 100644 --- a/sphinx/ext/doctest.py +++ b/sphinx/ext/doctest.py @@ -122,9 +122,15 @@ class TestDirective(SphinxDirective): # only save if it differs from code node['test'] = test if self.name == 'doctest': - node['language'] = 'pycon' + if self.config.highlight_language in ('py', 'python'): + node['language'] = 'pycon' + else: + node['language'] = 'pycon3' # default elif self.name == 'testcode': - node['language'] = 'python' + if self.config.highlight_language in ('py', 'python'): + node['language'] = 'python' + else: + node['language'] = 'python3' # default elif self.name == 'testoutput': # don't try to highlight output node['language'] = 'none' diff --git a/tests/test_ext_doctest.py b/tests/test_ext_doctest.py index 13e0066f6..58c4ca85e 100644 --- a/tests/test_ext_doctest.py +++ b/tests/test_ext_doctest.py @@ -11,6 +11,7 @@ import os from collections import Counter +from docutils import nodes import pytest from packaging.specifiers import InvalidSpecifier from packaging.version import InvalidVersion @@ -32,6 +33,23 @@ def test_build(app, status, warning): assert cleanup_called == 3, 'testcleanup did not get executed enough times' +@pytest.mark.sphinx('dummy', testroot='ext-doctest') +def test_highlight_language_default(app, status, warning): + app.build() + doctree = app.env.get_doctree('doctest') + for node in doctree.traverse(nodes.literal_block): + assert node['language'] in ('python3', 'pycon3', 'none') + + +@pytest.mark.sphinx('dummy', testroot='ext-doctest', + confoverrides={'highlight_language': 'python'}) +def test_highlight_language_python2(app, status, warning): + app.build() + doctree = app.env.get_doctree('doctest') + for node in doctree.traverse(nodes.literal_block): + assert node['language'] in ('python', 'pycon', 'none') + + def test_is_allowed_version(): assert is_allowed_version('<3.4', '3.3') is True assert is_allowed_version('<3.4', '3.3') is True