1.1 --- a/micropython/inspect.py Tue Oct 08 20:53:03 2013 +0200
1.2 +++ b/micropython/inspect.py Tue Oct 08 20:54:09 2013 +0200
1.3 @@ -224,16 +224,17 @@
1.4 # Module import declarations.
1.5
1.6 elif isinstance(n, compiler.ast.From):
1.7 + modname = self.get_module_name(n)
1.8
1.9 # Load the mentioned module.
1.10
1.11 - self.record_import(n.modname, n)
1.12 + self.record_import(modname, n)
1.13
1.14 # Speculatively load modules for names beneath the module.
1.15
1.16 for name, alias in n.names:
1.17 - modname = n.modname + "." + name
1.18 - self.record_import(modname, n)
1.19 + subname = modname + "." + name
1.20 + self.record_import(subname, n)
1.21
1.22 elif isinstance(n, compiler.ast.Import):
1.23
1.24 @@ -252,6 +253,32 @@
1.25 else:
1.26 self.process_structure(n)
1.27
1.28 + def get_module_name(self, node):
1.29 +
1.30 + """
1.31 + Return the module name from the given 'node', calculated using any
1.32 + relative import information.
1.33 + """
1.34 +
1.35 + # Absolute import.
1.36 +
1.37 + if node.level == 0:
1.38 + return node.modname
1.39 +
1.40 + # Relative to this module.
1.41 +
1.42 + elif node.level == 1:
1.43 + return "%s.%s" % (self.full_name(), node.modname)
1.44 +
1.45 + # Relative to an ancestor of this module.
1.46 +
1.47 + else:
1.48 + path = self.full_name().split(".")
1.49 + if node.level > len(path):
1.50 + raise InspectError("Relative import %r involves too many levels up from module %r" % (("." * node.level + node.modname), self.full_name()))
1.51 + else:
1.52 + return "%s.%s" % (".".join(path[:-node.level+1]), node.modname)
1.53 +
1.54 def get_module_paths(self, name):
1.55
1.56 """
1.57 @@ -1125,7 +1152,8 @@
1.58 self.resume_broken_branches()
1.59
1.60 def visitFrom(self, node):
1.61 - module = self.complete_import(node.modname, True)
1.62 + modname = self.get_module_name(node)
1.63 + module = self.complete_import(modname, True)
1.64
1.65 for name, alias in node.names:
1.66
1.67 @@ -1137,7 +1165,7 @@
1.68
1.69 # Missing names may refer to submodules.
1.70
1.71 - submodule = self.complete_import(node.modname + "." + name, True)
1.72 + submodule = self.complete_import(modname + "." + name, True)
1.73 if submodule:
1.74 if not module.has_key(name):
1.75 module.store(name, submodule)
1.76 @@ -1152,7 +1180,7 @@
1.77
1.78 # Support the import of names from missing modules.
1.79
1.80 - self.store(alias or name, UnresolvedName(name, node.modname, self))
1.81 + self.store(alias or name, UnresolvedName(name, modname, self))
1.82
1.83 # For wildcards, obtain and store all objects from a module in the
1.84 # current namespace.
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/tests/relative/__init__.py Tue Oct 08 20:54:09 2013 +0200
2.3 @@ -0,0 +1,3 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +# vim: tabstop=4 expandtab shiftwidth=4
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/tests/relative/subpackage/__init__.py Tue Oct 08 20:54:09 2013 +0200
3.3 @@ -0,0 +1,6 @@
3.4 +#!/usr/bin/env python
3.5 +
3.6 +from ..upper import x
3.7 +from .sibling import y
3.8 +
3.9 +# vim: tabstop=4 expandtab shiftwidth=4
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/tests/relative/subpackage/sibling.py Tue Oct 08 20:54:09 2013 +0200
4.3 @@ -0,0 +1,5 @@
4.4 +#!/usr/bin/env python
4.5 +
4.6 +y = 456
4.7 +
4.8 +# vim: tabstop=4 expandtab shiftwidth=4
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/tests/relative/upper.py Tue Oct 08 20:54:09 2013 +0200
5.3 @@ -0,0 +1,5 @@
5.4 +#!/usr/bin/env python
5.5 +
5.6 +x = 123
5.7 +
5.8 +# vim: tabstop=4 expandtab shiftwidth=4
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/tests/relative_import.py Tue Oct 08 20:54:09 2013 +0200
6.3 @@ -0,0 +1,8 @@
6.4 +#!/usr/bin/env python
6.5 +
6.6 +import relative.subpackage
6.7 +
6.8 +result_123 = relative.subpackage.x
6.9 +result_456 = relative.subpackage.y
6.10 +
6.11 +# vim: tabstop=4 expandtab shiftwidth=4