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

brett.cannon python-checkins at python.org
Thu Nov 2 23:41:26 CET 2006


Author: brett.cannon
Date: Thu Nov  2 23:41:25 2006
New Revision: 52596

Modified:
   sandbox/trunk/import_in_py/importer.py
   sandbox/trunk/import_in_py/mock_importer.py
   sandbox/trunk/import_in_py/test_importer.py
Log:
Add tests for setting __path__ by the py/pyc handler using new method parameter
lists.


Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py	(original)
+++ sandbox/trunk/import_in_py/importer.py	Thu Nov  2 23:41:25 2006
@@ -21,6 +21,9 @@
 * Raise ImportError when load_module() fails to load a module without
   raising an exception.
 * Is module returned by load_module() actually used for anything?
+    + Can always just return from sys.modules.
+* PEP 302 does not mention if __name__ must be set before any code is executed
+  or not.
 
 
 Package notes
@@ -74,13 +77,12 @@
     + One level deep.
     + Arbitrarily deep.
     + Search __path__ and not sys.path.
-#. Importing within package.
-    + Root package already imported.
-    + Root package not imported yet.
+    + Parent modules not already imported.
 #. fromlist semantics.
 #. Relative imports.
     + Py3K semantics.
     + 2.x semantics.
+    + From within a package's __init__ module.
 
 
 Things to be exposed at the Python level

Modified: sandbox/trunk/import_in_py/mock_importer.py
==============================================================================
--- sandbox/trunk/import_in_py/mock_importer.py	(original)
+++ sandbox/trunk/import_in_py/mock_importer.py	Thu Nov  2 23:41:25 2006
@@ -89,12 +89,18 @@
             pyc_ext = tuple()
         return handler(py_ext, pyc_ext)
         
-    def _handle_py(self, handler):
-        return handler.handle_code(self, self.module_name, self.py_path)
-        
-    def _handle_pyc(self, handler):
-        return handler.handle_code(self, self.module_name, self.pyc_path)
-        
+    def _handle_py(self, handler, package=None):
+        args = [self, self.module_name, self.py_path]
+        if package is not None:
+            args.append(package)
+        return handler.handle_code(*args)
+        
+    def _handle_pyc(self, handler, package=None):
+        args = [self, self.module_name, self.pyc_path]
+        if package is not None:
+            args.append(package)
+        return handler.handle_code(*args)
+
     def _verify_module(self, module, test_metadata=True):
         if not hasattr(module, 'test_attr'):
             raise test_support.TestFailed("test_attr attribute missing")

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	Thu Nov  2 23:41:25 2006
@@ -437,6 +437,7 @@
     """Test PyPycHandler.handle_code()."""
     
     def test_good_pyc_w_py(self):
+        # Test using a .pyc file that is valid and a .py file exists.
         loader = mock_importer.MockPyPycLoader.setup()
         handler = loader._create_handler(importer.PyPycHandler)
         module = loader._handle_pyc(handler)
@@ -463,6 +464,7 @@
         pass
         
     def test_py_no_pyc(self):
+        # Test importing a .py file where no .pyc path is available.
         loader = mock_importer.MockPyPycLoader.setup(pyc_exists=False)
         handler = loader._create_handler(importer.PyPycHandler)
         module = loader._handle_py(handler)
@@ -472,11 +474,21 @@
         self.failUnlessEqual(loader.log.count('read_data'), 1)
         
     def test_py_w_pyc(self):
+        # Test importing a .py file and a .pyc path is available.
         loader = mock_importer.MockPyPycLoader.setup()
         handler = loader._create_handler(importer.PyPycHandler)
         module = loader._handle_py(handler)
         loader._verify_module(module)
         self.failUnless('write_data' in loader.log)
+        
+    def test_package_init(self):
+        # Handling a package should set __path__ properly.
+        loader = mock_importer.MockPyPycLoader.setup(pyc_exists=False)
+        handler = loader._create_handler(importer.PyPycHandler)
+        pkg_path = 'pkg path'
+        module = loader._handle_py(handler, pkg_path)
+        loader._verify_module(module)
+        self.failUnlessEqual(module.__path__, [pkg_path])
 
 
 class ExtensionHandlerTests(unittest.TestCase):


More information about the Python-checkins mailing list