[Python-checkins] cpython: Tweak the handling of the empty string in sys.path for importlib.

brett.cannon python-checkins at python.org
Fri Feb 17 00:12:06 CET 2012


http://hg.python.org/cpython/rev/b8593ec7e8c5
changeset:   74994:b8593ec7e8c5
user:        Brett Cannon <brett at python.org>
date:        Thu Feb 16 18:12:00 2012 -0500
summary:
  Tweak the handling of the empty string in sys.path for importlib.
It seems better to cache the finder for the cwd under its full path
insetad of '' in case the cwd changes. Otherwise FileFinder needs to
dynamically change itself based on whether it is given '' instead of
caching a finder for every change to the cwd.

files:
  Lib/importlib/_bootstrap.py             |  4 +++-
  Lib/importlib/test/import_/test_path.py |  2 +-
  2 files changed, 4 insertions(+), 2 deletions(-)


diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -713,10 +713,12 @@
         the default hook, for which ImportError is raised.
 
         """
+        if path == '':
+            path = _os.getcwd()
         try:
             finder = sys.path_importer_cache[path]
         except KeyError:
-            finder = cls._path_hooks(path if path != '' else _os.getcwd())
+            finder = cls._path_hooks(path)
             sys.path_importer_cache[path] = finder
         else:
             if finder is None and default:
diff --git a/Lib/importlib/test/import_/test_path.py b/Lib/importlib/test/import_/test_path.py
--- a/Lib/importlib/test/import_/test_path.py
+++ b/Lib/importlib/test/import_/test_path.py
@@ -82,7 +82,7 @@
         with util.import_state(path=[path], path_hooks=[hook]):
             loader = machinery.PathFinder.find_module(module)
             self.assertIs(loader, importer)
-            self.assertIn('', sys.path_importer_cache)
+            self.assertIn(os.getcwd(), sys.path_importer_cache)
 
 
 class DefaultPathFinderTests(unittest.TestCase):

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list