[Python-checkins] r52156 - sandbox/trunk/import_in_py/importer.py sandbox/trunk/import_in_py/test_importer.py
brett.cannon
python-checkins at python.org
Thu Oct 5 01:05:22 CEST 2006
Author: brett.cannon
Date: Thu Oct 5 01:05:22 2006
New Revision: 52156
Modified:
sandbox/trunk/import_in_py/importer.py
sandbox/trunk/import_in_py/test_importer.py
Log:
Add frozen module importer. No tests since don't know of any frozen modules to
test against in Python's test suite.
Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py (original)
+++ sandbox/trunk/import_in_py/importer.py Thu Oct 5 01:05:22 2006
@@ -39,7 +39,7 @@
return None
# Could also check sys.builtin_module_names to see if the import is for
# a built-in module.
- if imp.is_builtin(fullname):
+ elif imp.is_builtin(fullname):
return cls
else:
return None
@@ -52,7 +52,38 @@
module in sys.modules before initializing the module.
"""
+ if path is not None:
+ raise TypeError("loader does not expect an argument for 'path'")
try:
return sys.modules[fullname]
except KeyError:
return imp.init_builtin(fullname)
+
+
+class FrozenImporter(object):
+
+ """sys.meta_path class for importing frozen modules.
+
+ Based off of BuiltinImporter.
+
+ """
+
+ @classmethod
+ def find_module(cls, fullname, path=None):
+ """Return a loader for frozen modules if the module is a frozen
+ module."""
+ if path is not None:
+ return None
+ elif imp.is_frozen(fullname):
+ return cls
+ else:
+ return None
+
+ @staticmethod
+ def load_module(fullname, path=None):
+ if path is not None:
+ raise TypeError("loader does not expect an argument for 'path'")
+ try:
+ return sys.modules[fullname]
+ except KeyError:
+ return imp.init_frozen(fullname)
Modified: sandbox/trunk/import_in_py/test_importer.py
==============================================================================
--- sandbox/trunk/import_in_py/test_importer.py (original)
+++ sandbox/trunk/import_in_py/test_importer.py Thu Oct 5 01:05:22 2006
@@ -39,6 +39,13 @@
self.failUnless(hasattr(sys_module, 'version'))
+class FrozenImporterTests(unittest.TestCase):
+
+ """Test the frozen module importer."""
+
+ pass
+
+
def test_main():
test_support.run_unittest(
BuiltinImporterTests,
More information about the Python-checkins
mailing list