[Python-checkins] r57208 - sandbox/trunk/import_in_py/tests/py_help.py sandbox/trunk/import_in_py/tests/test_fs_loader.py

brett.cannon python-checkins at python.org
Mon Aug 20 03:32:19 CEST 2007


Author: brett.cannon
Date: Mon Aug 20 03:32:19 2007
New Revision: 57208

Modified:
   sandbox/trunk/import_in_py/tests/py_help.py
   sandbox/trunk/import_in_py/tests/test_fs_loader.py
Log:
Test that the source loader uses bytecode when it is good and leaves the source
code alone.


Modified: sandbox/trunk/import_in_py/tests/py_help.py
==============================================================================
--- sandbox/trunk/import_in_py/tests/py_help.py	(original)
+++ sandbox/trunk/import_in_py/tests/py_help.py	Mon Aug 20 03:32:19 2007
@@ -146,7 +146,9 @@
         self.failUnlessEqual(getattr(module, self.test_attr[0]),
                                 self.test_attr[1])
         if module.__name__ == self.pkg_name:
-            self.failUnlessEqual(module.__file__, self.pkg_init_path)
+            self.failUnless(module.__file__ in
+                            [os.path.join(self.pkg_path, '__init__'+ext)
+                                for ext in [self.py_ext, self.pyc_ext]])
             self.failUnlessEqual(module.__path__, [self.pkg_path])
             # Module in top-level package.
             if actual_name and self.pkg_module_name in actual_name:

Modified: sandbox/trunk/import_in_py/tests/test_fs_loader.py
==============================================================================
--- sandbox/trunk/import_in_py/tests/test_fs_loader.py	(original)
+++ sandbox/trunk/import_in_py/tests/test_fs_loader.py	Mon Aug 20 03:32:19 2007
@@ -4,6 +4,7 @@
 from tests.py_help import TestPyPycPackages
 
 import os
+import py_compile
 import sys
 from test import test_support
 import unittest
@@ -27,7 +28,7 @@
         loaded = loader.load_module(name)
         self.assert_(loaded is sys.modules[name])
         self.assertEqual(loaded.__name__, name)
-        self.assertEqual(loaded.__file__, path)
+        self.assert_(loaded.__file__.startswith(path))
         self.assertEqual(loaded.__loader__, loader)
 
     def ImportError_on_bad_name(self, loader, bad_name, extra_methods=[]):
@@ -105,12 +106,25 @@
         def fail_loading(loader, name, *args):
             sys.modules[name] = 'Should not exist'
             raise ImportError('initial failed load')
+        if self.module_name in sys.modules:
+            del sys.modules[self.module_name]
         loader = importlib._PyFileLoader(self.module_name, self.py_path, False)
         loader._handler = fail_loading
         self.assertRaises(ImportError, loader.load_module, self.module_name)
         self.assert_(self.module_name not in sys.modules)
 
 
+def log_call(instance, method_name):
+    """Log a method call."""
+    method = getattr(instance, method_name)
+    if not hasattr(instance, '_log'):
+        instance._log = []
+    def logger(*args, **kwargs):
+        instance._log.append(method_name)
+        method(*args, **kwargs)
+    setattr(instance, method_name, logger)
+
+
 class PyFileLoaderLoadingTests(TestPyPycPackages):
 
     """Test that the source loader uses the proper file.
@@ -126,7 +140,16 @@
     def test_pyc_over_py(self):
         # If a bytecode file is good, don't even bother with the source
         # (top-level or package).
-        raise NotImplementedError
+        for name, path, is_pkg in [(self.module_name, self.py_path, False),
+                                    (self.pkg_name, self.pkg_init_path, True)]:
+            if name in sys.modules:
+                del sys.modules[name]
+            py_compile.compile(path)
+            loader = importlib._PyFileLoader(name, path, is_pkg)
+            log_call(loader, 'get_source')
+            found = loader.load_module(name)
+            self.assert_('get_source' not in loader._log)
+            self.verify_package(found, name)
 
     def test_only_good_pyc(self):
         # Should be able to load even if only bytecode is available (top-level


More information about the Python-checkins mailing list