summaryrefslogtreecommitdiffstats
path: root/Lib/pkgutil.py
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2014-03-04 10:39:42 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2014-03-04 10:39:42 (GMT)
commit62b4b9eecbcdd98d318eec522c8c48a3843be855 (patch)
treeb93c162769a10997e28b9ca1ab5adcb6329e6587 /Lib/pkgutil.py
parent01cc2d5fb8354f9bf8fe523ee1c26f82d871a175 (diff)
downloadcpython-62b4b9eecbcdd98d318eec522c8c48a3843be855.zip
cpython-62b4b9eecbcdd98d318eec522c8c48a3843be855.tar.gz
cpython-62b4b9eecbcdd98d318eec522c8c48a3843be855.tar.bz2
Close #20839: pkgutil.find_loader now uses importlib.util.find_spec
Diffstat (limited to 'Lib/pkgutil.py')
-rw-r--r--Lib/pkgutil.py17
1 files changed, 5 insertions, 12 deletions
diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py
index 326657a..58cccdc 100644
--- a/Lib/pkgutil.py
+++ b/Lib/pkgutil.py
@@ -470,29 +470,22 @@ def get_loader(module_or_name):
def find_loader(fullname):
"""Find a PEP 302 "loader" object for fullname
- This is s convenience wrapper around :func:`importlib.find_loader` that
- sets the *path* argument correctly when searching for submodules, and
- also ensures parent packages (if any) are imported before searching for
- submodules.
+ This is a backwards compatibility wrapper around
+ importlib.util.find_spec that converts most failures to ImportError
+ and only returns the loader rather than the full spec
"""
if fullname.startswith('.'):
msg = "Relative module name {!r} not supported".format(fullname)
raise ImportError(msg)
- path = None
- pkg_name = fullname.rpartition(".")[0]
- if pkg_name:
- pkg = importlib.import_module(pkg_name)
- path = getattr(pkg, "__path__", None)
- if path is None:
- return None
try:
- return importlib.find_loader(fullname, path)
+ spec = importlib.util.find_spec(fullname)
except (ImportError, AttributeError, TypeError, ValueError) as ex:
# This hack fixes an impedance mismatch between pkgutil and
# importlib, where the latter raises other errors for cases where
# pkgutil previously raised ImportError
msg = "Error while finding loader for {!r} ({}: {})"
raise ImportError(msg.format(fullname, type(ex), ex)) from ex
+ return spec.loader
def extend_path(path, name):