]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] Docs: Test presence of optional extensions with importlib (GH-130445) (#130464)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 22 Feb 2025 18:00:15 +0000 (19:00 +0100)
committerGitHub <noreply@github.com>
Sat, 22 Feb 2025 18:00:15 +0000 (18:00 +0000)
(cherry picked from commit 3cc9e867eba1ed139cf28c74b4a788bcc4801394)

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Doc/conf.py

index 6f85e92550f3f3ed1f102ca2f3f5e103cd8a6e62..40e436a27c7f22ce0287ed930b4ee2cc4dd185dc 100644 (file)
@@ -6,9 +6,10 @@
 # The contents of this file are pickled, so don't put values in the namespace
 # that aren't pickleable (module imports are okay, they're removed automatically).
 
-import importlib
 import os
 import sys
+from importlib import import_module
+from importlib.util import find_spec
 
 # Make our custom extensions available to Sphinx
 sys.path.append(os.path.abspath('tools/extensions'))
@@ -37,19 +38,17 @@ extensions = [
 ]
 
 # Skip if downstream redistributors haven't installed them
-try:
-    import notfound.extension  # noqa: F401
-except ImportError:
-    pass
-else:
-    extensions.append('notfound.extension')
-try:
-    import sphinxext.opengraph  # noqa: F401
-except ImportError:
-    pass
-else:
-    extensions.append('sphinxext.opengraph')
-
+_OPTIONAL_EXTENSIONS = (
+    'notfound.extension',
+    'sphinxext.opengraph',
+)
+for optional_ext in _OPTIONAL_EXTENSIONS:
+    try:
+        if find_spec(optional_ext) is not None:
+            extensions.append(optional_ext)
+    except (ImportError, ValueError):
+        pass
+del _OPTIONAL_EXTENSIONS
 
 doctest_global_setup = '''
 try:
@@ -72,7 +71,7 @@ copyright = "2001-%Y, Python Software Foundation"
 # We look for the Include/patchlevel.h file in the current Python source tree
 # and replace the values accordingly.
 # See Doc/tools/extensions/patchlevel.py
-version, release = importlib.import_module('patchlevel').get_version_info()
+version, release = import_module('patchlevel').get_version_info()
 
 rst_epilog = f"""
 .. |python_version_literal| replace:: ``Python {version}``