[Python-checkins] cpython: Issue #14581: Windows users are allowed to import modules w/o taking

brett.cannon python-checkins at python.org
Fri Apr 20 18:53:21 CEST 2012


http://hg.python.org/cpython/rev/a32be109bd86
changeset:   76428:a32be109bd86
user:        Brett Cannon <brett at python.org>
date:        Fri Apr 20 12:53:14 2012 -0400
summary:
  Issue #14581: Windows users are allowed to import modules w/o taking
the file suffix's case into account, even when doing a case-sensitive
import.

files:
  Lib/importlib/_bootstrap.py |   18 +++++++++++++++++-
  Python/importlib.h          |  Bin 
  2 files changed, 17 insertions(+), 1 deletions(-)


diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -841,7 +841,23 @@
         contents = _os.listdir(path)
         # We store two cached versions, to handle runtime changes of the
         # PYTHONCASEOK environment variable.
-        self._path_cache = set(contents)
+        if not sys.platform.startswith('win'):
+            self._path_cache = set(contents)
+        else:
+            # Windows users can import modules with case-insensitive file
+            # suffixes (for legacy reasons). Make the suffix lowercase here
+            # so it's done once instead of for every import. This is safe as
+            # the specified suffixes to check against are always specified in a
+            # case-sensitive manner.
+            lower_suffix_contents = set()
+            for item in contents:
+                name, dot, suffix = item.partition('.')
+                if dot:
+                    new_name = '{}.{}'.format(name, suffix.lower())
+                else:
+                    new_name = name
+                lower_suffix_contents.add(new_name)
+            self._path_cache = lower_suffix_contents
         if sys.platform.startswith(CASE_INSENSITIVE_PLATFORMS):
             self._relaxed_path_cache = set(fn.lower() for fn in contents)
 
diff --git a/Python/importlib.h b/Python/importlib.h
index 3ccea93e3eec8e84972c009a5e59939e9a0d6aac..88b89edb779b33dda3c6b8f3af878d66bde2fd99
GIT binary patch
[stripped]

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


More information about the Python-checkins mailing list