From ee78a2b51cd9ede91bb780b71444119e1da19e4e Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Sat, 12 May 2012 17:43:17 -0400 Subject: 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. --- Lib/importlib/__init__.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'Lib/importlib/__init__.py') 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. -- cgit v1.2.1