# HG changeset patch # User paulb@localhost.localdomain # Date 1165626980 -3600 # Node ID 3c8b082f6b407a60e782c0a06ecfe7ed875e2fec # Parent e0e3e6c92606d051bb18b5b1b49e6d4ea935c18a Fixed/improved module naming. Introduced a dictionary of imported modules. Fixed aliases used with import-related statements so that the bottommost module is associated with (or used to obtain) aliases, not the uppermost module. Added tests of packages. diff -r e0e3e6c92606 -r 3c8b082f6b40 annotate.py --- a/annotate.py Sat Dec 09 00:36:06 2006 +0100 +++ b/annotate.py Sat Dec 09 02:16:20 2006 +0100 @@ -387,7 +387,7 @@ and storing details on the node. """ - module = self.importer.load(import_.name, self.builtins) + module = self.importer.load(import_.name, self.builtins, getattr(import_, "alias", None)) if module is not None: self.namespace.set_types([module]) else: @@ -1329,7 +1329,7 @@ """ self.path = path or [os.getcwd()] - self.modules = [] + self.modules = {} def find_in_path(self, name): @@ -1405,7 +1405,7 @@ else: return None - def load(self, name, builtins): + def load(self, name, builtins, alias=None): """ Load the module or package with the given 'name' and using the specified @@ -1418,24 +1418,30 @@ if not m: return None # NOTE: Import error. d, filename = m - module = load(filename, builtins) - self.modules.append(module) + top = module = self.modules.get(path[0], load(filename, builtins, path[0], self)) + self.modules[path[0]] = module if len(path) > 1: + path_so_far = path[:1] for p in path[1:]: + path_so_far.append(p) m = self.find(d, p) if not m: return None # NOTE: Import error. d, filename = m - submodule = load(filename, builtins) - self.modules.append(submodule) + module_name = ".".join(path_so_far) + submodule = self.modules.get(module_name, load(filename, builtins, module_name, self)) + self.modules[module_name] = submodule # Store the submodule within its parent module. module.namespace[p] = [Attribute(None, submodule)] module = submodule - return Attribute(None, module) + if alias: + return Attribute(None, module) + else: + return Attribute(None, top) def combine(target, additions): @@ -1529,7 +1535,7 @@ # Convenience functions. -def load(name, builtins=None, importer=None): +def load(name, builtins=None, module_name=None, importer=None): """ Load the module with the given 'name' (which may be a full module path), @@ -1537,7 +1543,7 @@ optional 'importer' to provide a means of finding and loading modules. """ - module = simplify.simplify(name, builtins is None) + module = simplify.simplify(name, builtins is None, module_name) fixnames.fix(module, builtins) annotate(module, builtins, importer) return module diff -r e0e3e6c92606 -r 3c8b082f6b40 simplify.py --- a/simplify.py Sat Dec 09 00:36:06 2006 +0100 +++ b/simplify.py Sat Dec 09 02:16:20 2006 +0100 @@ -713,7 +713,7 @@ _names = [] code.append( StoreTemp( - expr=Import(name=from_.modname) + expr=Import(name=from_.modname, alias=1) ) ) from_._modname = code[-1].expr @@ -886,7 +886,7 @@ code = [] _names = [] for path, alias in import_.names: - importer = Import(name=path) + importer = Import(name=path, alias=alias) top = alias or path.split(".")[0] code.append(StoreName(expr=importer, name=top)) _names.append(code[-1].expr) @@ -1460,7 +1460,7 @@ # Convenience functions. -def simplify(filename, builtins=0): +def simplify(filename, builtins=0, module_name=None): """ Simplify the module stored in the file with the given 'filename'. @@ -1473,10 +1473,11 @@ module = compiler.parseFile(filename) compiler.misc.set_filename(filename, module) if builtins: - name = "__builtins__" + name = module_name or "__builtins__" else: path, ext = os.path.splitext(filename) path, name = os.path.split(path) + name = module_name or name simplified = simplifier.process(module, name) return simplified diff -r e0e3e6c92606 -r 3c8b082f6b40 test.py --- a/test.py Sat Dec 09 00:36:06 2006 +0100 +++ b/test.py Sat Dec 09 02:16:20 2006 +0100 @@ -2,15 +2,18 @@ import sys, os import viewer -from annotate import Importer, load +from annotate import AnnotationError, Importer, load if __name__ == "__main__": importer = Importer(sys.path) - builtins = load(os.path.join("lib", "builtins.py")) - module = load(sys.argv[1], builtins, importer) - - if "-d" in sys.argv: - viewer.makedocs(module, importer.modules, builtins) + try: + builtins = load(os.path.join("lib", "builtins.py")) + module = load(sys.argv[1], builtins, None, importer) + except AnnotationError, exc: + raise + else: + if "-d" in sys.argv: + viewer.makedocs(module, importer.modules.values(), builtins) # vim: tabstop=4 expandtab shiftwidth=4 diff -r e0e3e6c92606 -r 3c8b082f6b40 tests/import.py --- a/tests/import.py Sat Dec 09 00:36:06 2006 +0100 +++ b/tests/import.py Sat Dec 09 02:16:20 2006 +0100 @@ -1,1 +1,6 @@ -import a, b.c, d as e, f.g as h +import pkg, pkg.module, pkg as e, pkg.module as h + +pkg.f(1) +pkg.module.f(1, 2, 3) +e.f("2") +h.f("2", "3", "4") diff -r e0e3e6c92606 -r 3c8b082f6b40 tests/pkg/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/pkg/__init__.py Sat Dec 09 02:16:20 2006 +0100 @@ -0,0 +1,2 @@ +def f(a): + return a diff -r e0e3e6c92606 -r 3c8b082f6b40 tests/pkg/module.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/pkg/module.py Sat Dec 09 02:16:20 2006 +0100 @@ -0,0 +1,5 @@ +def f(a, b, c): + return c + +def p(x, y, z): + return x, y diff -r e0e3e6c92606 -r 3c8b082f6b40 tests/pkg/subpkg/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/pkg/subpkg/__init__.py Sat Dec 09 02:16:20 2006 +0100 @@ -0,0 +1,2 @@ +def g(a): + return -a diff -r e0e3e6c92606 -r 3c8b082f6b40 tests/pkg/subpkg/module.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/pkg/subpkg/module.py Sat Dec 09 02:16:20 2006 +0100 @@ -0,0 +1,2 @@ +def g(a, b, c): + return a