]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #444582: shutil.which() respects relative paths.
authorAntoine Pitrou <solipsis@pitrou.net>
Fri, 22 Jun 2012 21:33:05 +0000 (23:33 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Fri, 22 Jun 2012 21:33:05 +0000 (23:33 +0200)
Doc/library/shutil.rst
Lib/shutil.py
Lib/test/test_shutil.py

index b581ce8d89d1ce8a443a32129c0a65fad350217b..71561162a45d48caad6b1035e2c303957e1067d1 100644 (file)
@@ -249,8 +249,8 @@ Directory and files operations
 
 .. function:: which(cmd, mode=os.F_OK | os.X_OK, path=None)
 
-   Return the full path to an executable which would be run if the given
-   *cmd* was called. If no *cmd* would be called, return ``None``.
+   Return the path to an executable which would be run if the given *cmd*
+   was called. If no *cmd* would be called, return ``None``.
 
    *mode* is a permission mask passed a to :func:`os.access`, by default
    determining if the file exists and executable.
index cb8d23f32c186384fe332d1619536fc8f511e119..8da46d15a452a30f3a23d473f92d7b5797324b9b 100644 (file)
@@ -1000,7 +1000,7 @@ def which(cmd, mode=os.F_OK | os.X_OK, path=None):
 
     seen = set()
     for dir in path:
-        dir = os.path.normcase(os.path.abspath(dir))
+        dir = os.path.normcase(dir)
         if not dir in seen:
             seen.add(dir)
             for thefile in files:
index 96084ec55f736fddf33fb36f6e72f35a43dfe3ca..559f05b32b08fe82ff011730cd992b2ed10e7fb1 100644 (file)
@@ -1157,6 +1157,16 @@ class TestWhich(unittest.TestCase):
         rv = shutil.which(self.file, path=self.dir, mode=os.W_OK)
         self.assertIsNone(rv)
 
+    def test_relative(self):
+        old_cwd = os.getcwd()
+        base_dir, tail_dir = os.path.split(self.dir)
+        os.chdir(base_dir)
+        try:
+            rv = shutil.which(self.file, path=tail_dir)
+            self.assertEqual(rv, os.path.join(tail_dir, self.file))
+        finally:
+            os.chdir(old_cwd)
+
     def test_nonexistent_file(self):
         # Return None when no matching executable file is found on the path.
         rv = shutil.which("foo.exe", path=self.dir)