changeset: 77590:9d2fe615a400 user: Antoine Pitrou date: Fri Jun 22 23:33:05 2012 +0200 files: Doc/library/shutil.rst Lib/shutil.py Lib/test/test_shutil.py description: Issue #444582: shutil.which() respects relative paths. diff -r 1997633ff1fe -r 9d2fe615a400 Doc/library/shutil.rst --- a/Doc/library/shutil.rst Fri Jun 22 16:03:06 2012 -0500 +++ b/Doc/library/shutil.rst Fri Jun 22 23:33:05 2012 +0200 @@ -249,8 +249,8 @@ .. 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 -r 1997633ff1fe -r 9d2fe615a400 Lib/shutil.py --- a/Lib/shutil.py Fri Jun 22 16:03:06 2012 -0500 +++ b/Lib/shutil.py Fri Jun 22 23:33:05 2012 +0200 @@ -1000,7 +1000,7 @@ 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 -r 1997633ff1fe -r 9d2fe615a400 Lib/test/test_shutil.py --- a/Lib/test/test_shutil.py Fri Jun 22 16:03:06 2012 -0500 +++ b/Lib/test/test_shutil.py Fri Jun 22 23:33:05 2012 +0200 @@ -1157,6 +1157,16 @@ 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)

AltStyle によって変換されたページ (->オリジナル) /