Skip to content

Commit 3cc9e86

Browse files
authored
Docs: Test presence of optional extensions with importlib (#130445)
1 parent d8ce092 commit 3cc9e86

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

Doc/conf.py

+14-15
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
# The contents of this file are pickled, so don't put values in the namespace
77
# that aren't pickleable (module imports are okay, they're removed automatically).
88

9-
import importlib
109
import os
1110
import sys
11+
from importlib import import_module
12+
from importlib.util import find_spec
1213

1314
# Make our custom extensions available to Sphinx
1415
sys.path.append(os.path.abspath('tools/extensions'))
@@ -39,19 +40,17 @@
3940
]
4041

4142
# Skip if downstream redistributors haven't installed them
42-
try:
43-
import notfound.extension # noqa: F401
44-
except ImportError:
45-
pass
46-
else:
47-
extensions.append('notfound.extension')
48-
try:
49-
import sphinxext.opengraph # noqa: F401
50-
except ImportError:
51-
pass
52-
else:
53-
extensions.append('sphinxext.opengraph')
54-
43+
_OPTIONAL_EXTENSIONS = (
44+
'notfound.extension',
45+
'sphinxext.opengraph',
46+
)
47+
for optional_ext in _OPTIONAL_EXTENSIONS:
48+
try:
49+
if find_spec(optional_ext) is not None:
50+
extensions.append(optional_ext)
51+
except (ImportError, ValueError):
52+
pass
53+
del _OPTIONAL_EXTENSIONS
5554

5655
doctest_global_setup = '''
5756
try:
@@ -74,7 +73,7 @@
7473
# We look for the Include/patchlevel.h file in the current Python source tree
7574
# and replace the values accordingly.
7675
# See Doc/tools/extensions/patchlevel.py
77-
version, release = importlib.import_module('patchlevel').get_version_info()
76+
version, release = import_module('patchlevel').get_version_info()
7877

7978
rst_epilog = f"""
8079
.. |python_version_literal| replace:: ``Python {version}``

0 commit comments

Comments
 (0)