[Python-checkins] r57166 - in sandbox/trunk/import_in_py: _importlib.py tests/test_fs_importer.py

brett.cannon python-checkins at python.org
Sat Aug 18 01:13:09 CEST 2007


Author: brett.cannon
Date: Sat Aug 18 01:12:59 2007
New Revision: 57166
Modified:
 sandbox/trunk/import_in_py/_importlib.py
 sandbox/trunk/import_in_py/tests/test_fs_importer.py
Log:
Enforce that the source importer searches for source files before bytecode
files.
Modified: sandbox/trunk/import_in_py/_importlib.py
==============================================================================
--- sandbox/trunk/import_in_py/_importlib.py	(original)
+++ sandbox/trunk/import_in_py/_importlib.py	Sat Aug 18 01:12:59 2007
@@ -262,8 +262,16 @@
 raise
 
 
+def suffix_list(suffix_type):
+ """Return a list of suffixes for a specific type of file."""
+ return [suffix[0] for suffix in imp.get_suffixes()
+ if suffix[2] == suffix_type]
+
+
 class PyFileLoader(object):
 
+ # XXX Take in fullname from importer to make sure loader is not called for
+ # another module?
 def __init__(self, path):
 self._path = path
 
@@ -284,8 +292,8 @@
 
 Subclasses are expected to define the following attributes:
 
- * _file_types
- Sequence of imp file types (e.g., imp.C_EXTENSION).
+ * _suffixes
+ Sequence of file suffixes whose order will be followed.
 * _possible_package
 True if importer should check for packages.
 * _loader
@@ -302,8 +310,6 @@
 
 """
 self._path_entry = path_entry
- self._suffixes = [suffix[0] for suffix in imp.get_suffixes()
- if suffix[2] in self._file_types]
 
 def find_module(self, fullname, path=None):
 tail_module = fullname.rsplit('.', 1)[-1]
@@ -341,9 +347,9 @@
 _loader = ExtensionFileLoader
 
 def __init__(self, path_entry):
- # Assigning to _file_types here instead of at the class level because
+ # Assigning to _suffixes here instead of at the class level because
 # imp is not imported at the time of class creation.
- self._file_types = [imp.C_EXTENSION]
+ self._suffixes = suffix_list(imp.C_EXTENSION)
 super(self.__class__, self).__init__(path_entry)
 
 
@@ -355,7 +361,11 @@
 _loader = lambda path: FileSystemLoader(path, PyPycHandler)
 
 def __init__(self, path_entry):
- self._file_types = [imp.PY_SOURCE, imp.PY_COMPILED]
+ # Lack of imp during class creation means _suffixes is set here.
+ # Make sure that Python source files are listed first! Needed for an
+ # optimization by the loader.
+ self._suffixes = suffix_list(imp.PY_SOURCE)
+ self._suffixes += suffix_list(imp.PY_COMPILED)
 super(self.__class__, self).__init__(path_entry)
 
 
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	Sat Aug 18 01:12:59 2007
@@ -67,16 +67,20 @@
 self.assertEqual(found, expected)
 
 def test_file_type_order(self):
- # The order of the search should be preserved.
+ # The order of the search should be preserved with source files being
+ # found first.
 assert os.path.exists(self.py_path)
 assert self.py_ext in self.importer._suffixes
 assert os.path.exists(self.pyc_path)
 assert self.pyc_ext in self.importer._suffixes
- assert self.importer._suffixes[0] in [self.py_ext, self.pyc_ext]
- expected = self.importer._suffixes[0]
+ py_suffixes = importlib.suffix_list(imp.PY_SOURCE)
 found = self.importer.find_module(self.module_name)
- ext = os.path.splitext(found)[1]
- self.assertEqual(ext, self.importer._suffixes[0])
+ for suffix in py_suffixes:
+ if found.endswith(suffix):
+ break
+ else:
+ self.fail("Python source files not searched for before bytecode "
+ "files")
 
 def test_missing__init__warning(self):
 # An ImportWarning should be raised if a directory matches a module


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /