summaryrefslogtreecommitdiffstats
path: root/Lib/pyclbr.py
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2012-05-12 21:43:17 (GMT)
committerBrett Cannon <brett@python.org>2012-05-12 21:43:17 (GMT)
commitee78a2b51cd9ede91bb780b71444119e1da19e4e (patch)
tree1bd412c9e1f23283a0435e665ca2d33c00d2d3c4 /Lib/pyclbr.py
parentacc0c181a85143e0113f1ec9cb838e86cd8df50f (diff)
downloadcpython-ee78a2b51cd9ede91bb780b71444119e1da19e4e.zip
cpython-ee78a2b51cd9ede91bb780b71444119e1da19e4e.tar.gz
cpython-ee78a2b51cd9ede91bb780b71444119e1da19e4e.tar.bz2
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/pyclbr.py')
-rw-r--r--Lib/pyclbr.py25
1 files changed, 16 insertions, 9 deletions
diff --git a/Lib/pyclbr.py b/Lib/pyclbr.py
index 52cbdd5..4cd85b9 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
@@ -133,19 +135,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)