# HG changeset patch # User Paul Boddie # Date 1381265084 -7200 # Node ID be4ea92ff0629aa6e2ece116795c475e3e2f5557 # Parent d3a80d3d317ae3e6338ab9b7c24da80dca4976e9 Added support for "whole module" relative imports. diff -r d3a80d3d317a -r be4ea92ff062 micropython/inspect.py --- a/micropython/inspect.py Tue Oct 08 20:54:09 2013 +0200 +++ b/micropython/inspect.py Tue Oct 08 22:44:44 2013 +0200 @@ -224,17 +224,26 @@ # Module import declarations. elif isinstance(n, compiler.ast.From): - modname = self.get_module_name(n) + modname, names = self.get_module_name(n) + + # Perform whole module relative imports. - # Load the mentioned module. + if not modname: + for name, alias in names: + self.record_import(name, n) - self.record_import(modname, n) + # Otherwise, perform normal "from" imports. - # Speculatively load modules for names beneath the module. + else: + # Load the mentioned module. + + self.record_import(modname, n) - for name, alias in n.names: - subname = modname + "." + name - self.record_import(subname, n) + # Speculatively load modules for names beneath the module. + + for name, alias in n.names: + subname = modname + "." + name + self.record_import(subname, n) elif isinstance(n, compiler.ast.Import): @@ -256,19 +265,23 @@ def get_module_name(self, node): """ - Return the module name from the given 'node', calculated using any - relative import information. + Using the given From 'node', calculate any relative import information, + returning a tuple containing a module to import along with any names to + import based on the node's name information. + + Where the returned module is given as None, whole module imports should + be performed for the returned modules using the returned names. """ # Absolute import. if node.level == 0: - return node.modname + return node.modname, node.names # Relative to this module. elif node.level == 1: - return "%s.%s" % (self.full_name(), node.modname) + basename = self.full_name() # Relative to an ancestor of this module. @@ -276,8 +289,17 @@ path = self.full_name().split(".") if node.level > len(path): raise InspectError("Relative import %r involves too many levels up from module %r" % (("." * node.level + node.modname), self.full_name())) - else: - return "%s.%s" % (".".join(path[:-node.level+1]), node.modname) + basename = ".".join(path[:-node.level+1]) + + # Name imports from a module. + + if node.modname: + return "%s.%s" % (basename, node.modname), node.names + + # Relative whole module imports. + + else: + return None, [("%s.%s" % (basename, name), alias) for name, alias in node.names] def get_module_paths(self, name): @@ -1152,7 +1174,11 @@ self.resume_broken_branches() def visitFrom(self, node): - modname = self.get_module_name(node) + modname, names = self.get_module_name(node) + + if not modname: + return self._visitImport(names) + module = self.complete_import(modname, True) for name, alias in node.names: @@ -1259,7 +1285,10 @@ return make_instance() # either outcome is possible def visitImport(self, node): - for name, alias in node.names: + self._visitImport(node.names) + + def _visitImport(self, names): + for name, alias in names: module = self.complete_import(name, alias) if alias is not None: self.store(alias, module or UnresolvedName(None, name, self)) diff -r d3a80d3d317a -r be4ea92ff062 tests/relative/subpackage/__init__.py --- a/tests/relative/subpackage/__init__.py Tue Oct 08 20:54:09 2013 +0200 +++ b/tests/relative/subpackage/__init__.py Tue Oct 08 22:44:44 2013 +0200 @@ -2,5 +2,8 @@ from ..upper import x from .sibling import y +from . import whole + +z = whole.x # vim: tabstop=4 expandtab shiftwidth=4 diff -r d3a80d3d317a -r be4ea92ff062 tests/relative/subpackage/whole.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/relative/subpackage/whole.py Tue Oct 08 22:44:44 2013 +0200 @@ -0,0 +1,5 @@ +#!/usr/bin/env python + +x = 789 + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r d3a80d3d317a -r be4ea92ff062 tests/relative_import.py --- a/tests/relative_import.py Tue Oct 08 20:54:09 2013 +0200 +++ b/tests/relative_import.py Tue Oct 08 22:44:44 2013 +0200 @@ -4,5 +4,6 @@ result_123 = relative.subpackage.x result_456 = relative.subpackage.y +result_789 = relative.subpackage.z # vim: tabstop=4 expandtab shiftwidth=4