[Python-checkins] r57250 - in sandbox/trunk/import_in_py: controlled_importlib.py tests/test_controlled_importlib.py

brett.cannon python-checkins at python.org
Tue Aug 21 07:36:28 CEST 2007


Author: brett.cannon
Date: Tue Aug 21 07:36:28 2007
New Revision: 57250

Modified:
   sandbox/trunk/import_in_py/controlled_importlib.py
   sandbox/trunk/import_in_py/tests/test_controlled_importlib.py
Log:
Move controlled_importlib over to the new importers and loaders.


Modified: sandbox/trunk/import_in_py/controlled_importlib.py
==============================================================================
--- sandbox/trunk/import_in_py/controlled_importlib.py	(original)
+++ sandbox/trunk/import_in_py/controlled_importlib.py	Tue Aug 21 07:36:28 2007
@@ -13,6 +13,8 @@
 """
 import importlib
 import _importlib
+import functools
+import imp
 import sys
 
 # Need to inject 'open' into importlib as it will most likely be removed from
@@ -65,24 +67,39 @@
     pass
 
 
-class WhitelistExtHandler(importlib.ExtensionFileHandler):
+class WhitelistExtensionImporter(Whitelister, importlib.ExtensionFileImporter):
 
-    """Add whitelisting to the extension module handler."""
+    """Whitelisting importer for extension modules."""
 
-    def __init__(self, whitelist):
-        self._whitelist = frozenset(whitelist)
-        super(WhitelistExtHandler, self).__init__()
+    pass
 
-    def cannot_handle(self, name):
-        if name in self._whitelist:
-            return False
-        return True
 
-    def handle_code(self, loader, mod_name, extension_path, package=None):
-        if mod_name in self._whitelist:
-            return super(WhitelistExtHandler, self).handle_code(loader, mod_name,
-                    extension_path, package)
-        raise ImportError("not allowed to load module")
+class PyOnlyFileLoader(importlib._PyFileLoader):
+
+    """Only load source files; no bytecode."""
+
+    def __init__(self, *args, **kwargs):
+        """Remove the bytecode path."""
+        super(PyOnlyFileLoader, self).__init__(*args, **kwargs)
+        try:
+            del self._bytecode_path
+        except AttributeError:
+            pass
+
+    def write_bytecode(self, *args, **kwargs):
+        """Do not write out any bytecode."""
+        return False
+
+
+class PyOnlyFileImporter(importlib.PyFileImporter):
+
+    """Only find source files; no bytecode."""
+
+    _loader = PyOnlyFileLoader
+
+    def __init__(self, path_entry):
+        super(PyOnlyFileImporter, self).__init__(path_entry)
+        self._suffixes = importlib.suffix_list(imp.PY_SOURCE)
 
 
 class ControlledImport(importlib.Import):
@@ -107,11 +124,12 @@
         sys.meta_path.append(WhitelistBuiltin(safe_builtins))
         sys.meta_path.append(WhitelistFrozen(safe_frozen))
         # Whitelist extension modules on sys.path.
-        ext_handler = WhitelistExtHandler(safe_extensions)
+        ext_importer = functools.partial(WhitelistExtensionImporter,
+                                            safe_extensions)
         # Allow all .py files but not .pyc files on sys.path.
-        py_handler = importlib.PyPycHandler(bytecode_handles=())
-        # Put .py and extension handlers into a path_hooks factory.
-        fs_factory = importlib.FileSystemFactory(ext_handler, py_handler)
+        # XXX
+        fs_factory = importlib.chaining_fs_path_hook(ext_importer,
+                                                        PyOnlyFileImporter)
         sys.path_hooks.append(fs_factory)
 
     def module_from_cache(self, name):

Modified: sandbox/trunk/import_in_py/tests/test_controlled_importlib.py
==============================================================================
--- sandbox/trunk/import_in_py/tests/test_controlled_importlib.py	(original)
+++ sandbox/trunk/import_in_py/tests/test_controlled_importlib.py	Tue Aug 21 07:36:28 2007
@@ -2,6 +2,7 @@
 import importlib
 
 from tests import mock_importlib
+from tests.ext_help import find_ext_location
 from tests.py_help import TestPyPycPackages
 
 from contextlib import contextmanager, nested
@@ -112,57 +113,24 @@
             self.failUnlessRaises(ImportError, imp_load.load_module, blacklist)
 
 
-class ExtensionHelper(unittest.TestCase):
-
-    whitelist = 'time'
-    blacklist = 'datetime'
-
-    def setUp(self):
-        self.imp_load = controlled_importlib.WhitelistExtHandler([self.whitelist])
-
-    def find_module(self, module):
-        for entry in sys.path:
-            module_filename= module + self.imp_load.handles[0]
-            module_path= os.path.join(entry, module_filename)
-            if os.path.exists(module_path):
-                return module_path
-        else:
-            raise test_support.TestSkipped("sys.path empty")
+class WhitelistExtensionsTests(unittest.TestCase):
 
+    """Test the whitelisting of extension modules."""
 
-class WhitelistExtensionsTests(ExtensionHelper):
+    def test_whitelist(self):
+        whitelist = 'time'
+        blacklist = 'datetime'
+        assert (find_ext_location(whitelist)[0] ==
+                find_ext_location(blacklist)[0])
+        directory = find_ext_location(whitelist)[0]
+        importer = controlled_importlib.WhitelistExtensionImporter([whitelist],
+                                                                    directory)
+        result = importer.find_module(whitelist)
+        self.assert_(result is not None)
+        result = importer.find_module(blacklist)
+        self.assert_(result is None)
 
-    """Test the whitelisting of extension modules."""
 
-    def test_cannot_handle(self):
-        # Should return False for modules on the whitelist, True otherwise.
-        self.failUnless(not self.imp_load.cannot_handle(self.whitelist))
-        self.failUnless(self.imp_load.cannot_handle(self.blacklist))
-
-    def test_handle_code(self):
-        # Should raise ImportError if the module is not whitelisted, otherwise
-        # return the module.
-        whitelist_path = self.find_module(self.whitelist)
-        module = self.imp_load.handle_code(None, self.whitelist, whitelist_path)
-        self.failUnlessEqual(module.__name__, self.whitelist)
-        blacklist_path = self.find_module(self.blacklist)
-        self.failUnlessRaises(ImportError, self.imp_load.handle_code, None,
-                self.blacklist, blacklist_path)
-
-
-class FileSystemImporterTests(ExtensionHelper):
-
-    """Make sure that the filesystem importer respects what the handler's
-    cannot_handle method says."""
-
-    def test_find_module(self):
-        # Make sure that when the handler is used in a filesystem importer that
-        # whitelisting is still respected for find_module.
-        blacklist_path = self.find_module(self.blacklist)
-        sys_entry = os.path.split(blacklist_path)[0]
-        importer = importlib.FileSystemImporter(sys_entry, self.imp_load)
-        self.failUnless(importer.find_module(self.whitelist))
-        self.failUnless(importer.find_module(self.blacklist) is None)
 
 
 @contextmanager
@@ -289,6 +257,7 @@
             import_ = controlled_importlib.ControlledImport()
             os.unlink(self.py_path)
             assert os.path.exists(self.pyc_path)
+            assert not os.path.exists(self.py_path)
             self.failUnlessRaises(ImportError, import_, self.module_name,
                     level=0)
 
@@ -348,7 +317,6 @@
                                 WhitelistBuiltinTests,
                                 WhitelistFrozenTests,
                                 WhitelistExtensionsTests,
-                                FileSystemImporterTests,
                                 ControlledImportMethodTests,
                                 ControlledImportUsageTests)
 


More information about the Python-checkins mailing list