From: Antoine Pitrou Date: Fri, 22 Jun 2012 21:33:05 +0000 (+0200) Subject: Issue #444582: shutil.which() respects relative paths. X-Git-Tag: v3.3.0b1~154^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=07c24d13ed3902ed6d85e70602e3f77cca098b16;p=thirdparty%2FPython%2Fcpython.git Issue #444582: shutil.which() respects relative paths. --- diff --git a/Doc/library/shutil.rst b/Doc/library/shutil.rst index b581ce8d89d1..71561162a45d 100644 --- a/Doc/library/shutil.rst +++ b/Doc/library/shutil.rst @@ -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. diff --git a/Lib/shutil.py b/Lib/shutil.py index cb8d23f32c18..8da46d15a452 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -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: diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index 96084ec55f73..559f05b32b08 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -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)