Skip to content

Commit ac45567

Browse files
authored
[3.12] Docs: Test presence of optional extensions with importlib (GH-130445) (#130466)
* [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 9bd92fa commit ac45567

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

Doc/conf.py

+12-10
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,15 @@
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 = ('sphinxext.opengraph',)
42+
for optional_ext in _OPTIONAL_EXTENSIONS:
43+
try:
44+
if find_spec(optional_ext) is not None:
45+
extensions.append(optional_ext)
46+
except (ImportError, ValueError):
47+
pass
48+
del _OPTIONAL_EXTENSIONS
4749

4850
doctest_global_setup = '''
4951
try:
@@ -66,7 +68,7 @@
6668
# We look for the Include/patchlevel.h file in the current Python source tree
6769
# and replace the values accordingly.
6870
# See Doc/tools/extensions/patchlevel.py
69-
version, release = importlib.import_module('patchlevel').get_version_info()
71+
version, release = import_module('patchlevel').get_version_info()
7072

7173
rst_epilog = f"""
7274
.. |python_version_literal| replace:: ``Python {version}``

0 commit comments

Comments
 (0)