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

brett.cannon python-checkins at python.org
Mon Feb 5 23:20:32 CET 2007


Author: brett.cannon
Date: Mon Feb  5 23:20:31 2007
New Revision: 53640

Modified:
   sandbox/trunk/import_in_py/test_controlled_importlib.py
Log:
Fix up previous extension module whitelisting and add testing in
ControlledImport integration tests.


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	Mon Feb  5 23:20:31 2007
@@ -118,6 +118,15 @@
     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(ExtensionHelper):
 
@@ -128,14 +137,15 @@
         self.failUnless(not self.imp_load.cannot_handle(self.whitelist))
         self.failUnless(self.imp_load.cannot_handle(self.blacklist))
 
-    def handle_code(self):
+    def test_handle_code(self):
         # Should raise ImportError if the module is not whitelisted, otherwise
         # return the module.
-        module = self.imp_load.handle_code(None, self.whitelist,
-                self.imp_load.handles[0])
+        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, self.imp_load.handles[0])
+                self.blacklist, blacklist_path)
 
 
 class FileSystemImporterTests(ExtensionHelper):
@@ -146,14 +156,8 @@
     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.
-        for entry in sys.path:
-            blacklist_filename = self.blacklist + self.imp_load.handles[0]
-            blacklist_path = os.path.join(entry, blacklist_filename)
-            if os.path.exists(blacklist_path):
-                sys_entry = entry
-                break
-        else:
-            raise test_support.TestSkipped("sys.path empty")
+        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)
@@ -257,8 +261,14 @@
                 self.failUnlessRaises(ImportError, import_, blacklisted, level=0)
 
     def test_extension_whitelisting(self):
-        # XXX
-        pass
+        whitelist = 'time'
+        blacklist = 'datetime'
+        with remove_from_sys_modules(whitelist, blacklist):
+            import_ = controlled_importlib.ControlledImport([], [], [whitelist])
+            module = import_(whitelist, level=0)
+            self.failUnlessEqual(module.__name__, whitelist)
+            self.failUnlessRaises(ImportError, import_, blacklist, level=0)
+
 
     def test_pyc_blocking(self):
         # XXX


More information about the Python-checkins mailing list