[Python-checkins] r51984 - in python/branches/theller_modulefinder/Lib: modulefinder.py test/test_modulefinder.py

thomas.heller python-checkins at python.org
Fri Sep 22 21:34:52 CEST 2006


Author: thomas.heller
Date: Fri Sep 22 21:34:51 2006
New Revision: 51984

Modified:
   python/branches/theller_modulefinder/Lib/modulefinder.py
   python/branches/theller_modulefinder/Lib/test/test_modulefinder.py
Log:
Relative imports in the form 'from ... import name' now also work.

Modified: python/branches/theller_modulefinder/Lib/modulefinder.py
==============================================================================
--- python/branches/theller_modulefinder/Lib/modulefinder.py	(original)
+++ python/branches/theller_modulefinder/Lib/modulefinder.py	Fri Sep 22 21:34:51 2006
@@ -427,13 +427,20 @@
                         m.starimports[name] = 1
             elif what == "absolute_import":
                 fromlist, name = args
-                # XXX code missing, see above
+                # XXX
                 self._safe_import_hook(name, m, fromlist, level=0)
-                # XXX code missing, see above
+                # XXX
             elif what == "relative_import":
                 level, fromlist, name = args
-                # XXX code missing, see above
-                self._safe_import_hook(name, m, fromlist, level=level)
+                # XXX
+                if name:
+                    self._safe_import_hook(name, m, fromlist, level=level)
+                    # XXX
+                else:
+                    # XXX
+                    parent = self.determine_parent(m, level=level)
+                    self._safe_import_hook(parent.__name__, None, fromlist, level=0)
+                    # XXX
             else:
                 # We don't expect anything else from the generator.
                 raise RuntimeError(what)

Modified: python/branches/theller_modulefinder/Lib/test/test_modulefinder.py
==============================================================================
--- python/branches/theller_modulefinder/Lib/test/test_modulefinder.py	(original)
+++ python/branches/theller_modulefinder/Lib/test/test_modulefinder.py	Fri Sep 22 21:34:51 2006
@@ -107,9 +107,6 @@
                                 #from a.b.c import moduleC
                                 from .c import moduleC # a.b.moduleC
 a/b/x.py
-                                # Shouldn't this work? It doesn't seem to,
-                                # in Python:
-                                #from ..b import x
 a/b/y.py
 a/b/z.py
 a/b/g.py
@@ -122,6 +119,45 @@
 a/b/c/x.py
 """]
 
+relative_import_test_2 = [
+    "a.module",
+    ["a", "a.module",
+     "a.sys",
+     "a.b", "a.b.y", "a.b.z",
+     "a.b.c", "a.b.c.d",
+     "a.b.c.e",
+     "a.b.c.moduleC",
+     "a.b.c.f",
+     "a.b.x",
+     "a.another"],
+    [],
+    """\
+mymodule.py
+a/__init__.py
+                                from . import sys # a.sys
+a/another.py
+a/module.py
+                                from .b import y, z # a.b.y, a.b.z
+a/exceptions.py
+a/sys.py
+a/b/__init__.py
+                                from .c import moduleC # a.b.c.moduleC
+                                from .c import d # a.b.c.d
+a/b/x.py
+a/b/y.py
+a/b/z.py
+a/b/c/__init__.py
+                                from . import e # a.b.c.e
+a/b/c/moduleC.py
+                                #
+                                from . import f   # a.b.c.f
+                                from .. import x  # a.b.x
+                                from ... import another # a.another
+a/b/c/d.py
+a/b/c/e.py
+a/b/c/f.py
+"""]
+
 def open_file(path):
     ##print "#", os.path.abspath(path)
     dirname = os.path.dirname(path)
@@ -145,15 +181,15 @@
             mf.import_hook(import_this)
             if report:
                 mf.report()
-
-                opath = sys.path[:]
-                sys.path = TEST_PATH
-                try:
-                    __import__(import_this)
-                except:
-                    import traceback; traceback.print_exc()
-                sys.path = opath
-
+##                # This wouldn't work in general when executed several times:
+##                opath = sys.path[:]
+##                sys.path = TEST_PATH
+##                try:
+##                    __import__(import_this)
+##                except:
+##                    import traceback; traceback.print_exc()
+##                sys.path = opath
+##                return
             modules = set(modules)
             found = set(mf.modules.keys())
             more = list(found - modules)
@@ -178,6 +214,9 @@
         def test_relative_imports(self):
             self._do_test(relative_import_test)
 
+        def test_relative_imports_2(self):
+            self._do_test(relative_import_test_2)
+
 def test_main():
     test_support.run_unittest(ModuleFinderTest)
 


More information about the Python-checkins mailing list