[Python-checkins] r53625 - sandbox/trunk/import_in_py/controlled_importlib.py sandbox/trunk/import_in_py/importlib.py sandbox/trunk/import_in_py/test_controlled_importlib.py sandbox/trunk/import_in_py/test_importlib.py
brett.cannon
python-checkins at python.org
Sat Feb 3 06:18:41 CET 2007
Author: brett.cannon
Date: Sat Feb 3 06:18:40 2007
New Revision: 53625
Modified:
sandbox/trunk/import_in_py/controlled_importlib.py
sandbox/trunk/import_in_py/importlib.py
sandbox/trunk/import_in_py/test_controlled_importlib.py
sandbox/trunk/import_in_py/test_importlib.py
Log:
Add a post-import method. This allows for the stripping of __loader__ from
newly imported modules so that the global namespace of the loader is not
exposed.
Also add tests for module_from_cache.
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 Sat Feb 3 06:18:40 2007
@@ -84,3 +84,12 @@
raise ImportError("module names starting with '.' cannot be "
"imported")
return importlib.Import.module_from_cache(self, name)
+
+ def post_import(self, module):
+ """Strip off the __loader__ attribute so that the object's global
+ namespace is not exposed."""
+ try:
+ del module.__loader__
+ except AttributeError:
+ pass
+ return module
Modified: sandbox/trunk/import_in_py/importlib.py
==============================================================================
--- sandbox/trunk/import_in_py/importlib.py (original)
+++ sandbox/trunk/import_in_py/importlib.py Sat Feb 3 06:18:40 2007
@@ -702,6 +702,10 @@
return sys.modules[name]
else:
return False
+
+ def post_import(self, module):
+ """Perform any desired post-import processing on the module."""
+ return module
def _import_module(self, name, path=None):
"""Import the specified module with no handling of parent modules.
@@ -725,7 +729,7 @@
loader = self._search_std_path(name, path)
# A loader was found. It is the loader's responsibility to have put an
# entry in sys.modules.
- return loader.load_module(name)
+ return self.post_import(loader.load_module(name))
def _import_full_module(self, name):
"""Import a module along with its parent modules and set into
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 Sat Feb 3 06:18:40 2007
@@ -1,5 +1,7 @@
import controlled_importlib
+import mock_importlib
+from contextlib import contextmanager
import StringIO
import sys
import unittest
@@ -106,11 +108,77 @@
self.failUnlessRaises(ImportError, imp_load.load_module, blacklist)
+ at contextmanager
+def mutate_sys_modules(module, name):
+ try:
+ old_module = sys.modules.get(name)
+ sys.modules[name] = module
+ yield
+ finally:
+ if old_module:
+ sys.modules[name] = old_module
+ else:
+ del sys.modules[name]
+
+
+class ControlledImportMethodTests(unittest.TestCase):
+
+ """Test explicit methods of ControlledImport."""
+
+ def setUp(self):
+ self.import_ = controlled_importlib.ControlledImport([], [], [])
+
+ def test_module_from_cache(self):
+ # Importing of module names with a leading dot should not occur.
+ module = 'module'
+ module_name = '.blocked'
+ assert module_name.startswith('.')
+ with mutate_sys_modules(module, module_name):
+ self.failUnlessRaises(ImportError, self.import_.module_from_cache,
+ module_name)
+
+ def test_post_import(self):
+ # Any __loader__ attribute should be indiscriminately removed.
+ module = mock_importlib.MockModule()
+ self.failUnless(self.import_.post_import(module) is module)
+ module.__loader__ = None
+ stripped_module = self.import_.post_import(module)
+ self.failUnless(stripped_module is module)
+ self.failUnless(not hasattr(stripped_module, '__loader__'))
+
+
+class ControlledImportUsageTests(unittest.TestCase):
+
+ """Make sure that usage of ControlledImport works properly."""
+
+ def test_block_dot_modules(self):
+ # Modules with a leading dot should not be imported.
+ pass
+
+ def test_builtin_whitelisting(self):
+ pass
+
+ def test_frozen_whitelisting(self):
+ pass
+
+ def test_extension_whitelisting(self):
+ pass
+
+ def test_pyc_blocking(self):
+ pass
+
+ def test_py(self):
+ # XXX try importing something with the same name as a built-in that is
+ # not whitelisted.
+ pass
+
def test_main():
test_support.run_unittest(WhitelistTests,
WhitelistBuiltinTests,
- WhitelistFrozenTests)
+ WhitelistFrozenTests,
+ ControlledImportMethodTests,
+ ControlledImportUsageTests)
if __name__ == '__main__':
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 Sat Feb 3 06:18:40 2007
@@ -1152,6 +1152,12 @@
returned = self.importer.module_from_cache(mod_name + 'asdfeddf')
self.failUnless(returned is False)
+ def test_post_import(self):
+ # Post-import processing should do nothing but return the module
+ # unscathed.
+ module = "mod"
+ self.failUnless(self.importer.post_import(module) is module)
+
class ImportMetaPathTests(ImportHelper):
More information about the Python-checkins
mailing list