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

brett.cannon python-checkins at python.org
Tue Oct 10 22:29:22 CEST 2006


Author: brett.cannon
Date: Tue Oct 10 22:29:21 2006
New Revision: 52278

Modified:
   sandbox/trunk/import_in_py/test_importer.py
Log:
Refactor superclass for helping with py/pyc tests to generate both the py and
pyc files since specifying individual handlers makes sure that one does not
override the other when importing.


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 22:29:21 2006
@@ -110,34 +110,37 @@
         sys.stdout = self._orig_stdout
         
         
-class SourceFileTests(unittest.TestCase):
+class PyPycTests(unittest.TestCase):
     
-    """Base class to help in generating a fresh source  file."""
+    """Base class to help in generating a fresh source and bytecode file."""
     
     def setUp(self):
         """Generate the path to a temporary file to test with."""
         self.module = 'source_tester'
         self.directory = tempfile.gettempdir()
-        self.file_path = os.path.join(self.directory, self.module+'.py')
+        self.source_path = os.path.join(self.directory, self.module+'.py')
         self.attr_name = 'test_attr'
         self.attr_value = None
-        with open(self.file_path, 'w') as py_file:
+        with open(self.source_path, 'w') as py_file:
             py_file.write('%s = %r' % (self.attr_name, self.attr_value))
+        py_compile.compile(self.source_path, doraise=True)
+        self.bytecode_path = self.source_path + ('c' if __debug__ else 'o')
 
     def tearDown(self):
         """If the temporary path was used, make sure to clean up."""
-        os.remove(self.file_path)
+        os.remove(self.source_path)
+        os.remove(self.bytecode_path)
         
-    def verify_module(self, module):
+    def verify_module(self, module, file_path):
         """Verify that the module is the one created during setup and has the
         expected attributes and values."""
         self.failUnlessEqual(module.__name__, self.module)
-        self.failUnlessEqual(module.__file__, self.file_path)
+        self.failUnlessEqual(module.__file__, file_path)
         self.failUnless(hasattr(module, self.attr_name))
         self.failUnlessEqual(getattr(module, self.attr_name), self.attr_value)
 
 
-class SourceHandlerTests(SourceFileTests):
+class SourceHandlerTests(PyPycTests):
 
     """Test the Python source code handler."""
     
@@ -154,59 +157,19 @@
         # Should be able to handle a module that is directly pointed at.
         new_module = new.module(self.module)
         self.handler.handle_file(new_module, self.module,
-                                  None, self.file_path)
-        self.verify_module(new_module)
-                             
-                             
-class FileSystemLoaderTests(SourceFileTests):
-    
-    """Test the filesystem loader."""
-    
-    def setUp(self):
-        """Create a fresh loader per run."""
-        super(self.__class__, self).setUp()
-        self.loader = importer.FileSystemLoader(self.file_path,
-                                                importer.PySourceHandler())
-                                                
-    def test_load_module_fresh(self):
-        # Test a basic module load where there is no sys.modules entry.
-        try:
-            del sys.modules[self.module]
-        except KeyError:
-            pass
-        new_module = self.loader.load_module(self.module)
-        self.verify_module(new_module)
-        
-    def test_load_module_sys_modules(self):
-        # Make sure that the loader returns the module from sys.modules if it
-        # is there.
-        new_module = new.module(self.module)
-        sys.modules[self.module] = new_module
-        loaded_module = self.loader.load_module(self.module)
-        self.failUnless(loaded_module is new_module)
-        
-        
-class BytecodeHandlerTests(SourceFileTests):
+                                  None, self.source_path)
+        self.verify_module(new_module, self.source_path)
+
+
+class BytecodeHandlerTests(PyPycTests):
     
     """Tests for the bytecode handler."""
     
     def setUp(self):
         """Make sure that the module has its bytecode generated."""
         super(self.__class__, self).setUp()
-        # Bytecode file cleaned up by SourceFileTests.tearDown() thanks to
-        # assigning the bytecode file's path to self.file_path.
-        # But that does leave the original .py file in place; handle that in
-        # tearDown().
-        py_compile.compile(self.file_path, doraise=True)
-        self.file_path += 'c' if __debug__ else 'o'
         self.handler = importer.PyBytecodeHandler()
         
-    def tearDown(self):
-        """Clean up the .py file since self.file_path is set to the bytecode
-        file and that is handled in the superclass."""
-        super(self.__class__, self).tearDown()
-        os.remove(self.file_path[:-1])
-        
     def test_handles_attr(self):
         # 'handles' should return 'pyc' or 'pyo' depending on __debug__.
         try:
@@ -222,9 +185,37 @@
         # generated.
         new_module = new.module(self.module)
         self.handler.handle_file(new_module, self.module, None,
-                                    self.file_path)
-        self.verify_module(new_module)
-        
+                                    self.bytecode_path)
+        self.verify_module(new_module, self.bytecode_path)
+
+
+class FileSystemLoaderTests(PyPycTests):
+
+    """Test the filesystem loader."""
+
+    def setUp(self):
+        """Create a fresh loader per run."""
+        super(self.__class__, self).setUp()
+        self.loader = importer.FileSystemLoader(self.source_path,
+                                                importer.PySourceHandler())
+
+    def test_load_module_fresh(self):
+        # Test a basic module load where there is no sys.modules entry.
+        try:
+            del sys.modules[self.module]
+        except KeyError:
+            pass
+        new_module = self.loader.load_module(self.module)
+        self.verify_module(new_module, self.source_path)
+
+    def test_load_module_sys_modules(self):
+        # Make sure that the loader returns the module from sys.modules if it
+        # is there.
+        new_module = new.module(self.module)
+        sys.modules[self.module] = new_module
+        loaded_module = self.loader.load_module(self.module)
+        self.failUnless(loaded_module is new_module)
+ 
 
 def test_main():
     test_support.run_unittest(


More information about the Python-checkins mailing list