1.1 --- a/annotate.py Sat Dec 09 00:36:06 2006 +0100
1.2 +++ b/annotate.py Sat Dec 09 02:16:20 2006 +0100
1.3 @@ -387,7 +387,7 @@
1.4 and storing details on the node.
1.5 """
1.6
1.7 - module = self.importer.load(import_.name, self.builtins)
1.8 + module = self.importer.load(import_.name, self.builtins, getattr(import_, "alias", None))
1.9 if module is not None:
1.10 self.namespace.set_types([module])
1.11 else:
1.12 @@ -1329,7 +1329,7 @@
1.13 """
1.14
1.15 self.path = path or [os.getcwd()]
1.16 - self.modules = []
1.17 + self.modules = {}
1.18
1.19 def find_in_path(self, name):
1.20
1.21 @@ -1405,7 +1405,7 @@
1.22 else:
1.23 return None
1.24
1.25 - def load(self, name, builtins):
1.26 + def load(self, name, builtins, alias=None):
1.27
1.28 """
1.29 Load the module or package with the given 'name' and using the specified
1.30 @@ -1418,24 +1418,30 @@
1.31 if not m:
1.32 return None # NOTE: Import error.
1.33 d, filename = m
1.34 - module = load(filename, builtins)
1.35 - self.modules.append(module)
1.36 + top = module = self.modules.get(path[0], load(filename, builtins, path[0], self))
1.37 + self.modules[path[0]] = module
1.38
1.39 if len(path) > 1:
1.40 + path_so_far = path[:1]
1.41 for p in path[1:]:
1.42 + path_so_far.append(p)
1.43 m = self.find(d, p)
1.44 if not m:
1.45 return None # NOTE: Import error.
1.46 d, filename = m
1.47 - submodule = load(filename, builtins)
1.48 - self.modules.append(submodule)
1.49 + module_name = ".".join(path_so_far)
1.50 + submodule = self.modules.get(module_name, load(filename, builtins, module_name, self))
1.51 + self.modules[module_name] = submodule
1.52
1.53 # Store the submodule within its parent module.
1.54
1.55 module.namespace[p] = [Attribute(None, submodule)]
1.56 module = submodule
1.57
1.58 - return Attribute(None, module)
1.59 + if alias:
1.60 + return Attribute(None, module)
1.61 + else:
1.62 + return Attribute(None, top)
1.63
1.64 def combine(target, additions):
1.65
1.66 @@ -1529,7 +1535,7 @@
1.67
1.68 # Convenience functions.
1.69
1.70 -def load(name, builtins=None, importer=None):
1.71 +def load(name, builtins=None, module_name=None, importer=None):
1.72
1.73 """
1.74 Load the module with the given 'name' (which may be a full module path),
1.75 @@ -1537,7 +1543,7 @@
1.76 optional 'importer' to provide a means of finding and loading modules.
1.77 """
1.78
1.79 - module = simplify.simplify(name, builtins is None)
1.80 + module = simplify.simplify(name, builtins is None, module_name)
1.81 fixnames.fix(module, builtins)
1.82 annotate(module, builtins, importer)
1.83 return module
2.1 --- a/simplify.py Sat Dec 09 00:36:06 2006 +0100
2.2 +++ b/simplify.py Sat Dec 09 02:16:20 2006 +0100
2.3 @@ -713,7 +713,7 @@
2.4 _names = []
2.5 code.append(
2.6 StoreTemp(
2.7 - expr=Import(name=from_.modname)
2.8 + expr=Import(name=from_.modname, alias=1)
2.9 )
2.10 )
2.11 from_._modname = code[-1].expr
2.12 @@ -886,7 +886,7 @@
2.13 code = []
2.14 _names = []
2.15 for path, alias in import_.names:
2.16 - importer = Import(name=path)
2.17 + importer = Import(name=path, alias=alias)
2.18 top = alias or path.split(".")[0]
2.19 code.append(StoreName(expr=importer, name=top))
2.20 _names.append(code[-1].expr)
2.21 @@ -1460,7 +1460,7 @@
2.22
2.23 # Convenience functions.
2.24
2.25 -def simplify(filename, builtins=0):
2.26 +def simplify(filename, builtins=0, module_name=None):
2.27
2.28 """
2.29 Simplify the module stored in the file with the given 'filename'.
2.30 @@ -1473,10 +1473,11 @@
2.31 module = compiler.parseFile(filename)
2.32 compiler.misc.set_filename(filename, module)
2.33 if builtins:
2.34 - name = "__builtins__"
2.35 + name = module_name or "__builtins__"
2.36 else:
2.37 path, ext = os.path.splitext(filename)
2.38 path, name = os.path.split(path)
2.39 + name = module_name or name
2.40 simplified = simplifier.process(module, name)
2.41 return simplified
2.42
3.1 --- a/test.py Sat Dec 09 00:36:06 2006 +0100
3.2 +++ b/test.py Sat Dec 09 02:16:20 2006 +0100
3.3 @@ -2,15 +2,18 @@
3.4
3.5 import sys, os
3.6 import viewer
3.7 -from annotate import Importer, load
3.8 +from annotate import AnnotationError, Importer, load
3.9
3.10 if __name__ == "__main__":
3.11
3.12 importer = Importer(sys.path)
3.13 - builtins = load(os.path.join("lib", "builtins.py"))
3.14 - module = load(sys.argv[1], builtins, importer)
3.15 -
3.16 - if "-d" in sys.argv:
3.17 - viewer.makedocs(module, importer.modules, builtins)
3.18 + try:
3.19 + builtins = load(os.path.join("lib", "builtins.py"))
3.20 + module = load(sys.argv[1], builtins, None, importer)
3.21 + except AnnotationError, exc:
3.22 + raise
3.23 + else:
3.24 + if "-d" in sys.argv:
3.25 + viewer.makedocs(module, importer.modules.values(), builtins)
3.26
3.27 # vim: tabstop=4 expandtab shiftwidth=4
4.1 --- a/tests/import.py Sat Dec 09 00:36:06 2006 +0100
4.2 +++ b/tests/import.py Sat Dec 09 02:16:20 2006 +0100
4.3 @@ -1,1 +1,6 @@
4.4 -import a, b.c, d as e, f.g as h
4.5 +import pkg, pkg.module, pkg as e, pkg.module as h
4.6 +
4.7 +pkg.f(1)
4.8 +pkg.module.f(1, 2, 3)
4.9 +e.f("2")
4.10 +h.f("2", "3", "4")
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/tests/pkg/__init__.py Sat Dec 09 02:16:20 2006 +0100
5.3 @@ -0,0 +1,2 @@
5.4 +def f(a):
5.5 + return a
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/tests/pkg/module.py Sat Dec 09 02:16:20 2006 +0100
6.3 @@ -0,0 +1,5 @@
6.4 +def f(a, b, c):
6.5 + return c
6.6 +
6.7 +def p(x, y, z):
6.8 + return x, y
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/tests/pkg/subpkg/__init__.py Sat Dec 09 02:16:20 2006 +0100
7.3 @@ -0,0 +1,2 @@
7.4 +def g(a):
7.5 + return -a
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/tests/pkg/subpkg/module.py Sat Dec 09 02:16:20 2006 +0100
8.3 @@ -0,0 +1,2 @@
8.4 +def g(a, b, c):
8.5 + return a