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

brett.cannon python-checkins at python.org
Wed Nov 15 22:35:55 CET 2006


Author: brett.cannon
Date: Wed Nov 15 22:35:54 2006
New Revision: 52758
Modified:
 sandbox/trunk/import_in_py/importer.py
 sandbox/trunk/import_in_py/test_importer.py
Log:
Add regression tests for new-style relative imports.
Also added helper functions for replacing the built-in import function with
the new import code.
Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py	(original)
+++ sandbox/trunk/import_in_py/importer.py	Wed Nov 15 22:35:54 2006
@@ -95,11 +95,11 @@
 way and hyper-generalize by having a meta_path importer that
 returns entries in sys.path.
 * Loaders don't have to return the loaded module.
- + Since they have the responsibility of adding it to sys.modules, there
- no real need.
 + Importing the module being imported in a circular import dependency
 requires that module added to sys.modules stay consistent from the
 point it is added to initialization anyway.
+ + Can get the module the loader was supposed to handle directly out of
+ sys.modules.
 * Remove any idea of a default importer.
 + Removes support for None entries from sys.path_importer_cache.
 + Rely on default importers being in sys.path_hooks or sys.meta_path. 
@@ -152,6 +152,18 @@
 import py_compile
 
 
+def _set__import__():
+ """Set __import__ to an instance of Import."""
+ global original__import__
+ original__import__ = __import__
+ __builtins__['__import__'] = Import()
+
+def _reset__import__():
+ """Set __import__ back to the original implementation (assumes
+ _set__import__ was called previously)."""
+ __builtins__['__import__'] = original__import__
+
+
 class BuiltinFrozenBaseImporter(object):
 
 """Base class for meta_path importers for built-in and frozen modules.
@@ -237,8 +249,9 @@
 def __call__(self, path_entry):
 """If path_entry is a directory, return an importer object for it, else
 raise ImportError."""
- if os.path.isdir(path_entry):
- return FileSystemImporter(path_entry, *self.handlers)
+ absolute_path = os.path.abspath(path_entry)
+ if os.path.isdir(absolute_path):
+ return FileSystemImporter(absolute_path, *self.handlers)
 else:
 raise ImportError("can only handle directory entries from "
 "sys.path")
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	Wed Nov 15 22:35:54 2006
@@ -137,7 +137,7 @@
 
 """
 
- def create_files(self, faked_names=True):
+ def setUp(self, faked_names=True):
 """Generate the path to a temporary file to test with.
 
 If faked_names is true then all names are non-standard compared to
@@ -173,7 +173,7 @@
 self.bytecode = marshal.dumps(code)
 sys.path.insert(0, self.directory)
 
- def remove_files(self):
+ def tearDown(self):
 """If the temporary path was used, make sure to clean up."""
 if self.directory in sys.path:
 sys.path.remove(self.directory)
@@ -208,8 +208,8 @@
 
 """
 
- def create_files(self, faked_names=True):
- TestPyPycFiles.create_files(self, faked_names)
+ def setUp(self, faked_names=True):
+ TestPyPycFiles.setUp(self, faked_names)
 self.top_level_module_name = 'top_level_' + self.module_name
 self.top_level_module_path = os.path.join(self.directory,
 self.top_level_module_name+self.py_ext)
@@ -249,8 +249,8 @@
 with open(self.sub_pkg_module_path, 'w') as submodule_file:
 submodule_file.write(self.source)
 
- def remove_files(self):
- TestPyPycFiles.remove_files(self)
+ def tearDown(self):
+ TestPyPycFiles.tearDown(self)
 os.remove(self.top_level_module_path)
 shutil.rmtree(self.pkg_path)
 
@@ -287,22 +287,25 @@
 self.failUnlessEqual(module.__file__, self.pkg_module_path)
 
 
+class FileSystemFactoryTests(TestPyPycPackages):
+
+ """Test the filesystem path_hooks factory function."""
+
+ pass # XXX
+
+
 class FileSystemImporterTests(TestPyPycPackages):
 
 """Test the filesystem importer."""
 
 def setUp(self):
 """Create a basic importer."""
- TestPyPycPackages.create_files(self)
+ TestPyPycPackages.setUp(self)
 self.handler = mock_importer.MockHandler(self.py_ext)
 self.importer = importer.FileSystemImporter(self.directory,
 self.handler)
 self.importer.loader = mock_importer.MockPyPycLoader
 
- def tearDown(self):
- """Clean up the created file."""
- TestPyPycPackages.remove_files(self)
-
 def test_find_module_single_handler(self):
 # Having a single handler should work without issue.
 loader = self.importer.find_module(self.module_name)
@@ -476,12 +479,9 @@
 """Test the py/pyc handler."""
 
 def setUp(self):
- self.create_files()
+ TestPyPycFiles.setUp(self)
 self.handler = importer.PyPycHandler()
 
- def tearDown(self):
- self.remove_files()
- 
 def test_init(self):
 # Test __init__ usage.
 proper_bytecode_ext = '.pyc' if __debug__ else '.pyo'
@@ -960,7 +960,7 @@
 """
 
 def setUp(self):
- TestPyPycPackages.create_files(self, False)
+ TestPyPycPackages.setUp(self, False)
 self.import_ = importer.Import()
 self.cached_modules = []
 if self.module_name in sys.modules:
@@ -971,7 +971,7 @@
 del sys.modules[self.pkg_module_name]
 
 def tearDown(self):
- TestPyPycPackages.remove_files(self)
+ TestPyPycPackages.tearDown(self)
 for module_name, module in self.cached_modules:
 sys.modules[module_name] = module
 
@@ -1089,11 +1089,19 @@
 self.verify_package(module)
 self.failUnlessEqual(module.__name__, self.top_level_module_name)
 
- def XXX_test_relative_import_in_package_init(self):
- pass
+ def test_relative_import_in_package_init(self):
+ # Importing a module with a relative name in a package's __init__ file
+ # should work.
+ package_globals = {'__name__':self.pkg_name, '__path__':self.pkg_path}
+ module = self.import_(self.module_name, package_globals, level=1)
+ self.verify_package(module, self.pkg_module_name)
 
- def XXX_test_relative_import_in_package(self):
- pass
+ def test_relative_import_in_package(self):
+ # Importing a module with a relative name in another module should
+ # work.
+ module_globals = {'__name__':self.pkg_name + '.another_module'}
+ module = self.import_(self.module_name, module_globals, level=1)
+ self.verify_package(module, self.pkg_module_name)
 
 
 def test_main():


More information about the Python-checkins mailing list

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