[Python-checkins] r57252 - in sandbox/trunk/import_in_py: _importlib.py tests/mock_importlib.py tests/test_ext_handler.py tests/test_fs_importer.py tests/test_py_handler.py

brett.cannon python-checkins at python.org
Tue Aug 21 07:47:25 CEST 2007


Author: brett.cannon
Date: Tue Aug 21 07:47:24 2007
New Revision: 57252

Removed:
   sandbox/trunk/import_in_py/tests/test_ext_handler.py
   sandbox/trunk/import_in_py/tests/test_py_handler.py
Modified:
   sandbox/trunk/import_in_py/_importlib.py
   sandbox/trunk/import_in_py/tests/mock_importlib.py
   sandbox/trunk/import_in_py/tests/test_fs_importer.py
Log:
Move over to the new importers and loader for source and extension modules and
remove the old ones.

Still need to run all of Python's unit tests using the importer to verify
everything works correctly.


Modified: sandbox/trunk/import_in_py/_importlib.py
==============================================================================
--- sandbox/trunk/import_in_py/_importlib.py	(original)
+++ sandbox/trunk/import_in_py/_importlib.py	Tue Aug 21 07:47:24 2007
@@ -205,47 +205,6 @@
     return chained_fs_path_hook
 
 
-# XXX Deprecated.
-class FileSystemFactory(object):
-
-    """Factory function for sys.path_hooks for directory entries on sys.path.
-    
-    The path to be handled, if it is a filesystem directory, is used to
-    initialize a new FileSystemImporter for that path entry.  An instantiated
-    object stores various handlers that are to be used to decide if the path
-    entry contains the module to be imported.
-
-    XXX Possible optimization would be to get the directory's contents and make
-    sure that only handlers for the files in the directory are given to the
-    returned importer.  This would kill the ability to add a new module into
-    the directory during run-time that is a new type of file, though.
-    
-    """
-
-    def __init__(self, *handlers):
-        """Store handlers to be used in attempting to import.
-
-        The order that the handlers are specified are the order in which
-        the handlers will be passed on to any instantiated importers.
-
-        """
-        self.handlers = handlers
-
-    def __call__(self, path_entry):
-        """If path_entry is a directory, return an importer object for it, else
-        raise ImportError.
-   
-        Both relative and absolute paths are accepted (the former because of
-        interactive interpreter usage).
-    
-        """
-        absolute_path = _path_absolute(path_entry)
-        if _path_isdir(absolute_path):
-            return FileSystemImporter(absolute_path, *self.handlers)
-        else:
-            raise ImportError("can only handle directories")
-
-
 def check_name(method):
     """Decorator to verify that the module being requested matches the one the
     loader can handle."""
@@ -608,358 +567,6 @@
         super(PyFileImporter, self).__init__(path_entry)
 
 
