[Python-checkins] r53622 - sandbox/trunk/import_in_py/importlib.py sandbox/trunk/import_in_py/test_importlib.py

brett.cannon python-checkins at python.org
Fri Feb 2 00:15:48 CET 2007


Author: brett.cannon
Date: Fri Feb  2 00:15:47 2007
New Revision: 53622

Modified:
   sandbox/trunk/import_in_py/importlib.py
   sandbox/trunk/import_in_py/test_importlib.py
Log:
Abstract out getting a module from the cache, sys.modules.  Allows for more
fine-grained control over whether the existence of a module in sys.modules
should allow for it to be returned or should be blocked for security reasons.


Modified: sandbox/trunk/import_in_py/importlib.py
==============================================================================
--- sandbox/trunk/import_in_py/importlib.py	(original)
+++ sandbox/trunk/import_in_py/importlib.py	Fri Feb  2 00:15:47 2007
@@ -692,6 +692,16 @@
                 return loader
         else:
             raise ImportError("No module found named %s" % name)
+
+    def module_from_cache(self, name):
+        """Try to return the named module from sys.modules.
+        
+        Return False if the module is not in the cache. 
+        """
+        if name in sys.modules:
+            return sys.modules[name]
+        else:
+            return False
             
     def _import_module(self, name, path=None):
         """Import the specified module with no handling of parent modules.
@@ -700,12 +710,12 @@
         import was attempted and failed) then ImportError is raised.
         
         """
-        if name in sys.modules:
-            value = sys.modules[name]
-            if value is None:
+        cached_module = self.module_from_cache(name)
+        if cached_module is not False:
+            if cached_module is None:
                 raise ImportError("relative import redirect")
             else:
-                return value
+                return cached_module
         try:
             # Attempt to find a loader on sys.meta_path.
             loader = self._search_meta_path(name, path)

Modified: sandbox/trunk/import_in_py/test_importlib.py
==============================================================================
--- sandbox/trunk/import_in_py/test_importlib.py	(original)
+++ sandbox/trunk/import_in_py/test_importlib.py	Fri Feb  2 00:15:47 2007
@@ -1141,6 +1141,17 @@
         for level in (-1, 0):
             self.failUnlessRaises(ValueError, self.importer, '', {}, {}, level)
 
+    def test_module_from_cache(self):
+        # If a value is in sys.modules it should be returned (no matter the
+        # object type), else return False.
+        value = "a 'module'"
+        mod_name = "module name"
+        sys.modules[mod_name] = value
+        returned = self.importer.module_from_cache(mod_name)
+        self.failUnless(returned is value)
+        returned = self.importer.module_from_cache(mod_name + 'asdfeddf')
+        self.failUnless(returned is False)
+
           
 class ImportMetaPathTests(ImportHelper):
     


More information about the Python-checkins mailing list