diff --git a/sphinx/ext/doctest.py b/sphinx/ext/doctest.py index 8e060907f12..966ef708474 100644 --- a/sphinx/ext/doctest.py +++ b/sphinx/ext/doctest.py @@ -94,15 +94,9 @@ def run(self) -> List[Node]: # only save if it differs from code node['test'] = test if self.name == 'doctest': - if self.config.highlight_language in ('py', 'python'): - node['language'] = 'pycon' - else: - node['language'] = 'pycon3' # default + node['language'] = 'pycon' elif self.name == 'testcode': - if self.config.highlight_language in ('py', 'python'): - node['language'] = 'python' - else: - node['language'] = 'python3' # default + node['language'] = 'python' elif self.name == 'testoutput': # don't try to highlight output node['language'] = 'none' diff --git a/sphinx/ext/viewcode.py b/sphinx/ext/viewcode.py index 279997b4394..d61837945d6 100644 --- a/sphinx/ext/viewcode.py +++ b/sphinx/ext/viewcode.py @@ -244,7 +244,7 @@ def collect_pages(app: Sphinx) -> Generator[Tuple[str, Dict[str, Any], str], Non # construct a page name for the highlighted source pagename = posixpath.join(OUTPUT_DIRNAME, modname.replace('.', '/')) # highlight the source using the builder's highlighter - if env.config.highlight_language in ('python3', 'default', 'none'): + if env.config.highlight_language in {'default', 'none'}: lexer = env.config.highlight_language else: lexer = 'python' diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py index 4a737eb4072..7716f2a1270 100644 --- a/sphinx/highlighting.py +++ b/sphinx/highlighting.py @@ -120,6 +120,8 @@ def get_lexer(self, source: str, lang: str, opts: Optional[Dict] = None, lang = 'pycon' else: lang = 'python' + if lang == 'pycon3': + lang = 'pycon' if lang in lexers: # just return custom lexers here (without installing raiseonerror filter) diff --git a/sphinx/transforms/post_transforms/code.py b/sphinx/transforms/post_transforms/code.py index 3c3f5432892..5a5980c4a79 100644 --- a/sphinx/transforms/post_transforms/code.py +++ b/sphinx/transforms/post_transforms/code.py @@ -109,9 +109,9 @@ def is_pyconsole(node: nodes.literal_block) -> bool: return False # skip parsed-literal node language = node.get('language') - if language in ('pycon', 'pycon3'): + if language in {'pycon', 'pycon3'}: return True - elif language in ('py', 'py3', 'python', 'python3', 'default'): + elif language in {'py', 'python', 'py3', 'python3', 'default'}: return node.rawsource.startswith('>>>') elif language == 'guess': try: diff --git a/tests/test_ext_doctest.py b/tests/test_ext_doctest.py index 6ec0495ef24..6c628904f27 100644 --- a/tests/test_ext_doctest.py +++ b/tests/test_ext_doctest.py @@ -29,16 +29,16 @@ def test_highlight_language_default(app, status, warning): app.build() doctree = app.env.get_doctree('doctest') for node in doctree.findall(nodes.literal_block): - assert node['language'] in ('python3', 'pycon3', 'none') + assert node['language'] in {'python', 'pycon', 'none'} @pytest.mark.sphinx('dummy', testroot='ext-doctest', confoverrides={'highlight_language': 'python'}) -def test_highlight_language_python2(app, status, warning): +def test_highlight_language_python3(app, status, warning): app.build() doctree = app.env.get_doctree('doctest') for node in doctree.findall(nodes.literal_block): - assert node['language'] in ('python', 'pycon', 'none') + assert node['language'] in {'python', 'pycon', 'none'} def test_is_allowed_version(): diff --git a/tests/test_highlighting.py b/tests/test_highlighting.py index 92276a21c45..d7e3625c4c6 100644 --- a/tests/test_highlighting.py +++ b/tests/test_highlighting.py @@ -81,14 +81,23 @@ def test_default_highlight(logger): ret = bridge.highlight_block('reST ``like`` text', 'default') assert ret == '
reST ``like`` text\n
\n' - # python3: highlights as python3 - ret = bridge.highlight_block('print "Hello sphinx world"', 'python3') - assert ret == ('
print '
-                   '"Hello sphinx world"\n
\n') + # python: highlights as python3 + ret = bridge.highlight_block('print("Hello sphinx world")', 'python') + assert ret == ('
print'
+                   '('
+                   '"Hello sphinx world"'
+                   ')\n
\n') - # python3: raises error if highlighting failed - ret = bridge.highlight_block('reST ``like`` text', 'python3') + # python3: highlights as python3 + ret = bridge.highlight_block('print("Hello sphinx world")', 'python3') + assert ret == ('
print'
+                   '('
+                   '"Hello sphinx world"'
+                   ')\n
\n') + + # python: raises error if highlighting failed + ret = bridge.highlight_block('reST ``like`` text', 'python') logger.warning.assert_called_with('Could not lex literal_block as "%s". ' - 'Highlighting skipped.', 'python3', + 'Highlighting skipped.', 'python', type='misc', subtype='highlighting_failure', location=None)