Skip to content

Commit ac6ed80

Browse files
committed
[3.12] Docs: Test presence of optional extensions with importlib (GH-130445)
(cherry picked from commit 3cc9e86) Co-authored-by: Adam Turner <[email protected]>
1 parent 6c59ea6 commit ac6ed80

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

Doc/conf.py

Lines changed: 14 additions & 10 deletions
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'))
@@ -36,14 +37,17 @@
3637
'sphinx.ext.extlinks',
3738
]
3839

39-
# Skip if downstream redistributors haven't installed it
40-
try:
41-
import sphinxext.opengraph # noqa: F401
42-
except ImportError:
43-
pass
44-
else:
45-
extensions.append('sphinxext.opengraph')
46-
40+
# Skip if downstream redistributors haven't installed them
41+
_OPTIONAL_EXTENSIONS = (
42+
'sphinxext.opengraph',
43+
)
44+
for optional_ext in _OPTIONAL_EXTENSIONS:
45+
try:
46+
if find_spec(optional_ext) is not None:
47+
extensions.append(optional_ext)
48+
except (ImportError, ValueError):
49+
pass
50+
del _OPTIONAL_EXTENSIONS
4751

4852
doctest_global_setup = '''
4953
try:
@@ -66,7 +70,7 @@
6670
# We look for the Include/patchlevel.h file in the current Python source tree
6771
# and replace the values accordingly.
6872
# See Doc/tools/extensions/patchlevel.py
69-
version, release = importlib.import_module('patchlevel').get_version_info()
73+
version, release = import_module('patchlevel').get_version_info()
7074

7175
rst_epilog = f"""
7276
.. |python_version_literal| replace:: ``Python {version}``

0 commit comments

Comments
 (0)