[Python-checkins] r52728 - sandbox/trunk/import_in_py/importer.py sandbox/trunk/import_in_py/test_importer.py

brett.cannon python-checkins at python.org
Fri Nov 10 21:55:13 CET 2006


Author: brett.cannon
Date: Fri Nov 10 21:55:12 2006
New Revision: 52728

Modified:
   sandbox/trunk/import_in_py/importer.py
   sandbox/trunk/import_in_py/test_importer.py
Log:
Finish testing and fixing support for classic relative imports.


Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py	(original)
+++ sandbox/trunk/import_in_py/importer.py	Fri Nov 10 21:55:12 2006
@@ -713,29 +713,31 @@
             # Handle the classic style of import: relative first, then
             # absolute.
             if level == -1:
-                relative_name = self.classic_resolve_name(name, caller_name,
+                imported_name = self.classic_resolve_name(name, caller_name,
                                                             is_pkg)
                 try:
                     # Try a relative import first.
-                    self.import_full_module(relative_name)
+                    self.import_full_module(imported_name)
                     # XXX Probably need to do something about values of None
                     # being in sys.modules here.
                 except ImportError:
                     # If the relative import fails, try an absolute import.
+                    imported_name = name
                     self.import_full_module(name)
             # If using absolute imports with a relative path, only attempt with
             # the fully-resolved module name.
             else:
-                relative_name = self.resolve_name(name, caller_name, is_pkg,
+                imported_name = self.resolve_name(name, caller_name, is_pkg,
                                                     level)
-                self.import_full_module(relative_name)
+                self.import_full_module(imported_name)
         else:
+            imported_name = name
             self.import_full_module(name)
         # When fromlist is not specified, return the root module (i.e., module
         # up to first dot).
         if not fromlist:
-            return sys.modules[name.split('.', 1)[0]]
+            return sys.modules[imported_name.split('.', 1)[0]]
         # When fromlist is not empty, return the actual module specified in
         # the import.
         else:
-            return sys.modules[name]
+            return sys.modules[imported_name]

Modified: sandbox/trunk/import_in_py/test_importer.py
==============================================================================
--- sandbox/trunk/import_in_py/test_importer.py	(original)
+++ sandbox/trunk/import_in_py/test_importer.py	Fri Nov 10 21:55:12 2006
@@ -1024,14 +1024,18 @@
         module = self.import_(self.sub_pkg_module_name)
         self.verify_package(module, self.sub_pkg_module_name)
 
-    def XXX_test_classic_relative_import_in_package_init(self):
+    def test_classic_relative_import_in_package_init(self):
         # Importing within a package's __init__ file using a relative name
         # should work properly.
-        pass
+        package_globals = {'__name__':self.pkg_name, '__path__':['some_path']}
+        module = self.import_(self.module_name, package_globals, level=-1)
+        self.verify_package(module, self.pkg_module_name)
 
-    def XXX_test_classic_relative_import_in_module(self):
+    def test_classic_relative_import_in_module(self):
         # Importing using a relative name in a module in a package should work.
-        pass
+        module_globals = {'__name__':self.pkg_name + '.' + 'another_module'}
+        module = self.import_(self.module_name, module_globals, level=-1)
+        self.verify_package(module, self.pkg_module_name)
 
     def test_absolute_name_in_classic_relative_context(self):
         # Importing a module that happens to be ambiguous in terms of being


More information about the Python-checkins mailing list