]> granicus.if.org Git - python/commitdiff
Issue #16993: shutil.which() now preserves the case of the path and extension
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 21 Jan 2013 13:00:27 +0000 (15:00 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 21 Jan 2013 13:00:27 +0000 (15:00 +0200)
on Windows.

Doc/library/shutil.rst
Lib/shutil.py
Lib/test/test_shutil.py
Misc/NEWS

index e9621120a9fa6829c4caee4052c030a00a46e587..b5f39fe1e42a672268a16930afb450866970f536 100644 (file)
@@ -335,7 +335,7 @@ Directory and files operations
    directories.  For example, on Windows::
 
       >>> shutil.which("python")
-      'c:\\python33\\python.exe'
+      'C:\\Python33\\python.exe'
 
    .. versionadded:: 3.3
 
index 9c66008ed2fcf7d1d7846571b06830f5f0c8f675..86c32fa20ad79644243afdc59131327b6b9a1a20 100644 (file)
@@ -1093,10 +1093,12 @@ def which(cmd, mode=os.F_OK | os.X_OK, path=None):
         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.
@@ -1104,9 +1106,9 @@ def which(cmd, mode=os.F_OK | os.X_OK, path=None):
 
     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):
index 01b93fc441125cb66484af9c75d03eb052a16368..6ae051b0aecbb1b5124e871f26d5bbf35044b250 100644 (file)
@@ -1269,12 +1269,13 @@ class TestShutil(unittest.TestCase):
 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)
@@ -1317,7 +1318,7 @@ class TestWhich(unittest.TestCase):
         # 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):
index 7a720c195a8baa62347793f79422154ff178f653..df97d42d3ab65a8d397df8013e4143b8c985454c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -150,6 +150,9 @@ Core and Builtins
 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.