|
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'))
|
|
37 | 38 | ]
|
38 | 39 |
|
39 | 40 | # 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 |
53 | 52 |
|
54 | 53 | doctest_global_setup = '''
|
55 | 54 | try:
|
|
72 | 71 | # We look for the Include/patchlevel.h file in the current Python source tree
|
73 | 72 | # and replace the values accordingly.
|
74 | 73 | # 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() |
76 | 75 |
|
77 | 76 | rst_epilog = f"""
|
78 | 77 | .. |python_version_literal| replace:: ``Python {version}``
|
|
0 commit comments