[Python-checkins] r52618 - 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
Sat Nov 4 01:25:51 CET 2006


Author: brett.cannon
Date: Sat Nov  4 01:25:51 2006
New Revision: 52618

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:
Fix parameter lists for handler to accept 'package' argument to be used to set
__path__.

Also add notes on integration tests to write along with some cleanup of some
support code.


Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py	(original)
+++ sandbox/trunk/import_in_py/importer.py	Sat Nov  4 01:25:51 2006
@@ -318,7 +318,7 @@
                 module = self.handler.handle_code(self, fullname,
                                                     self.file_path, self.package)
             except:
-                if fullname in sys.modules:                    
+                if fullname in sys.modules:
                     del sys.modules[fullname]
                 raise
             return module
@@ -414,8 +414,9 @@
         return data
 
     def handle_code(self, loader, mod_name, path, package=None):
-        """Handle creating a new module object that is initialized from Python
-        bytecode or source.
+        """Handle creating a new module object for the module mod_name that is
+        to be initialized with data from 'path' and, if a package, has a
+        package location of 'package'.
         
         The loader needs to implement several methods in order to this handler
         to be able to load needed data.  A key point with some of these methods
@@ -439,7 +440,6 @@
             written as binary or textual data.
         
         """
-        # XXX Does not worry about packages.
         source_path = None
         source_timstamp = None
         bytecode_path = None
@@ -515,9 +515,12 @@
         self.handles = tuple(suffix[0] for suffix in imp.get_suffixes()
                                 if suffix[2] == imp.C_EXTENSION)
     
-    def handle_code(self, loader, mod_name, extension_path):
+    def handle_code(self, loader, mod_name, extension_path, package=None):
         """Import an extension module."""
-        return imp.load_dynamic(mod_name, extension_path)
+        module = imp.load_dynamic(mod_name, extension_path)
+        if package is not None:
+            module.__path__ = [package]
+        return module
                                 
 
 class Import(object):
@@ -693,4 +696,4 @@
         # When fromlist is not empty, return the actual module specified in
         # the import.
         else:
-            return sys.modules[name]
\ No newline at end of file
+            return sys.modules[name]

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	Sat Nov  4 01:25:51 2006
@@ -181,6 +181,7 @@
         assert binary
         # XXX Assert proper magic number
         # XXX Assert time stamp
+        # XXX mock
         module = imp.new_module(self.module_name)
         code = marshal.loads(data[8:])
         exec 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	Sat Nov  4 01:25:51 2006
@@ -1,4 +1,3 @@
-"""XXX separate medium/large tests into a single test class."""
 from __future__ import with_statement
 import importer
 
@@ -120,7 +119,15 @@
         
 class PyPycFileHelper(unittest.TestCase):
     
-    """Base class to help in generating a fresh source and bytecode file."""
+    """Base class to help in generating a fresh source and bytecode file.
+
+    XXX refactor:
+    * better attribute names.
+    * file names that cannot be imported normally.
+    * Can easily be used by other classes that also need to generate a package.
+    * Ditch need for separate methods.
+
+    """
     
     def setUp(self):
         """Generate the path to a temporary file to test with."""
@@ -399,6 +406,7 @@
             pyc = bytecode_file.read()
         magic, timestamp, bytecode = self.handler.parse_pyc(pyc)
         code = marshal.loads(bytecode)
+        # XXX mock
         module = imp.new_module(self.module)
         exec code in module.__dict__
         self.verify_module(module)
@@ -660,6 +668,7 @@
         # If a parent module has __path__ defined it should be passed as an
         # argument during importing.
         test_path = ['<test path>']
+        # XXX mock
         pkg_module = imp.new_module('_test_pkg')
         pkg_module.__path__ = test_path
         sys.modules['_test_pkg'] = pkg_module
@@ -766,46 +775,58 @@
         
 class IntegrationTests(unittest.TestCase):
     
-    """Hold tests that involve multiple classes from 'importer' and verify
-    integration semantics.
-    
-    XXX
-    * file loader <-> py/pyc handler
-    * file loader <-> extension handler
-    * file importer -> file loader
-    * file importer -> file loader <-> py/pyc handler
-    * Importer -> importer -> loader
-    
+    """Tests that verify the default semantics are what is expected.
+
+    Tests should verify that:
+    * The proper module was returned.
+    * All expected modules were added to sys.modules.
+    * All modules imported by the call have the proper attributes.
+
     """
-    def XXX_test_default_import(self):
-        # The default initialization should work with a None entry for every
-        # sys.path entry in sys.path_importer_cache.  It should also lead to
-        # built-in, frozen, extension, .pyc, and .py files being imported if
-        # desired.
-        sys.path_importer_cache = dict((entry, None) for entry in sys.path)
-        self.clear_sys_modules('sys', '__hello__', 'time', 'token')
-        # Restore sys.path for 'time' import.
-        sys.path = self.old_sys_path
-        import_ = importer.Import()
-        # Built-ins.
-        module = import_('sys')
-        self.failUnlessEqual(module.__name__, 'sys')
-        self.failUnless(hasattr(sys, 'version'))
-        # Frozen modules.
-        try:
-            sys.stdout = StringIO.StringIO()
-            module = import_('__hello__')
-        finally:
-            sys.stdout = sys.__stdout__
-        self.failUnlessEqual(module.__name__, '__hello__')
-        # Extension modules.
-        module = import_('time')
-        self.failUnlessEqual(module.__name__, 'time')
-        self.failUnless(hasattr(module, 'sleep'))
-        # .py/.pyc files.
-        module = import_('token')
-        self.failUnlessEqual(module.__name__, 'token')
-        self.failUnless(hasattr(module, 'ISTERMINAL'))
+
+    def setUp(self):
+        """Create a test environment:
+        * .py file
+        * .pyc file
+        * package
+            + __init__.py
+            + .py file
+
+        """
+        pass
+
+    def tearDown(self):
+        """Remove all test files."""
+        pass
+
+    def XXX_test_builtin(self):
+        # Test importing a built-in module.
+        pass
+
+    def XXX_test_frozen(self):
+        # Importing a frozen module should work.
+        pass
+
+    def XXX_test_pyc_w_py(self):
+        # Should be able to import a .pyc file when a .py is also present.
+        pass
+
+    def XXX_test_pyc_wo_py(self):
+        # Importing just a .pyc file (w/ no .py) should be okay.
+        pass
+
+    def XXX_test_py(self):
+        # Importing a .py file should work and generate a .pyc file.
+        pass
+
+    def XXX_test_top_level_package(self):
+        # Should be able to import a top-level package.
+        pass
+
+    def XXX_test_package_module(self):
+        # A module within a top-level package should work with the package not
+        # already imported.
+        pass
 
 
 def test_main():


More information about the Python-checkins mailing list