-# XXX Deprecated.
-class FileSystemImporter(object):
-
-    """Importer for the filesystem using the passed-in handlers."""
-
-    def __init__(self, path_entry, *handlers):
-        """Store the path this importer handles and the handlers to use.
-
-        The order of the handlers determines precedence of file types.
-
-        """
-        self.path_entry = path_entry
-        self.handlers = handlers
-        self.loader = FileSystemLoader
-
-    def find_module(self, fullname, path=None):
-        """Determine if this path entry can handle this import, and if so,
-        return a loader for the module on this path entry based on the handlers
-        registered.
-       
-        If the module's name is dotted then only search for the trailing
-        module's name on the path entry.  An importer is already created
-        for each directory in the __path__ attribute for a package.
-
-        'path' is ignored as that is meant for meta_path entries only.
-        
-        """
-        tail_module = fullname.rsplit('.', 1)[-1]
-        package_directory = _path_join(self.path_entry, tail_module)
-        for handler in self.handlers:
-            if handler.cannot_handle(fullname):
-                continue
-            for file_ext in handler.handles:
-                # XXX Backwards-incompatible to use anything but .py/.pyc
-                # files for __init__?
-                init_filename = '__init__' + file_ext
-                package_init = _path_join(package_directory, init_filename)
-                # Check if it is a package with an __init__ file.
-                if (_path_isfile(package_init) and
-                        _case_ok(self.path_entry, tail_module) and
-                        _case_ok(package_directory, init_filename)):
-                    return self.loader(package_init, handler, package_directory)
-                # See if it is a module.
-                file_name = tail_module + file_ext
-                file_path = _path_join(self.path_entry, file_name)
-                if (_path_isfile(file_path) and
-                        _case_ok(self.path_entry, file_name)):
-                    return self.loader(file_path, handler)
-        else:
-            # Raise a warning if it matches a directory w/o an __init__ file.
-            if (_path_isdir(package_directory) and
-                    _case_ok(self.path_entry, tail_module)):
-                warnings.warn("Not importing directory %s: missing __init__.py"
-                                    % package_directory,
-                                ImportWarning)
-            return None
-
-
-# XXX Deprecated.
-class FileSystemLoader(object):
-
-    """Loader for the filesystem.
-    
-    It implements the needed API for the PyPycHandler.
-    
-    """
-
-    def __init__(self, file_path, handler, package=None):
-        """Store the path to the file to use for the import and the handler to
-        use along with whether this is a package (this does not include modules
-        within a package)."""
-        self.file_path = file_path
-        self.handler = handler
-        self.package = package
-
-    def load_module(self, fullname):
-        """Load the module from self.path using self.handler.
-        
-        The handler is expected to implement a handle_code method that will
-        deal with initializing the module passed to it.
-        
-        """
-        if self.handler.cannot_handle(fullname):
-            raise ImportError("cannot load")
-        try:
-            module = self.handler.handle_code(self, fullname,
-                                                self.file_path, self.package)
-        # Don't leave any partially initialised modules behind.
-        except:
-            if fullname in sys.modules:
-                del sys.modules[fullname]
-            raise
-        else:
-            return module
-        
-    def mod_time(self, path):
-        """Return the modification time for the specified path as an integer.
-        
-        This method is required as part of the interface needed for
-        PyPycHandler.
-        
-        """
-        return int(_os.stat(path).st_mtime)
-        
-    def split_path(self, path):
-        """Split the specified path into a base path and the type of the
-        path.
-        
-        This method is required for PyPycHandler.
-        
-        """
-        splits = path.rpartition('.')
-        return splits[0], splits[1] + splits[2]
-        
-    def create_path(self, base_path, type_, must_exist=False):
-        """Create a new path based on a base path and requested path type.
-        
-        If must_exist is True, the path must already exist in order to return a
-        path instead of None.
-
-        This method is required for PyPycHandler.
-        
-        """
-        path = base_path + type_
-        if must_exist:
-            path = path if _path_exists(path) else None
-        return path
-        
-    def read_data(self, path, binary=False):
-        """Open the path and return the data read from it in the specified
-        format.
-        
-        This method is required for PyPycHandler.
-        
-        """
-        with open(path, 'rb' if binary else 'U') as data_file:
-            data = data_file.read()
-        return data
-        
-    def write_data(self, data, path, binary=False):
-        """Write data to a specified path as either binary or textual data.
-       
-        If the path cannot be accessed, then exit silently.
-
-        This method is required for PyPycHandler.
-        
-        """
-        try:
-            with open(path, 'wb' if binary else 'w') as data_file:
-                data_file.write(data)
-        except IOError as exc:
-            if exc.errno == EACCES:
-                pass
-            else:
-                raise
-
-
-# XXX Deprecated.
-class PyPycHandler(object):
-    
-    """Handle reading Python bytecode/source with writing out of bytecode as
-    needed.
-    
-    The handle_code method is the key entry point for this class.
-    
-    """
-    
-    def __init__(self, source_handles=None, bytecode_handles=None):
-        """Specify the file types for source and bytecode to be handled by this
-        handler and set 'handles' appropriately as tuples (empty tuples are
-        acceptable).
-
-        Not specifying handles will lead to reasonable defaults being used.
-        
-        """
-        # XXX Move over to use imp.get_suffixes(); make sure no bad assumptions
-        # made about suffix being a one-item tuple.
-        if source_handles is None:
-            self.source_handles = ('.py',)
-        else:
-            self.source_handles = source_handles
-        if bytecode_handles is None:
-            self.bytecode_handles = ('.pyc',) if __debug__ else ('.pyo',)
-        else:
-            self.bytecode_handles = bytecode_handles
-        self.handles = self.bytecode_handles + self.source_handles
-
-    def cannot_handle(self, name):
-        """Allow the handler to tell an importer whether it does not
-        want to handle a module."""
-        return False
-        
-    def new_module(self, name):
-        """Retun a new module to be initialized (with __name__ set)."""
-        return imp.new_module(name)
-        
-    def parse_pyc(self, data):
-        """Split data into magic number, timestamp, and bytecode."""
-        return data[:4], _r_long(data[4:8]), data[8:]
-
-    def check_magic(self, magic):
-        """Check whether the magic number is correct or not for the current
-        running interpreter."""
-        return imp.get_magic() == magic
-        
-    def code_from_bytecode(self, bytecode):
-        """Create a code object from bytecode.
-        
-        ValueError is expected to be raised if a code object can not be created
-        from the bytecode.
-        
-        """
-        return marshal.loads(bytecode)
-        
-    def code_from_source(self, source, path):
-        """Create a code object from source."""
-        return compile(source, str(path), 'exec')
-        
-    def create_pyc(self, bytecode, timestamp):
-        """Create data to be written out for a .pyc file."""
-        data = imp.get_magic()
-        data += _w_long(timestamp)
-        data += marshal.dumps(bytecode)
-        return data
-
-    def handle_code(self, loader, mod_name, path, package=None):
-        """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 the needed data.  A key point with some of these
-        methods is the idea of opaque code objects which are not directly
-        touched by the handler but are passed back to the loader so as to allow
-        for a way to keep state:
-        
-        * split_path(path)
-            Take in an opaque path object and split it into a base path and a
-            string representing the type of the path (which should be in the
-            'handles' attribute of the handler).
-        * create_path(base_path, type_, must_exist)
-            Create an opaque path object from a base path and the type of path
-            that is desired.  If must_exist is True the path must already
-            exist.
-        * mod_time(path)
-            Return the last modification time for a path as an integer.
-        * read_data(path, binary)
-            Read the data from the specified path.  'binary' is a boolean that
-            flags whether the data should be read as binary data or text data.
-        * write_data(data, path, binary)
-            Write data to a path.  'binary' flags whether the data should be
-            written as binary or textual data.
-        
-        """
-        source_path = None
-        source_timestamp = None
-        bytecode_path = None
-        module = sys.modules.get(mod_name)
-        if module is None:
-            module = self.new_module(mod_name)
-            sys.modules[mod_name] = module
-        # __file__, __path__, and __loader__ *must* be set on the module before
-        # any code is executed by the import.  __name__ is set by new_module.
-        module.__loader__ = loader
-        module.__file__ = path
-        if package is not None:
-            module.__path__ = [package]
-        base_path, type_ = loader.split_path(path)
-        if type_ in self.bytecode_handles:
-            # Attempt to use bytecode.
-            bytecode_path = path
-            source_path = None
-            pyc_data = loader.read_data(path, True)
-            magic, pyc_timestamp, bytecode = self.parse_pyc(pyc_data)
-            # Try to find corresponding source code.
-            for source_handle in self.source_handles:
-                source_path = loader.create_path(base_path, source_handle, True)
-                if source_path:
-                    break
-            try:
-                # Verify that the magic number is valid.
-                if not self.check_magic(magic):
-                    raise ImportError("bad magic number")
-                # Verify that the bytecode is not stale.
-                if source_path:
-                    source_timestamp = loader.mod_time(source_path)
-                    if pyc_timestamp < source_timestamp:
-                        raise ImportError("bytcode is stale")
-                # Bytecode is valid.
-                module.__file__ = path
-                try:
-                    code_object = self.code_from_bytecode(bytecode)
-                except ValueError:
-                    # Since bad bytecode halts the import entirely having the
-                    # source code is useless.  Use this fact to signal that
-                    # the ImportError being raised should propagate.
-                    source_path = None
-                    raise ImportError('Non-code object in %s' %
-                                        str(bytecode_path))
-                exec code_object in module.__dict__
-                return module
-            except ImportError:
-                # No source code to use to recover from bad bytecode.
-                if not source_path:
-                    raise
-        if type_ in self.source_handles:
-            # Asked directly to handle source code.
-            source_path = path
-        # Get the source code.
-        source = loader.read_data(source_path, False)
-        module.__file__ = source_path
-        code = self.code_from_source(source, source_path)
-        # See if there is a corresponding path to bytecode.
-        if not bytecode_path:
-            if self.bytecode_handles:
-                bytecode_handle = self.bytecode_handles[0]
-                bytecode_path = loader.create_path(base_path,
-                                                    bytecode_handle)
-            source_timestamp = loader.mod_time(source_path)
-        # If there is a possible path to bytecode, generate .pyc file.
-        if bytecode_path:
-            if not source_timestamp:
-                source_timestamp = loader.mod_time(source_path)
-            pyc = self.create_pyc(code, source_timestamp)
-            loader.write_data(pyc, bytecode_path, True)
-        exec code in module.__dict__
-        return module
-
-
-# XXX Deprecated.
-class ExtensionFileHandler(object):
-    
-    """A handler for loading extension modules."""
-    
-    def __init__(self):
-        """Set 'handles'."""
-        self.handles = tuple(suffix[0] for suffix in imp.get_suffixes()
-                                if suffix[2] == imp.C_EXTENSION)
-
-    def cannot_handle(self, name):
-        """Tell an importer whether the handler cannot handle a specific
-        module."""
-        return False
-    
-    def handle_code(self, loader, mod_name, extension_path, package=None):
-        """Import an extension module."""
-        module = imp.load_dynamic(mod_name, extension_path)
-        if package is not None:
-            module.__path__ = [package]
-        return module
-
-
 class ImportLockContext(object):
 
     """Context manager for the import lock."""
