Skip to content

Commit 2412c91

Browse files
[3.13] Docs: Test presence of optional extensions with importlib (GH-130445) (#130464)
(cherry picked from commit 3cc9e86) Co-authored-by: Adam Turner <[email protected]>
1 parent 27a28ee commit 2412c91

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'))
@@ -37,19 +38,17 @@
3738
]
3839

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

5453
doctest_global_setup = '''
5554
try:
@@ -72,7 +71,7 @@
7271
# We look for the Include/patchlevel.h file in the current Python source tree
7372
# and replace the values accordingly.
7473
# See Doc/tools/extensions/patchlevel.py
75-
version, release = importlib.import_module('patchlevel').get_version_info()
74+
version, release = import_module('patchlevel').get_version_info()
7675

7776
rst_epilog = f"""
7877
.. |python_version_literal| replace:: ``Python {version}``

0 commit comments

Comments
 (0)