[Python-checkins] r52265 - sandbox/trunk/import_in_py/importer.py sandbox/trunk/import_in_py/test_importer.py

brett.cannon python-checkins at python.org
Tue Oct 10 06:46:51 CEST 2006


Author: brett.cannon
Date: Tue Oct 10 06:46:50 2006
New Revision: 52265

Modified:
   sandbox/trunk/import_in_py/importer.py
   sandbox/trunk/import_in_py/test_importer.py
Log:
Add tests for the bytecode handler (along with fixes needed to make it work and
a few notes about its current shortcomings).


Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py	(original)
+++ sandbox/trunk/import_in_py/importer.py	Tue Oct 10 06:46:50 2006
@@ -32,6 +32,7 @@
 
 import imp
 import sys
+import marshal
 # XXX Importing os will not work in the end as it is implemented in Python
 # itself.
 import os
@@ -204,7 +205,9 @@
         module.__file__ = file_path
         module.__name__ = fullname
         with open(file_path, 'rb') as bytecode_file:
+            # XXX No verification of magic number.
             magic = bytecode_file.read(4)
+            # XXX No verification of timestamp.
             mtime = bytecode_file.read(4)
             compiled_code = marshal.load(bytecode_file)
         exec compiled_code in module.__dict__

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	Tue Oct 10 06:46:50 2006
@@ -7,6 +7,7 @@
 import os
 import tempfile
 import new
+import py_compile
 
 
 class BuiltinFrozen_Tester(unittest.TestCase):
@@ -184,12 +185,43 @@
         loaded_module = self.loader.load_module(self.module)
         self.failUnless(loaded_module is new_module)
         
+        
+class BytecodeHandlerTests(SourceFileTests):
+    
+    """Tests for the bytecode handler."""
+    
+    def setUp(self):
+        """Make sure that the module has its bytecode generated."""
+        super(self.__class__, self).setUp()
+        py_compile.compile(self.file_path, doraise=True)
+        self.file_path += 'c' if __debug__ else 'o'
+        self.handler = importer.PyBytecodeHandler()
+        
+    def test_handles_attr(self):
+        # 'handles' should return 'pyc' or 'pyo' depending on __debug__.
+        try:
+            importer.__debug__ = True
+            self.failUnlessEqual(self.handler.handles, 'pyc')
+            importer.__debug__ = False
+            self.failUnlessEqual(self.handler.handles, 'pyo')
+        finally:
+            del importer.__debug__
+        
+    def test_handle_file(self):
+        # Should be able to handle a simple bytecode file that is freshly
+        # generated.
+        new_module = new.module(self.module)
+        self.handler.handle_file(new_module, self.module, None,
+                                    self.file_path)
+        self.verify_module(new_module)
+        
 
 def test_main():
     test_support.run_unittest(
                 BuiltinImporterTests,
                 FrozenImporterTests,
                 SourceHandlerTests,
+                BytecodeHandlerTests,
                 FileSystemLoaderTests,
             )
 


More information about the Python-checkins mailing list