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

brett.cannon python-checkins at python.org
Wed Nov 1 00:35:02 CET 2006


Author: brett.cannon
Date: Wed Nov  1 00:35:02 2006
New Revision: 52568

Modified:
   sandbox/trunk/import_in_py/importer.py
   sandbox/trunk/import_in_py/test_importer.py
Log:
Add preliminary code to handle what module to return based on fromlist.  Tests
are not done as more package support is needed.

Also broke out code that handles actual import into another method so that
__call__ can be for handling relative/absolute resolution.


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  1 00:35:02 2006
@@ -30,6 +30,9 @@
 * __path__ is not inherited in any way [introspection].
 * sys.path_importer_cache has an entry for each package directory that was
   successfully imported [introspection].
+* When importing pre-requisite modules/packages for a dotted module name,
+  calls are handled internally [introspection].
+    + sys.modules is checked, then find/load is done.
 
 * Packages take precedence over modules with the same name in the same sys.path
   entry [package essay].
@@ -577,14 +580,32 @@
                 return loader
         else:
             raise ImportError("%s not found on sys.path" % name)
+            
+    def import(self, name):
+        """Import a module."""
+        try:
+            # Attempt to get a cached version of the module from sys.modules.
+            return sys.modules[name]
+        except KeyError:
+            pass
+        try:
+            # Attempt to find a loader on sys.meta_path.
+            loader = self.search_meta_path(name)
+        except ImportError:
+            # sys.meta_path search failed.  Attempt to find a loader on
+            # sys.path.  If this fails then module cannot be found.
+            loader = self.search_sys_path(name)
+        # A loader was found.  It is the loader's responsibility to have put an
+        # entry in sys.modules.
+        return loader.load_module(name)
 
     def __call__(self, name, globals={}, locals={}, fromlist=[], level=-1):
-        """Import a module.
+        """Import a module after resolving relative/absolute import issues.
 
         'name' is the dotted name of the module/package to import.  'globals'and
         'locals' are the global and local namespace dictionaries of the caller
         (only 'globals' is used to introspect the __path__ attribute of the calling
-        module).  fromlist is any specific objects that are to eventually be put
+        module).  fromlist lists any specific objects that are to eventually be put
         into the namespace (e.g., ``from for.bar import baz`` would have baz in the
         fromlist).  'level' is set to -1 if both relative and absolute imports are
         supported, 0 if only for absolute, and positive values represent the number
@@ -593,10 +614,11 @@
         When 'name' is a dotted name, there are two different situations to
         consider.  One is when the fromlist is empty.  In this situation the import
         imports and returns the name up to the first dot.  All subsequent names are
-        imported but set at attributes as needed.  When fromlist is not empty then
+        imported but set as attributes as needed.  When fromlist is not empty then
         the module represented by the full dotted name is returned.
 
         """
+        # XXX Ignores fromlist.
         # XXX Does not handle packages yet, which means no absolute/relative imports
         # or fromlist worries.
 
@@ -607,15 +629,12 @@
         # Import submodules; short-circuits search if module is already
         # in sys.modules.
         # XXX
-
-
-        # Try meta_path entries.
-        try:
-            # Attempt to find a loader on sys.meta_path.
-            loader = self.search_meta_path(name)
-        except ImportError:
-            # sys.meta_path search failed.  Attempt to find a loader on
-            # sys.path.  If this fails then module cannot be found.
-            loader = self.search_sys_path(name)
-        # A loader was found.
-        return loader.load_module(name)
+        module = self.import(name)
+        # 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]]
+        # When fromlist is not empty, return the actual module specified in
+        # the import.
+        else:
+            return module
\ No newline at end of file

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  1 00:35:02 2006
@@ -581,6 +581,19 @@
         sys.path_importer_cache.clear()
         self.failUnlessRaises(ImportError, self.import_.search_sys_path, 'sys')
         self.failUnless(sys.path_importer_cache[path_entry] is None)
+        
+        
+class PackageImportTests(unittest.TestCase):
+    
+    """Test cases involving package semantics."""
+    
+    def test_empty_fromlist(self):
+        # An empty fromlist means that the root module is returned.
+        pass
+        
+    def test_nonempty_fromlist(self):
+        # A fromlist with values should return the specified module.
+        pass
 
 
 def test_main():


More information about the Python-checkins mailing list