[Python-checkins] r52371 - sandbox/trunk/import_in_py/importer.py sandbox/trunk/import_in_py/test_importer.py
brett.cannon
python-checkins at python.org
Wed Oct 18 00:42:16 CEST 2006
Author: brett.cannon
Date: Wed Oct 18 00:42:15 2006
New Revision: 52371
Modified:
sandbox/trunk/import_in_py/importer.py
sandbox/trunk/import_in_py/test_importer.py
Log:
Implement an extension module file handler.
Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py (original)
+++ sandbox/trunk/import_in_py/importer.py Wed Oct 18 00:42:15 2006
@@ -525,3 +525,18 @@
source_location = self.get_location(source_file)
bytecode_location = self.get_location(bytecode_file)
py_compile.compile(source_location, bytecode_location, doraise=True)
+
+
+class ExtensionFileHandler(object):
+
+ """A handler for loading extension modules."""
+
+ def __init__(self):
+ """Set 'handles'."""
+ self.handles = tuple(suffix[0] for suffix in imp.get_suffixes()
+ if suffix[2] == imp.C_EXTENSION)
+
+ def handle_code(self, module, extension_file, to_handle):
+ """Import an extension module."""
+ return imp.load_dynamic(module.__name__, extension_file.name,
+ extension_file)
\ No newline at end of file
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 Wed Oct 18 00:42:15 2006
@@ -581,6 +581,37 @@
self.handler.handle_code(self.module_object, source_file, '.py')
self.verify_module(self.module_object)
self.failUnless(os.path.exists(self.bytecode_path))
+
+
+class ExtensionHandlerTests(unittest.TestCase):
+
+ """Test that extension modules can be loaded."""
+
+ def setUp(self):
+ """Find an extension module to test against."""
+ self.handler = importer.ExtensionFileHandler()
+ for entry in sys.path:
+ if not os.path.isdir(entry):
+ continue
+ ext_paths = [ext for ext in os.listdir(entry)
+ if any(True for suffix in self.handler.handles
+ if ext.endswith(suffix))]
+ if ext_paths:
+ break
+ else:
+ raise test_support.TestSkipped("not extension modules found")
+ self.ext_path = os.path.join(entry, ext_paths[0])
+ self.module_name = os.path.splitext(os.path.split(self.ext_path)[1])[0]
+
+ def test_handle_code(self):
+ # Make sure an extension module can be loaded.
+ new_module = new.module(self.module_name)
+ with open(self.ext_path, 'rb') as ext_file:
+ module = self.handler.handle_code(new_module, ext_file,
+ self.handler.handles[0])
+ # There should be at least one attribute that does not start with '_'.
+ self.failUnless(any(True for attr in dir(module)
+ if not attr.startswith('_')))
def test_main():
@@ -591,6 +622,7 @@
FileSystemImporterTests,
PyPycBaseHandlerTests,
PyPycFileHandlerTests,
+ ExtensionHandlerTests,
)
More information about the Python-checkins
mailing list