@@ -997,8 +604,8 @@
             # Create a handler to deal with extension modules, .py, and .pyc
             # files.  Built-in and frozen modules are handled by sys.meta_path
             # entries.
-            handlers = ExtensionFileHandler(), PyPycHandler()
-            self.default_path_hook = FileSystemFactory(*handlers)
+            importers = [ExtensionFileImporter, PyFileImporter]
+            self.default_path_hook = chaining_fs_path_hook(*importers)
 
     def _search_meta_path(self, name, path=None):
         """Check the importers on sys.meta_path for a loader along with the

Modified: sandbox/trunk/import_in_py/tests/mock_importlib.py
==============================================================================
--- sandbox/trunk/import_in_py/tests/mock_importlib.py	(original)
+++ sandbox/trunk/import_in_py/tests/mock_importlib.py	Tue Aug 21 07:47:24 2007
@@ -4,6 +4,7 @@
 from test import test_support
 from importlib import _w_long
 
+
 def log_call(method):
     """Log method calls to self.log."""
     def log_and_call(self, *args, **kwargs):
@@ -11,6 +12,7 @@
         return method(self, *args, **kwargs)
     return log_and_call
 
+
 class MockModule(object):
     
     """A mock module."""
@@ -26,189 +28,6 @@
             self.__all__ = __all__
 
 
-class MockHandler(object):
-    
-    """Mock a handler."""
-
-    def __init__(self, *handles):
-        self.handles = handles
-
-    def cannot_handle(self, name):
-        return False
-    
-    def handle_code(self, loader, mod_name, path, package=None):
-        """Mock implementation of a handler.
-        
-        An object that can have arbitrary attributes attached to it must be
-        returned.
-        
-        """
-        module = sys.modules.get(mod_name)
-        if module is None:
-            module = self
-            sys.modules[mod_name] = self
-        try:
-            module.loader = loader
-            module.module_name = mod_name
-            module.path = path
-            module.package = package
-        except AttributeError:
-            pass
-        return module
-
-class MockPyPycLoader(object):
-    
-    """Mock loader object for testing importer.PyPycHandler.
-    
-    Opaque path objects are two-item tuple consisting of a string representing
-    the base path and another string representing the type.
-    
-    """
-    
-    def __init__(self, file_path, handler, package=None):
-        """Store arguments passed into the constructor for verification."""
-        self.file_path = file_path
-        self.handler = handler
-        self.package = package
-
-    @classmethod
-    def setup(cls, good_magic=True, good_timestamp=True, pyc_exists=True,
-                    py_exists=True, good_bytecode=True):
-        """Set up the mock loader based on possible scenarios of source and
-        bytecode combinations/issues."""
-        self = cls('<setup>', '<setup>')
-        self.module_name = 'test_module'
-        self.py_ext = "source" if py_exists else None
-        self.pyc_ext = "bytecode" if pyc_exists else None
-        self.base_path = "base path"
-        self.py_path = (self.base_path, self.py_ext)
-        self.pyc_path = (self.base_path, self.pyc_ext)
-        self.modification_time = 1
-        # Needed for read_data on source path.
-        self.attr_name = 'test_attr'
-        self.attr_value = None
-        self.source = "%s = %s" % (self.attr_name, self.attr_value)
-        code_object = compile(self.source, "<mock loader>", 'exec')
-        bytecode = marshal.dumps(code_object)
-        if good_magic:
-            pyc = imp.get_magic()
-        else:
-            magic = imp.get_magic()
-            pyc = magic[:3] + chr(ord(magic[3]) - 1)
-        if good_timestamp:
-            pyc += _w_long(self.modification_time)
-        else:
-            pyc += _w_long(self.modification_time - 1)
-        # Needed for read_data on .pyc path.
-        if good_bytecode:
-            self.pyc = pyc + bytecode
-        else:
-            self.pyc = pyc + "asd4asfasd4ae4fds"
-        self.log = []
-        return self
-        
-    def _create_handler(self, handler):
-        if self.py_ext:
-            py_ext = (self.py_ext,)
-        else:
-            py_ext = tuple()
-        if self.pyc_ext:
-            pyc_ext = (self.pyc_ext,)
-        else:
-            pyc_ext = tuple()
-        return handler(py_ext, pyc_ext)
-        
-    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")
-        if not module.test_attr is None:
-            raise test_support.TestFailed("test_attr not set to None")
-        if test_metadata:
-            assert module.__name__ == self.module_name
-            assert module.__loader__ == self
-            assert self.base_path in module.__file__
-            assert (self.pyc_ext in module.__file__ or
-                    self.py_ext in module.__file__)
-
-        return True
-            
-    def load_module(self, fullname):
-        """Return what the method was called with."""
-        return fullname
-    
-    @log_call
-    def split_path(self, path):
-        """'path' should be a two-item tuple, so just return it since the
-        caller expects two items back."""
-        assert (path[0] == self.base_path and
-                path[1] in (self.py_ext, self.pyc_ext))
-
-        return path
-    
-    @log_call
-    def create_path(self, base, type_, must_exist=False):
-        """Create a tuple from 'base' and type_."""
-        assert base == self.base_path
-        assert type_ in (self.py_ext, self.pyc_ext)
-
-        if must_exist:
-            if type_ not in (self.py_ext, self.pyc_ext):
-                return None
-            else:
-                return (base, type_)
-        else:
-            return (base, type_)
-
-    @log_call
-    def mod_time(self, path):
-        """Consider self.modification_time the baseline modification time."""
-        assert path[0] == self.base_path
-        assert path[1] == self.py_ext
-        
-        return self.modification_time
-    
-    @log_call
-    def read_data(self, path, binary):
-        """Return the proper source or binary data based on the path."""
-        assert path[0] == self.base_path
-        assert path[1] in (self.py_ext, self.pyc_ext)
-        
-        if binary:
-            assert self.pyc_ext and path[1] == self.pyc_ext
-            return self.pyc
-        else:
-            assert self.py_ext and path[1] == self.py_ext
-            return self.source
-    
-    @log_call
-    def write_data(self, data, path, binary):
-        """Should only be requested to write to a bytecode path."""
-        assert path[0] == self.base_path
-        assert self.pyc_ext
-        assert path[1] == self.pyc_ext, "%s != %s" % (path[1], self.pyc_ext)
-        assert binary
-        assert data[:4] == imp.get_magic()
-        assert _w_long(self.modification_time) == data[4:8]
-        module = MockModule(self.module_name)
-        code = marshal.loads(data[8:])
-        exec code in module.__dict__
-        assert self._verify_module(module, False)
-        
-        return None
-
-
 # Mock Importers (with optional path_hooks support).
 
 class ErrorImporter(object):

Deleted: /sandbox/trunk/import_in_py/tests/test_ext_handler.py
==============================================================================
--- /sandbox/trunk/import_in_py/tests/test_ext_handler.py	Tue Aug 21 07:47:24 2007
+++ (empty file)
@@ -1,53 +0,0 @@
-import importlib
-
-from tests import mock_importlib
-
-import os
-import os.path
-import sys
-from test.test_support import run_unittest
-import unittest
-
-
-# XXX Obsolete.
-class ExtensionHandlerTests(unittest.TestCase):
-    
-    """Test that extension modules can be loaded."""
-    
-    def setUp(self):
-        """Find an extension module to test against."""
-        self.handler = importlib.ExtensionFileHandler()
-        for entry in sys.path:
-            if not os.path.isdir(entry):
-                continue
-            ext_paths = [ext for ext in os.listdir(entry)
-                            if any(True for suffix in self.handler.handles
-                                    if ext.endswith(suffix))]
-            if ext_paths:
-                break
-        else:
-            raise test_support.TestSkipped("not extension modules found")
-        self.ext_path = os.path.join(entry, ext_paths[0])
-        self.module_name = os.path.splitext(os.path.split(self.ext_path)[1])[0]
-        self.loader = mock_importlib.MockHandler()
-
-    def test_cannot_handle(self):
-        # Should always return False.
-        self.failUnless(not self.handler.cannot_handle('asdfdd'))
-        
-    def test_handle_code(self):
-        # Make sure an extension module can be loaded.
-        module = self.handler.handle_code(self.loader, self.module_name,
-                                            self.ext_path)
-        # There should be at least one attribute that does not start with '_'.
-        self.failUnless(any(True for attr in dir(module)
-                            if not attr.startswith('_')))
-
-
-def test_main():
-    run_unittest(ExtensionHandlerTests)
-
-
-
-if __name__ == '__main__':
-    test_main()

Modified: sandbox/trunk/import_in_py/tests/test_fs_importer.py
==============================================================================
--- sandbox/trunk/import_in_py/tests/test_fs_importer.py	(original)
+++ sandbox/trunk/import_in_py/tests/test_fs_importer.py	Tue Aug 21 07:47:24 2007
@@ -201,309 +201,9 @@
         found = importer.find_module(search_for)
         self.assert_(found is None)
 
-                    
-
-
-
-
-
-
-# XXX Obsolete.
-class FileSystemFactoryTests(unittest.TestCase):
-
-    """Test the filesystem path_hooks factory function."""
-
-    def setUp(self):
-        mock_handler = mock_importlib.MockHandler('.X')
-        self.factory = importlib.FileSystemFactory(mock_handler)
-
-    def test_absolute_path_accepted(self):
-        # Passing in an absolute path to a directory should be accepted.
-        importer_ = self.factory(os.path.abspath(os.getcwd()))
-        self.failUnless(isinstance(importer_, importlib.FileSystemImporter))
-
-    def test_relative_path_accepted(self):
-        # Passing in a relative directory path should be accepted.
-        importer_ = self.factory('')
-        self.failUnless(isinstance(importer_, importlib.FileSystemImporter))
-
-    def test_bad_directory(self):
-        # Should raise ImportError when used on a non-existing directory.
-        self.failUnlessRaises(ImportError, self.factory, 'sdadfasdfsdfsadfsdf')
-
-    def test_file(self):
-        # Passing in a file should raise ImportError.
-        with open(test_support.TESTFN, 'w') as test_file:
-            test_file.write('for testing FileSystemFactory')
-        try:
-            self.failUnlessRaises(ImportError, self.factory,
-                                    test_support.TESTFN)
-        finally:
-            os.remove(test_support.TESTFN)
-
-
-# XXX Obsolete.
-class FileSystemImporterTests(TestPyPycPackages):
-
-    """Test the filesystem importer."""
-
-    def setUp(self):
-        """Create a basic importer."""
-        TestPyPycPackages.setUp(self)
-        self.handler = mock_importlib.MockHandler(self.py_ext)
-        self.importer = importlib.FileSystemImporter(self.directory,
-                                                    self.handler)
-        self.importer.loader = mock_importlib.MockPyPycLoader
-
-    def test_module_case_sensitivity(self):
-        # Case-sensitivity should always matter as long as PYTHONCASEOK is not
-        # set.
-        name_len = len(self.top_level_module_name)
-        bad_case_name = (self.top_level_module_name[:name_len/2].upper() +
-                            self.top_level_module_name[name_len/2:].lower())
-        env_guard = test_support.EnvironmentVarGuard()
-        env_guard.unset('PYTHONCASEOK')
-        with env_guard:
-            self.failUnless(not self.importer.find_module(bad_case_name))
-        if sys.platform not in ('win32', 'mac', 'darwin', 'cygwin', 'os2emx',
-                'riscos'):
-            return
-        env_guard = test_support.EnvironmentVarGuard()
-        env_guard.set('PYTHONCASEOK', '1')
-        with env_guard:
-            assert os.environ['PYTHONCASEOK']
-            self.failUnless(self.importer.find_module(bad_case_name))
-
-    def test_package_case_sensitivity(self):
-        # Case-sensitivity should always matter as long as PYTHONCASEOK is not
-        # set.
-        name_len = len(self.pkg_name)
-        bad_case_name = (self.pkg_name[:name_len/2].upper() +
-                            self.pkg_name[name_len/2:].lower())
-        bad_init_name = os.path.join(self.directory, self.pkg_name,
-                                        '__INit__.py')
-        env_guard = test_support.EnvironmentVarGuard()
-        env_guard.unset('PYTHONCASEOK')
-        with env_guard:
-            self.failUnless(not self.importer.find_module(bad_case_name))
-            os.unlink(self.pkg_init_path)
-            with open(bad_init_name, 'w') as init_file:
-                init_file.write('# Test case-sensitivity of imports.')
-            self.failUnless(not self.importer.find_module(self.pkg_name))
-        if sys.platform not in ('win32', 'mac', 'darwin', 'cygwin', 'os2emx',
-                'riscos'):
-            return
-        os.unlink(bad_init_name)
-        with open(self.pkg_init_path, 'w') as init_file:
-            init_file.write('# Used for testing import.')
-        env_guard = test_support.EnvironmentVarGuard()
-        env_guard.set('PYTHONCASEOK', '1')
-        with env_guard:
-            assert os.environ['PYTHONCASEOK']
-            self.failUnless(self.importer.find_module(bad_case_name))
-            with open(bad_init_name, 'w') as init_file:
-                init_file.write('# Used to test case-insensitivity of import.')
-            self.failUnless(self.importer.find_module(self.pkg_name))
-
-    def test_find_module_single_handler(self):
-        # Having a single handler should work without issue.
-        loader = self.importer.find_module(self.module_name)
-        self.failUnless(isinstance(loader, mock_importlib.MockPyPycLoader))
-        self.failUnlessEqual(loader.file_path, self.py_path)
-        self.failUnless(isinstance(loader.handler, mock_importlib.MockHandler))
-        self.failUnless(loader.package is None)
-
-    def test_find_module_cannot_find(self):
-        # Should return None if it can't find the module.
-        found = self.importer.find_module('gobbledeegook')
-        self.failUnlessEqual(found, None)
-
-    def test_find_module_multiple_handlers(self):
-        # Modules should be found based on the order of the handlers.
-        # mock
-        first_handler = mock_importlib.MockHandler('.not_found')
-        second_handler = mock_importlib.MockHandler(self.py_ext)
-        fs_importer = importlib.FileSystemImporter(self.directory,
-                                                  first_handler,
-                                                  second_handler)
-        fs_importer.loader = mock_importlib.MockPyPycLoader
-        loader = fs_importer.find_module(self.module_name)
-        self.failUnless(isinstance(loader, mock_importlib.MockPyPycLoader))
-        self.failUnlessEqual(loader.file_path, self.py_path)
-        self.failUnless(isinstance(loader.handler, mock_importlib.MockHandler))
-        
-    def test_pkg_discovery(self):
-        # If a module name refers to a directory with an __init__ file it
-        # should be recognized as a package.
-        loader = self.importer.find_module(self.pkg_name)
-        self.failUnless(isinstance(loader, mock_importlib.MockPyPycLoader))
-        self.failUnlessEqual(loader.file_path, self.pkg_init_path)
-        self.failUnlessEqual(loader.handler, self.handler)
-        self.failUnlessEqual(loader.package, self.pkg_path)
-
-    def test_pkg_before_module(self):
-        # Importing a name that is represented as both a package and a module
-        # should give precedence to the package.
-        module_path = os.path.join(self.directory, self.pkg_name + self.py_ext)
-        with open(module_path, 'w') as test_file:
-            test_file.write("blah")
-        try:
-            loader = self.importer.find_module(self.pkg_name)
-            self.failUnlessEqual(loader.file_path, self.pkg_init_path)
-        finally:
-            os.remove(module_path)
-            
-    def test_directory_no_init_warning(self):
-        # If a directory matches for a package name but lacks an __init__.py
-        # file then raise ImportWarning.
-        os.remove(self.pkg_init_path)
-        with test_support.guard_warnings_filter():
-            warnings.simplefilter('error', ImportWarning)
-            self.failUnlessRaises(ImportWarning, self.importer.find_module,
-                                    self.pkg_name)
-
-
-class FileSystemLoaderMockEnv(unittest.TestCase):
-    
-    """Helper for settting up mock environment for testing
-    importlib.FileSystemLoader."""
-
-    def setUp(self):
-        """Create a fresh loader per run."""
-        mock_handler = mock_importlib.MockHandler()
-        self.test_path = "<test path>"
-        self.pkg_path = "<test pkg>"
-        self.pkg_init_path = os.path.join(self.pkg_path, '__init__.test')
-        self.module_name = "test_module_name"
-        self.pkg_name = "test_pkg"
-        self.loader = importlib.FileSystemLoader(self.test_path,
-                                                mock_handler)
-                                                
-    def tearDown(self):
-        """Make sure that there is no straggling modules in sys.modules."""
-        try:
-            del sys.modules[self.module_name]
-        except KeyError:
-            pass
-        try:
-            del sys.modules[self.pkg_name]
-        except KeyError:
-            pass
-
-
-class FileSystemLoaderBlackBoxTests(FileSystemLoaderMockEnv):
-
-    """Test the filesystem loader's PEP 302 API compliance."""
-
-    def test_load_module_fresh(self):
-        # Test a basic module load where there is no sys.modules entry.
-        new_module = self.loader.load_module(self.module_name)
-        self.failUnlessEqual(new_module.loader, self.loader)
-        self.failUnlessEqual(new_module.module_name, self.module_name)
-        self.failUnlessEqual(new_module.path, self.test_path)
-
-    def test_load_module_sys_modules(self):
-        # Make sure that the loader returns the module from sys.modules if it
-        # is there.
-        sys.modules[self.module_name] = self.module_name
-        loaded_module = self.loader.load_module(self.module_name)
-        self.failUnless(loaded_module is self.module_name)
-        
-    def test_sys_module_cleared_on_error(self):
-        # Any entry made for module into sys.modules should be cleared upon error.
-        class RaiseErrorHandler(object):
-            def cannot_handle(*args): return False
-            def handle_code(*args):
-                raise ImportError
-                
-        loader = importlib.FileSystemLoader(self.test_path, RaiseErrorHandler())
-        try:
-            loader.load_module(self.module_name)
-        except ImportError:
-            self.failUnless(self.module_name not in sys.modules)
-            
-    def test_package_init(self):
-        # Test that instantiating a loader for handling a package works
-        # properly.
-        pkg_loader = importlib.FileSystemLoader(self.pkg_init_path,
-                                                mock_importlib.MockHandler(),
-                                                self.pkg_path)
-        module = pkg_loader.load_module(self.pkg_name)
-        self.failUnlessEqual(module.loader, pkg_loader)
-        self.failUnlessEqual(module.path, self.pkg_init_path)
-        self.failUnlessEqual(module.module_name, self.pkg_name)
-        self.failUnlessEqual(module.package, self.pkg_path)
-            
-            
-class FileSystemLoaderWhiteBoxTests(FileSystemLoaderMockEnv):
-    
-    """Test the filesystem loader's non-PEP 302 API."""
-    
-    def setUp(self):
-        """Set up a test file."""
-        super(FileSystemLoaderWhiteBoxTests, self).setUp()
-        self.directory = tempfile.gettempdir()
-        self.file_ext = ".test"
-        self.module_name = "import_test"
-        self.file_name = self.module_name + self.file_ext
-        self.file_path = os.path.join(self.directory, self.file_name)
-        self.data = "File used for testing 'importlib' module.\r\n42"
-        with open(self.file_path, 'w') as test_file:
-            test_file.write(self.data)
-            
-    def tearDown(self):
-        """Delete the test file."""
-        super(FileSystemLoaderWhiteBoxTests, self).tearDown()
-        try:
-            os.remove(self.file_path)
-        except OSError:
-            pass
-
-    def test_mod_time(self):
-        # Modification time for the passed-in path should match the file.
-        true_mod_time = os.stat(self.file_path).st_mtime
-        mod_time = self.loader.mod_time(self.file_path)
-        self.failUnless(isinstance(mod_time, (int, long)))
-        self.failUnlessEqual(mod_time, true_mod_time)
-        
-    def test_split_path(self):
-        # Should split a path just like os.path.splitext().
-        true_split_path = os.path.splitext(self.file_path)
-        split_path = self.loader.split_path(self.file_path)
-        self.failUnlessEqual(split_path, true_split_path)
-        
-    def test_create_path(self):
-        # Should work like concatenating two strings.
-        true_path = self.file_path
-        path = self.loader.create_path(*self.loader.split_path(
-                                                        self.file_path))
-        self.failUnlessEqual(path, true_path)
-        self.failUnlessEqual(None, self.loader.create_path('sadfasdfd', 'aaa',
-                                                            True))
-        self.failUnlessEqual('ab', self.loader.create_path('a', 'b'))
-        
-    def test_read_data(self):
-        # Should be able to read a file.
-        data = self.loader.read_data(self.file_path, False)
-        self.failUnlessEqual(data, self.data.replace('\r', ''))
-        data = self.loader.read_data(self.file_path, True)
-        self.failUnlessEqual(data, self.data)
-        test_text = '1\r\n2'
-        
-    def test_write_data(self):
-        # Should be able to write a file.
-        self.loader.write_data(self.data, self.file_path, True)
-        with open(self.file_path, 'rb') as test_file:
-            data = test_file.read()
-        self.failUnlessEqual(data, self.data)
-        # XXX How to test data written with/without 'b'?
-
 
 def test_main():
     test_support.run_unittest(PyFileImporterTests, ExtensionFileImporterTests)
