summaryrefslogtreecommitdiff
path: root/Lib/importlib/__init__.py
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2012-05-12 17:43:17 -0400
committerBrett Cannon <brett@python.org>2012-05-12 17:43:17 -0400
commitee78a2b51cd9ede91bb780b71444119e1da19e4e (patch)
tree1bd412c9e1f23283a0435e665ca2d33c00d2d3c4 /Lib/importlib/__init__.py
parentacc0c181a85143e0113f1ec9cb838e86cd8df50f (diff)
downloadcpython-git-ee78a2b51cd9ede91bb780b71444119e1da19e4e.tar.gz
Issue #13959: Introduce importlib.find_loader().
The long-term goal is to deprecate imp.find_module() in favour of this API, but it will take some time as some APIs explicitly return/use what imp.find_module() returns.
Diffstat (limited to 'Lib/importlib/__init__.py')
-rw-r--r--Lib/importlib/__init__.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/Lib/importlib/__init__.py b/Lib/importlib/__init__.py
index 57fb284ab1..90e163c433 100644
--- a/Lib/importlib/__init__.py
+++ b/Lib/importlib/__init__.py
@@ -29,6 +29,30 @@ def invalidate_caches():
finder.invalidate_caches()
+def find_loader(name, path=None):
+ """Find the loader for the specified module.
+
+ First, sys.modules is checked to see if the module was already imported. If
+ so, then sys.modules[name].__loader__ is returned. If that happens to be
+ set to None, then ValueError is raised. If the module is not in
+ sys.modules, then sys.meta_path is searched for a suitable loader with the
+ value of 'path' given to the finders. None is returned if no loader could
+ be found.
+
+ Dotted names do not have their parent packages implicitly imported.
+
+ """
+ try:
+ loader = sys.modules[name].__loader__
+ if loader is None:
+ raise ValueError('{}.__loader__ is None'.format(name))
+ else:
+ return loader
+ except KeyError:
+ pass
+ return _bootstrap._find_module(name, path)
+
+
def import_module(name, package=None):
"""Import a module.