From ac6ed8010e58880703c499f0163272f3e6d8751b Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Date: Sat, 22 Feb 2025 17:52:47 +0000 Subject: [PATCH 1/2] [3.12] Docs: Test presence of optional extensions with importlib (GH-130445) (cherry picked from commit 3cc9e867eba1ed139cf28c74b4a788bcc4801394) Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> --- Doc/conf.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/Doc/conf.py b/Doc/conf.py index 023b2ace21dd40..a8688e9260d9db 100644 --- a/Doc/conf.py +++ b/Doc/conf.py @@ -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')) @@ -36,14 +37,17 @@ 'sphinx.ext.extlinks', ] -# Skip if downstream redistributors haven't installed it -try: - import sphinxext.opengraph # noqa: F401 -except ImportError: - pass -else: - extensions.append('sphinxext.opengraph') - +# Skip if downstream redistributors haven't installed them +_OPTIONAL_EXTENSIONS = ( + '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: @@ -66,7 +70,7 @@ # 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}`` From 035c9ff09c30b7ae5ffc1cbebd8906a361decbec Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Sat, 22 Feb 2025 18:01:47 +0000 Subject: [PATCH 2/2] fmt --- Doc/conf.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Doc/conf.py b/Doc/conf.py index a8688e9260d9db..42442742f65781 100644 --- a/Doc/conf.py +++ b/Doc/conf.py @@ -38,9 +38,7 @@ ] # Skip if downstream redistributors haven't installed them -_OPTIONAL_EXTENSIONS = ( - 'sphinxext.opengraph', -) +_OPTIONAL_EXTENSIONS = ('sphinxext.opengraph',) for optional_ext in _OPTIONAL_EXTENSIONS: try: if find_spec(optional_ext) is not None: