diff options
author | Jon Dufresne <jon.dufresne@gmail.com> | 2019-08-17 11:45:39 -0700 |
---|---|---|
committer | Jon Dufresne <jon.dufresne@gmail.com> | 2019-08-17 11:45:39 -0700 |
commit | ecb1e763ad0917ac37061b648a53793a687cc760 (patch) | |
tree | da14d950c57dd801c1ac2d01995eaec574dcff4e /sphinx/registry.py | |
parent | bf573ae1454fd0a6f5bbcb316fbabc42a056da3a (diff) | |
download | sphinx-git-ecb1e763ad0917ac37061b648a53793a687cc760.tar.gz |
Switch uses of __import__ to importlib.get_module()
The Python docs for __import__ recommend using importlib.get_module().
https://docs.python.org/3/library/functions.html#__import__
> Note: This is an advanced function that is not needed in everyday
> Python programming, unlike importlib.import_module().
As importlib.get_module() uses the Python module cache and returns the
module, this also allows simplifying many module cache checks of use of
sys.modules.
importlib.get_module() has been available since Python 3.3.
Diffstat (limited to 'sphinx/registry.py')
-rw-r--r-- | sphinx/registry.py | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/sphinx/registry.py b/sphinx/registry.py index f8ee39466..3666c758b 100644 --- a/sphinx/registry.py +++ b/sphinx/registry.py @@ -10,6 +10,7 @@ import traceback import warnings +from importlib import import_module from inspect import isclass from types import MethodType @@ -471,18 +472,19 @@ class SphinxComponentRegistry: prefix = __('while setting up extension %s:') % extname with prefixed_warnings(prefix): try: - mod = __import__(extname, None, None, ['setup']) + mod = import_module(extname) except ImportError as err: logger.verbose(__('Original exception:\n') + traceback.format_exc()) raise ExtensionError(__('Could not import extension %s') % extname, err) - if not hasattr(mod, 'setup'): + setup = getattr(mod, 'setup', None) + if setup is None: logger.warning(__('extension %r has no setup() function; is it really ' 'a Sphinx extension module?'), extname) metadata = {} # type: Dict[str, Any] else: try: - metadata = mod.setup(app) + metadata = setup(app) except VersionRequirementError as err: # add the extension name to the version required raise VersionRequirementError( |