-    test_support.run_unittest(
-    #FileSystemFactoryTests, FileSystemImporterTests,
-            FileSystemLoaderBlackBoxTests, FileSystemLoaderWhiteBoxTests)
 
 
 if __name__ == '__main__':

Deleted: /sandbox/trunk/import_in_py/tests/test_py_handler.py
==============================================================================
--- /sandbox/trunk/import_in_py/tests/test_py_handler.py	Tue Aug 21 07:47:24 2007
+++ (empty file)
@@ -1,223 +0,0 @@
-from __future__ import with_statement
-
-import importlib
-
-from tests import mock_importlib
-from tests.py_help import TestPyPycFiles
-from importlib import _r_long
-
-import imp
-import marshal
-import sys
-from test.test_support import run_unittest
-import unittest
-
-
-class PyPycHandlerSupportingMethodTests(TestPyPycFiles):
-    
-    """Test the py/pyc handler."""
-    
-    def setUp(self):
-        TestPyPycFiles.setUp(self)
-        self.handler = importlib.PyPycHandler()
-
-    def test_init(self):
-        # Test __init__ usage.
-        proper_bytecode_ext = '.pyc' if __debug__ else '.pyo'
-        for source_ext, bytecode_ext in ((None, None), (('A',), None),
-                                         (None, ('A',)), (('A',), ('B',))):
-            handler = importlib.PyPycHandler(source_ext, bytecode_ext)
-            if source_ext is None:
-                source = ('.py',)
-            if bytecode_ext is None:
-                bytecode = (proper_bytecode_ext,)
-            self.failUnlessEqual(self.handler.source_handles, source)
-            self.failUnlessEqual(self.handler.bytecode_handles, bytecode)
-            self.failUnlessEqual(self.handler.handles, bytecode + source)
-
-    def test_cannot_handle(self):
-        # Should always return False.
-        self.failUnless(not self.handler.cannot_handle('asdfddss'))
-    
-    def test_new_module(self):
-        # Should return a new module with the proper value for __name__.
-        module = self.handler.new_module('test')
-        self.failUnlessEqual('test', module.__name__)
-        self.failUnless(isinstance(module, importlib.__class__))
-        
-    def test_parse_pyc(self):
-        # Split up data from .pyc file for the magic number, timestamp,
-        # and bytecode.
-        with open(self.pyc_path, 'rb') as bytecode_file:
-            pyc = bytecode_file.read()
-        magic, timestamp, bytecode = self.handler.parse_pyc(pyc)
-        code = marshal.loads(bytecode)
-        module = mock_importlib.MockModule(self.module_name, self.py_path)
-        exec code in module.__dict__
-        self.verify_module(module, self.py_path)
-        
-    def test_check_magic(self):
-        # Compare a number against the magic number.
-        magic_number = imp.get_magic()
-        self.failUnless(self.handler.check_magic(magic_number))
-        smaller_number = magic_number[:3] + chr(ord(magic_number[3]) - 1)
-        self.failUnless(not self.handler.check_magic(smaller_number))
-        larger_number = magic_number[:3] + chr(ord(magic_number[3]) + 1)
-        self.failUnless(not self.handler.check_magic(larger_number))
-        
-    def test_code_from_bytecode(self):
-        # Create a code object from bytecode; raises ValueError if code object
-        # cannot be created.
-        code_object = self.handler.code_from_bytecode(self.bytecode)
-        module = mock_importlib.MockModule(self.module_name)
-        exec code_object in module.__dict__
-        self.verify_module(module)
-        self.failUnlessRaises(ValueError, self.handler.code_from_bytecode,
-                                "234lkjfdase4")
-        
-    def test_code_from_source(self):
-        # Create a code object from source.
-        code = self.handler.code_from_source(self.source, self.py_path)
-        module = mock_importlib.MockModule(self.module_name)
-        exec code in module.__dict__
-        self.verify_module(module)
-        
-    def test_create_pyc(self):
-        # Test creating bytes for creating a .pyc file.
-        bytecode = 'bytecode'
-        timestamp = 42
-        data = self.handler.create_pyc(bytecode, timestamp)
-        self.failUnlessEqual(imp.get_magic(), data[:4])
-        # XXX Using importlib module's marshal long function.
-        self.failUnlessEqual(timestamp, _r_long(data[4:8]))
-        self.failUnlessEqual(marshal.dumps(bytecode), data[8:])
-
-
-class PyPycHandlerHandleCodeTests(unittest.TestCase):
-    
-    """Test PyPycHandler.handle_code().
-    
-    Various situations that must be handled (and thus tested):
-    * Valid .pyc
-        + .py exists [test_good_pyc_w_py]
-        + No .py .
-    * Invalid .pyc
-        + Reasons it is invalid
-            - Bad magic number.
-                * With .py [test_bad_magic_w_py]
-                * Without .py [test_bad_magic_no_py]
-            - .pyc stale based on timestamp.
-                * Requires .py [test_bad_timestamp_w_py]
-            - Bytecode is malformed.
-                * With .py [test_bad_bytecode_w_py]
-                * Without .py [test_bad_bytecode_no_py]
-    * No .pyc
-        + Valid .py
-            - Regenerate .pyc [test_py_w_pyc]
-            - Do not create .pyc [test_py_no_pyc]
-    
-    """
-    
-    def test_good_pyc_w_py(self):
-        # Test using a .pyc file that is valid and a .py file exists.
-        loader = mock_importlib.MockPyPycLoader.setup()
-        handler = loader._create_handler(importlib.PyPycHandler)
-        module = loader._handle_pyc(handler)
-        loader._verify_module(module)
-        # Source code should never be read, only bytecode.
-        self.failUnlessEqual(loader.log.count('read_data'), 1)
-        # Bytecode should not be written.
-        self.failUnless("write_data" not in loader.log)
-    
-    def test_bad_magic_no_py(self):
-        # ImportError should be raised when a .pyc has a bad magic number and
-        # there is no corresponding .py file.
-        loader = mock_importlib.MockPyPycLoader.setup(good_magic=False,
-                                                        py_exists=False)
-        handler = loader._create_handler(importlib.PyPycHandler)
-        self.failUnlessRaises(ImportError, loader._handle_pyc, handler)
-        
-    def test_bad_bytecode_no_py(self):
-        # A .pyc file with bad bytecode (but good magic number and timestamp)
-        # should raise an ImportError.
-        loader = mock_importlib.MockPyPycLoader.setup(py_exists=False,
-                                                     good_bytecode=False)
-        handler = loader._create_handler(importlib.PyPycHandler)
-        self.failUnlessRaises(ImportError, loader._handle_pyc, handler)
-        
-    def test_bad_magic_w_py(self):
-        # A .pyc file with a bad magic number should lead to the .py file being
-        # used to generate a new .pyc.
-        loader = mock_importlib.MockPyPycLoader.setup(good_magic=False)
-        handler = loader._create_handler(importlib.PyPycHandler)
-        module = loader._handle_pyc(handler)
-        loader._verify_module(module)
-        self.failUnless("write_data" in loader.log)
-        
-    def test_bad_timestamp_w_py(self):
-        # A .pyc file with a outdated timestamp should be regenerated.
-        loader = mock_importlib.MockPyPycLoader.setup(good_timestamp=False)
-        handler = loader._create_handler(importlib.PyPycHandler)
-        module = loader._handle_pyc(handler)
-        loader._verify_module(module)
-        self.failUnless("write_data" in loader.log)
-
-    def test_bad_bytecode_w_py(self):
-        # Even when there is a valid .py file, if the .pyc file has bad bytecode
-        # ImportError is raised.
-        loader = mock_importlib.MockPyPycLoader.setup(good_bytecode=False)
-        handler = loader._create_handler(importlib.PyPycHandler)
-        self.failUnlessRaises(ImportError, loader._handle_pyc, handler)
-        
-    def test_py_no_pyc(self):
-        # Test importing a .py file where no .pyc path is available.
-        loader = mock_importlib.MockPyPycLoader.setup(pyc_exists=False)
-        handler = loader._create_handler(importlib.PyPycHandler)
-        module = loader._handle_py(handler)
-        loader._verify_module(module)
-        self.failUnless('write_data' not in loader.log)
-        # No attempt should be made to read bytecode.
-        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_importlib.MockPyPycLoader.setup()
-        handler = loader._create_handler(importlib.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_importlib.MockPyPycLoader.setup(pyc_exists=False)
-        handler = loader._create_handler(importlib.PyPycHandler)
-        pkg_path = 'pkg path'
-        module = loader._handle_py(handler, pkg_path)
-        loader._verify_module(module)
-        self.failUnlessEqual(module.__path__, [pkg_path])
-        
-    def test_sys_modules_used(self):
-        # Handler should re-initialize an existing module in sys.modules
-        # (needed for 'reload').
-        loader = mock_importlib.MockPyPycLoader.setup()
-        original_module = mock_importlib.MockModule(loader.module_name)
-        original_value = -13
-        assert loader.attr_value != original_value
-        setattr(original_module, loader.attr_name, original_value)
-        sys.modules[loader.module_name] = original_module
-        handler = loader._create_handler(importlib.PyPycHandler)
-        new_module = loader._handle_pyc(handler)
-        self.failUnless(new_module is original_module)
-        self.failUnlessEqual(getattr(new_module, loader.attr_name),
-                                loader.attr_value)
-
-
-
-def test_main():
-    run_unittest(PyPycHandlerSupportingMethodTests,
-            PyPycHandlerHandleCodeTests)
-
-
-
-if __name__ == '__main__':
-    test_main()


More information about the Python-checkins mailing list