[Python-checkins] r53642 - sandbox/trunk/import_in_py/controlled_importlib.py sandbox/trunk/import_in_py/test_controlled_importlib.py

brett.cannon python-checkins at python.org
Tue Feb 6 00:45:30 CET 2007


Author: brett.cannon
Date: Tue Feb  6 00:45:29 2007
New Revision: 53642

Modified:
   sandbox/trunk/import_in_py/controlled_importlib.py
   sandbox/trunk/import_in_py/test_controlled_importlib.py
Log:
Add support for importing any .py file but blocking all .pyc files.  That means
that an entry on sys.path_hooks is now added by ControlledImport.__init__.

Also added tests to make sure that __loader__ attributes are not exposed on the
modules.


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 Feb  6 00:45:29 2007
@@ -73,7 +73,7 @@
 
     def handle_code(self, loader, mod_name, extension_path, package=None):
         if mod_name in self._whitelist:
-            return super(WhitelistExtHandler, self).handle_code(loder, mod_name,
+            return super(WhitelistExtHandler, self).handle_code(loader, mod_name,
                     extension_path, package)
         raise ImportError("not allowed to load module")
 
@@ -96,9 +96,12 @@
         sys.meta_path.append(WhitelistBuiltin(safe_builtins))
         sys.meta_path.append(WhitelistFrozen(safe_frozen))
         # Whitelist extension modules on sys.path.
-        # XXX
+        ext_handler = WhitelistExtHandler(safe_extensions)
         # Allow all .py files but not .pyc files on sys.path.
-        # XXX
+        py_handler = importlib.PyPycHandler(bytecode_handles=())
+        # Put .py and extension handlers into a path_hooks factory.
+        fs_factory = importlib.FileSystemFactory(ext_handler, py_handler)
+        sys.path_hooks.append(fs_factory)
 
     def module_from_cache(self, name):
         """Override so that any module name starting with a dot raises

Modified: sandbox/trunk/import_in_py/test_controlled_importlib.py
==============================================================================
--- sandbox/trunk/import_in_py/test_controlled_importlib.py	(original)
+++ sandbox/trunk/import_in_py/test_controlled_importlib.py	Tue Feb  6 00:45:29 2007
@@ -1,6 +1,7 @@
 import controlled_importlib
 import importlib
 import mock_importlib
+import test_importlib
 
 from contextlib import contextmanager, nested
 import os
@@ -228,9 +229,13 @@
         self.failUnless(not hasattr(stripped_module, '__loader__'))
 
 
-class ControlledImportUsageTests(unittest.TestCase):
+class ControlledImportUsageTests(test_importlib.TestPyPycPackages):
 
     """Make sure that usage of ControlledImport works properly."""
+
+    def setUp(self):
+        """Create .py and .pyc files for testing purposes."""
+        super(ControlledImportUsageTests, self).setUp(faked_names=False)
     
     def test_block_dot_modules(self):
         # Modules with a leading dot should not be imported.
@@ -271,18 +276,50 @@
 
 
     def test_pyc_blocking(self):
-        # XXX
-        pass
+        # Importing of a .pyc file should fail.  Also, no .pyc should be
+        # generated.
+        with remove_from_sys_modules(self.module_name):
+            import_ = controlled_importlib.ControlledImport()
+            os.unlink(self.py_path)
+            assert os.path.exists(self.pyc_path)
+            self.failUnlessRaises(ImportError, import_, self.module_name,
+                    level=0)
 
     def test_py(self):
-        # XXX try importing something with the same name as a built-in that is
-        # not whitelisted.
-        pass
+        # Should be able to import a .py module.
+        with remove_from_sys_modules(self.module_name):
+            os.unlink(self.pyc_path)
+            import_ = controlled_importlib.ControlledImport()
+            self.failUnless(import_(self.module_name, level=0))
+
+    def test_no_pyc_creation(self):
+        # No .pyc file should be created by importing a .py file.
+        with remove_from_sys_modules(self.module_name):
+            os.unlink(self.pyc_path)
+            assert os.path.exists(self.py_path)
+            assert not os.path.exists(self.pyc_path)
+            import_ = controlled_importlib.ControlledImport()
+            module = import_(self.module_name, level=0)
+            self.failUnless(module)
+            self.failUnless(not os.path.exists(self.pyc_path))
 
     def test_no_loader_attribute(self):
         # No __loader__ attribute should be exposed on any module or package.
-        # XXX check both modules, packages, and submodules.
-        pass
+        # Purposefully skipped the sub-package to make sure that implicit
+        # imports of dependencies does not leave __loader__ on by importing a
+        # module in the sub-package.
+        module_names = [self.top_level_module_name, self.pkg_name,
+                self.pkg_module_name, self.sub_pkg_module_name]
+        assert self.sub_pkg_name not in module_names
+        with remove_from_sys_modules(*module_names):
+            import_ = controlled_importlib.ControlledImport()
+            for module_name in module_names:
+                module = import_(module_name, level=0)
+                self.failUnless(not hasattr(sys.modules[module_name],
+                '__loader__'))
+            else:
+                self.failUnless(not hasattr(sys.modules[self.sub_pkg_name],
+                '__loader__'))
 
     def test_relative_import(self):
         # A relative import within a package should not be able to circumvent


More information about the Python-checkins mailing list