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

thomas.heller python-checkins at python.org
Fri Sep 22 20:23:05 CEST 2006


Author: thomas.heller
Date: Fri Sep 22 20:23:04 2006
New Revision: 51983

Modified:
   python/branches/theller_modulefinder/Lib/modulefinder.py
   python/branches/theller_modulefinder/Lib/test/test_modulefinder.py
Log:
More progress with relative imports.
'from ...x import y' now works from inside an __init__.py file.

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 20:23:04 2006
@@ -137,9 +137,14 @@
             self.msgout(4, "determine_parent -> None")
             return None
         pname = caller.__name__
-        if level >= 1:
+        if level >= 1: # relative import
             if caller.__path__:
                 level -= 1
+            if level == 0:
+                parent = self.modules[pname]
+                assert parent is caller
+                self.msgout(4, "determine_parent ->", parent)
+                return parent
             if pname.count(".") < level:
                 raise ImportError, "relative importpath too deep"
             pname = ".".join(pname.split(".")[:-level])

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 20:23:04 2006
@@ -90,36 +90,36 @@
      "a.b", "a.b.y", "a.b.z",
      "a.b.c", "a.b.c.moduleC",
      "a.b.c.d", "a.b.c.e",
+     "a.b.x",
      "exceptions"],
     [],
-# The 'from ... import name' constructs stil fail'
     """\
 mymodule.py
 a/__init__.py
-                                ##from . import sys # a.sys
+                                from .b import y, z # a.b.y, a.b.z
 a/module.py
                                 from __future__ import absolute_import # __future__
                                 import exceptions # exceptions
-                                #from . import x # a.x
-                                from .b import y, z
-                                #from . import sys # a.sys
 a/exceptions.py
 a/sys.py
 a/b/__init__.py
-                                #from .c import moduleC
-                                from a.b.c import moduleC
+                                from ..b import x # a.b.x
+                                #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
 a/b/c/__init__.py
                                 from ..c import e # a.b.c.e
 a/b/c/moduleC.py
-                                #
-                                #from .. import c
-                                #from .. import x # a.b.x
                                 from ..c import d # a.b.c.d
 a/b/c/d.py
 a/b/c/e.py
+a/b/c/x.py
 """]
 
 def open_file(path):


More information about the Python-checkins mailing list