diff options
author | Petri Lehtinen <petri@digip.org> | 2012-05-18 21:51:11 +0300 |
---|---|---|
committer | Petri Lehtinen <petri@digip.org> | 2012-05-18 21:51:11 +0300 |
commit | 45905c26e809847690c744562378a9c2a1313667 (patch) | |
tree | 66b201df1dc529c317f8728f9ef246fe0499df0d /Lib/pyclbr.py | |
parent | fb2091578e1f57e6ea1c8045701cdcdf22ff3421 (diff) | |
parent | dfdf514bfe5c630793e4726e212b6dd83fbb4ea8 (diff) | |
download | cpython-45905c26e809847690c744562378a9c2a1313667.tar.gz |
#14798: pyclbr now raises ImportError instead of KeyError for missing packages
Diffstat (limited to 'Lib/pyclbr.py')
-rw-r--r-- | Lib/pyclbr.py | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/Lib/pyclbr.py b/Lib/pyclbr.py index 65e9fbedfd..9ec05ee80b 100644 --- a/Lib/pyclbr.py +++ b/Lib/pyclbr.py @@ -39,8 +39,10 @@ Instances of this class have the following instance variables: lineno -- the line in the file on which the class statement occurred """ +import io +import os import sys -import imp +import importlib import tokenize from token import NAME, DEDENT, OP from operator import itemgetter @@ -135,19 +137,24 @@ def _readmodule(module, path, inpackage=None): # Search the path for the module f = None if inpackage is not None: - f, fname, (_s, _m, ty) = imp.find_module(module, path) + search_path = path else: - f, fname, (_s, _m, ty) = imp.find_module(module, path + sys.path) - if ty == imp.PKG_DIRECTORY: - dict['__path__'] = [fname] - path = [fname] + path - f, fname, (_s, _m, ty) = imp.find_module('__init__', [fname]) + search_path = path + sys.path + loader = importlib.find_loader(fullmodule, search_path) + fname = loader.get_filename(fullmodule) _modules[fullmodule] = dict - if ty != imp.PY_SOURCE: + if loader.is_package(fullmodule): + dict['__path__'] = [os.path.dirname(fname)] + try: + source = loader.get_source(fullmodule) + if source is None: + return dict + except (AttributeError, ImportError): # not Python source, can't do anything with this module - f.close() return dict + f = io.StringIO(source) + stack = [] # stack of (class, indent) pairs g = tokenize.generate_tokens(f.readline) |