[Python-checkins] r57118 - in sandbox/trunk/import_in_py: _importlib.py tests/test_fs_loader.py

brett.cannon python-checkins at python.org
Fri Aug 17 04:46:54 CEST 2007


Author: brett.cannon
Date: Fri Aug 17 04:46:52 2007
New Revision: 57118

Added:
   sandbox/trunk/import_in_py/tests/test_fs_loader.py   (contents, props changed)
Modified:
   sandbox/trunk/import_in_py/_importlib.py
Log:
Implement a loader for extension modules.


Modified: sandbox/trunk/import_in_py/_importlib.py
==============================================================================
--- sandbox/trunk/import_in_py/_importlib.py	(original)
+++ sandbox/trunk/import_in_py/_importlib.py	Fri Aug 17 04:46:52 2007
@@ -296,13 +296,35 @@
             return None
 
 
+class ExtensionFileLoader(object):
+
+    """Provide the ability to load an extension module."""
+
+    def __init__(self, path):
+        self._path = path
+
+    def load_module(self, fullname):
+        """Load an extension module."""
+        # Check sys.modules first.
+        if fullname in sys.modules:
+            return sys.modules[fullname]
+        try:
+            module = imp.load_dynamic(fullname, self._path)
+            module.__loader__ = self
+        except:
+            # If an error occurred, don't leave a partially initialized module.
+            if fullname in sys.modules:
+                del sys.modules[fullname]
+            raise
+        return module
+
+
 class ExtensionFileImporter(FileImporter):
 
     """Importer for extension files."""
 
     _possible_package = False
-    #_loader = ExtensionFileLoader
-    _loader = lambda path: FileSystemLoader(path, ExtensionFileHandler)
+    _loader = ExtensionFileLoader
 
     def __init__(self, path_entry):
         # Assigning to _file_types here instead of at the class level because
@@ -323,7 +345,6 @@
         super(self.__class__, self).__init__(path_entry)
 
 
-
 # XXX Deprecated.
 class FileSystemImporter(object):
 

Added: sandbox/trunk/import_in_py/tests/test_fs_loader.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/import_in_py/tests/test_fs_loader.py	Fri Aug 17 04:46:52 2007
@@ -0,0 +1,43 @@
+import importlib
+
+from tests.ext_help import find_ext_location
+
+import os
+import sys
+from test import test_support
+import unittest
+
+
+class LoaderBasics(unittest.TestCase):
+
+    def test_cache_check(self):
+        # sys.modules should always be checked first.
+        cached = sys.modules.keys()[0]
+        cached_module = sys.modules[cached]
+        loader = importlib.ExtensionFileLoader('.')
+        module = loader.load_module(cached)
+        self.assert_(module is cached_module)
+
+
+class ExtensionFileLoaderTests(LoaderBasics):
+
+    def test_basic(self):
+        # Should be able to import an extension module.
+        module_name = 'datetime'
+        if module_name in sys.modules:
+            del sys.modules[module_name]
+        path, filename = find_ext_location(module_name)
+        module_path = os.path.join(path, filename)
+        loader = importlib.ExtensionFileLoader(module_path)
+        module = loader.load_module(module_name)
+        self.assertEqual(module.__name__, module_name)
+        self.assertEqual(module.__file__, module_path)
+        self.assertEqual(module.__loader__, loader)
+
+
+def test_main():
+    test_support.run_unittest(ExtensionFileLoaderTests)
+
+
+if __name__ == '__main__':
+    test_main()


More information about the Python-checkins mailing list