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

brett.cannon python-checkins at python.org
Wed Nov 8 22:21:40 CET 2006


Author: brett.cannon
Date: Wed Nov  8 22:21:40 2006
New Revision: 52681

Modified:
   sandbox/trunk/import_in_py/importer.py
   sandbox/trunk/import_in_py/test_importer.py
Log:
Add support for modules contained withn a top-level module in the filesystem
importer.


Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py	(original)
+++ sandbox/trunk/import_in_py/importer.py	Wed Nov  8 22:21:40 2006
@@ -267,19 +267,25 @@
     def find_module(self, fullname, path=None):
         """Determine if this path entry can handle this import, and if so,
         return a loader for the module on this path entry based on the handlers
-        registered."""
+        registered.
+       
+        If the module's name is dotted then only search for the trailing
+        module's name on the path entry.
+        
+        """
         # XXX Does not worry about case-insensitive filesystems.
         for handler in self.handlers:
             for file_ext in handler.handles:
                 # XXX Backwards-incompatible to use anything but .py/.pyc
                 # files for __init__?
-                package_entry = os.path.join(self.path_entry, fullname)
+                tail_module = fullname.rsplit('.', 1)[-1]
+                package_entry = os.path.join(self.path_entry, tail_module)
                 package_name = os.path.join(package_entry,
                                             '__init__'+file_ext)
                 file_path = os.path.join(self.path_entry, package_name)
                 if os.path.isfile(file_path):
-                    return FileSystemLoader(file_path, handler, package_entry)
-                file_name = fullname + file_ext
+                    return self.loader(file_path, handler, package_entry)
+                file_name = tail_module + file_ext
                 file_path = os.path.join(self.path_entry, file_name)
                 if os.path.isfile(file_path):
                     return self.loader(file_path, handler)
@@ -690,7 +696,7 @@
         # When fromlist is not specified, return the root module (i.e., module
         # up to first dot).
         if not fromlist:
-            return sys.modules[name.partition('.')[0]]
+            return sys.modules[name.split('.', 1)[0]]
         # When fromlist is not empty, return the actual module specified in
         # the import.
         else:

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	Wed Nov  8 22:21:40 2006
@@ -282,6 +282,7 @@
         # If a module name refers to a directory with an __init__ file it
         # should be recognized as a package.
         loader = self.importer.find_module(self.pkg_name)
+        self.failUnless(isinstance(loader, mock_importer.MockPyPycLoader))
         self.failUnlessEqual(loader.file_path, self.pkg_init_path)
         self.failUnlessEqual(loader.handler, self.handler)
         self.failUnlessEqual(loader.package, self.pkg_path)
@@ -925,9 +926,10 @@
         module = self.import_(self.pkg_name)
         self.verify_package(module)
 
-    def XXX_test_package_module(self):
+    def test_package_module(self):
         # A module within a top-level package should work with the package not
         # already imported.
+        assert '.' in self.pkg_module_name
         module = self.import_(self.pkg_module_name)
         self.verify_package(module, self.pkg_module_name)
 


More information about the Python-checkins mailing list