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

brett.cannon python-checkins at python.org
Sun Feb 4 04:33:46 CET 2007


Author: brett.cannon
Date: Sun Feb  4 04:33:38 2007
New Revision: 53633

Modified:
   sandbox/trunk/import_in_py/importlib.py
   sandbox/trunk/import_in_py/mock_importlib.py
   sandbox/trunk/import_in_py/test_importlib.py
Log:
Add the cannot_handle method for handlers.  This gives handlers a way to say
they will not handle a module based on its name.  This has been introduced for
the purpose of allowing a whitelisting of modules based on the handler and not
the importer/loader.


Modified: sandbox/trunk/import_in_py/importlib.py
==============================================================================
--- sandbox/trunk/import_in_py/importlib.py	(original)
+++ sandbox/trunk/import_in_py/importlib.py	Sun Feb  4 04:33:38 2007
@@ -277,6 +277,8 @@
         tail_module = fullname.rsplit('.', 1)[-1]
         package_directory = os.path.join(self.path_entry, tail_module)
         for handler in self.handlers:
+            if handler.cannot_handle(fullname):
+                continue
             for file_ext in handler.handles:
                 # XXX Backwards-incompatible to use anything but .py/.pyc
                 # files for __init__?
@@ -326,6 +328,8 @@
         deal with initializing the module passed to it.
         
         """
+        if self.handler.cannot_handle(fullname):
+            raise ImportError("cannot load")
         try:
             module = self.handler.handle_code(self, fullname,
                                                 self.file_path, self.package)
@@ -424,6 +428,11 @@
         else:
             self.bytecode_handles = bytecode_handles
         self.handles = self.bytecode_handles + self.source_handles
+
+    def cannot_handle(self, name):
+        """Allow the handler to tell an importer whether it does not
+        want to handle a module."""
+        return False
         
     def new_module(self, name):
         """Retun a new module to be initialized (with __name__ set)."""
@@ -570,6 +579,11 @@
         """Set 'handles'."""
         self.handles = tuple(suffix[0] for suffix in imp.get_suffixes()
                                 if suffix[2] == imp.C_EXTENSION)
+
+    def cannot_handle(self, name):
+        """Tell an importer whether the handler cannot handle a specific
+        module."""
+        return False
     
     def handle_code(self, loader, mod_name, extension_path, package=None):
         """Import an extension module."""

Modified: sandbox/trunk/import_in_py/mock_importlib.py
==============================================================================
--- sandbox/trunk/import_in_py/mock_importlib.py	(original)
+++ sandbox/trunk/import_in_py/mock_importlib.py	Sun Feb  4 04:33:38 2007
@@ -32,6 +32,9 @@
 
     def __init__(self, *handles):
         self.handles = handles
+
+    def cannot_handle(self, name):
+        return False
     
     def handle_code(self, loader, mod_name, path, package=None):
         """Mock implementation of a handler.

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	Sun Feb  4 04:33:38 2007
@@ -495,6 +495,7 @@
     def test_sys_module_cleared_on_error(self):
         # Any entry made for module into sys.modules should be cleared upon error.
         class RaiseErrorHandler(object):
+            def cannot_handle(*args): return False
             def handle_code(*args):
                 raise ImportError
                 
@@ -602,6 +603,10 @@
             self.failUnlessEqual(self.handler.source_handles, source)
             self.failUnlessEqual(self.handler.bytecode_handles, bytecode)
             self.failUnlessEqual(self.handler.handles, bytecode + source)
+
+    def test_cannot_handle(self):
+        # Should always return False.
+        self.failUnless(not self.handler.cannot_handle('asdfddss'))
     
     def test_new_module(self):
         # Should return a new module with the proper value for __name__.
@@ -796,6 +801,10 @@
         self.ext_path = os.path.join(entry, ext_paths[0])
         self.module_name = os.path.splitext(os.path.split(self.ext_path)[1])[0]
         self.loader = mock_importlib.MockHandler()
+
+    def test_cannot_handle(self):
+        # Should always return False.
+        self.failUnless(not self.handler.cannot_handle('asdfdd'))
         
     def test_handle_code(self):
         # Make sure an extension module can be loaded.


More information about the Python-checkins mailing list