|
6 | 6 | # The contents of this file are pickled, so don't put values in the namespace |
7 | 7 | # that aren't pickleable (module imports are okay, they're removed automatically). |
8 | 8 |
|
9 | | -import importlib |
10 | 9 | import os |
11 | 10 | import sys |
| 11 | +from importlib import import_module |
| 12 | +from importlib.util import find_spec |
12 | 13 |
|
13 | 14 | # Make our custom extensions available to Sphinx |
14 | 15 | sys.path.append(os.path.abspath('tools/extensions')) |
|
39 | 40 | ] |
40 | 41 |
|
41 | 42 | # Skip if downstream redistributors haven't installed them |
42 | | -try: |
43 | | - import notfound.extension # noqa: F401 |
44 | | -except ImportError: |
45 | | - pass |
46 | | -else: |
47 | | - extensions.append('notfound.extension') |
48 | | -try: |
49 | | - import sphinxext.opengraph # noqa: F401 |
50 | | -except ImportError: |
51 | | - pass |
52 | | -else: |
53 | | - extensions.append('sphinxext.opengraph') |
54 | | - |
| 43 | +_OPTIONAL_EXTENSIONS = ( |
| 44 | + 'notfound.extension', |
| 45 | + 'sphinxext.opengraph', |
| 46 | +) |
| 47 | +for optional_ext in _OPTIONAL_EXTENSIONS: |
| 48 | + try: |
| 49 | + if find_spec(optional_ext) is not None: |
| 50 | + extensions.append(optional_ext) |
| 51 | + except (ImportError, ValueError): |
| 52 | + pass |
| 53 | +del _OPTIONAL_EXTENSIONS |
55 | 54 |
|
56 | 55 | doctest_global_setup = ''' |
57 | 56 | try: |
|
74 | 73 | # We look for the Include/patchlevel.h file in the current Python source tree |
75 | 74 | # and replace the values accordingly. |
76 | 75 | # See Doc/tools/extensions/patchlevel.py |
77 | | -version, release = importlib.import_module('patchlevel').get_version_info() |
| 76 | +version, release = import_module('patchlevel').get_version_info() |
78 | 77 |
|
79 | 78 | rst_epilog = f""" |
80 | 79 | .. |python_version_literal| replace:: ``Python {version}`` |
|
0 commit comments