[Python-checkins] cpython (merge 3.3 -> default): Issue #16993: shutil.which() now preserves the case of the path and extension

serhiy.storchaka python-checkins at python.org
Mon Jan 21 14:04:17 CET 2013


http://hg.python.org/cpython/rev/5faae2bdf1e0
changeset:   81632:5faae2bdf1e0
parent:      81629:f90d6ce49772
parent:      81631:d2db601a53b3
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Mon Jan 21 15:01:34 2013 +0200
summary:
  Issue #16993: shutil.which() now preserves the case of the path and extension
on Windows.

files:
  Doc/library/shutil.rst  |   2 +-
  Lib/shutil.py           |  12 +++++++-----
  Lib/test/test_shutil.py |   7 ++++---
  Misc/NEWS               |   3 +++
  4 files changed, 15 insertions(+), 9 deletions(-)


diff --git a/Doc/library/shutil.rst b/Doc/library/shutil.rst
--- a/Doc/library/shutil.rst
+++ b/Doc/library/shutil.rst
@@ -348,7 +348,7 @@
    directories.  For example, on Windows::
 
       >>> shutil.which("python")
-      'c:\\python33\\python.exe'
+      'C:\\Python33\\python.exe'
 
    .. versionadded:: 3.3
 
diff --git a/Lib/shutil.py b/Lib/shutil.py
--- a/Lib/shutil.py
+++ b/Lib/shutil.py
@@ -1092,10 +1092,12 @@
         pathext = os.environ.get("PATHEXT", "").split(os.pathsep)
         # See if the given file matches any of the expected path extensions.
         # This will allow us to short circuit when given "python.exe".
-        matches = [cmd for ext in pathext if cmd.lower().endswith(ext.lower())]
         # If it does match, only test that one, otherwise we have to try
         # others.
-        files = [cmd] if matches else [cmd + ext.lower() for ext in pathext]
+        if any(cmd.lower().endswith(ext.lower()) for ext in pathext):
+            files = [cmd]
+        else:
+            files = [cmd + ext for ext in pathext]
     else:
         # On other platforms you don't have things like PATHEXT to tell you
         # what file suffixes are executable, so just pass on cmd as-is.
@@ -1103,9 +1105,9 @@
 
     seen = set()
     for dir in path:
-        dir = os.path.normcase(dir)
-        if not dir in seen:
-            seen.add(dir)
+        normdir = os.path.normcase(dir)
+        if not normdir in seen:
+            seen.add(normdir)
             for thefile in files:
                 name = os.path.join(dir, thefile)
                 if _access_check(name, mode):
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py
--- a/Lib/test/test_shutil.py
+++ b/Lib/test/test_shutil.py
@@ -1280,12 +1280,13 @@
 class TestWhich(unittest.TestCase):
 
     def setUp(self):
-        self.temp_dir = tempfile.mkdtemp()
+        self.temp_dir = tempfile.mkdtemp(prefix="Tmp")
         self.addCleanup(shutil.rmtree, self.temp_dir, True)
         # Give the temp_file an ".exe" suffix for all.
         # It's needed on Windows and not harmful on other platforms.
         self.temp_file = tempfile.NamedTemporaryFile(dir=self.temp_dir,
-                                                     suffix=".exe")
+                                                     prefix="Tmp",
+                                                     suffix=".Exe")
         os.chmod(self.temp_file.name, stat.S_IXUSR)
         self.addCleanup(self.temp_file.close)
         self.dir, self.file = os.path.split(self.temp_file.name)
@@ -1328,7 +1329,7 @@
         # Ask for the file without the ".exe" extension, then ensure that
         # it gets found properly with the extension.
         rv = shutil.which(self.temp_file.name[:-4], path=self.dir)
-        self.assertEqual(self.temp_file.name, rv)
+        self.assertEqual(rv, self.temp_file.name[:-4] + ".exe")
 
 
 class TestMove(unittest.TestCase):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -220,6 +220,9 @@
 Library
 -------
 
+- Issue #16993: shutil.which() now preserves the case of the path and extension
+  on Windows.
+
 - Issue #16992: On Windows in signal.set_wakeup_fd, validate the file
   descriptor argument.
 

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


More information about the Python-checkins mailing list