cpython: 7b51568cfbae (original) (raw)
Mercurial > cpython
changeset 81669:7b51568cfbae
Issue #16957: shutil.which() no longer searches a bare file name in the current directory on Unix and no longer searches a relative file path with a directory part in PATH directories. Patch by Thomas Kluyver. [#16957]
Serhiy Storchaka storchaka@gmail.com | |
---|---|
date | Wed, 23 Jan 2013 10:45:33 +0200 |
parents | 9a0cd5363c2a(current diff)f18d11ab53a0(diff) |
children | 02e7da4c4fe3 |
files | Lib/shutil.py Lib/test/test_shutil.py Misc/NEWS |
diffstat | 3 files changed, 40 insertions(+), 12 deletions(-)[+] [-] Lib/shutil.py 11 Lib/test/test_shutil.py 37 Misc/NEWS 4 |
line wrap: on
line diff
--- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -1076,10 +1076,13 @@ def which(cmd, mode=os.F_OK | os.X_OK, p return (os.path.exists(fn) and os.access(fn, mode) and not os.path.isdir(fn))
Short circuit. If we're given a full path which matches the mode
and it exists, we're done here.
- if _access_check(cmd, mode):
return cmd[](#l1.10)
If we're given a path with a directory part, look it up directly rather
than referring to PATH directories. This includes checking relative to the
current directory, e.g. ./script
- if os.path.dirname(cmd):
if _access_check(cmd, mode):[](#l1.15)
return cmd[](#l1.16)
return None[](#l1.17)
path = (path or os.environ.get("PATH", os.defpath)).split(os.pathsep)
--- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -1296,11 +1296,36 @@ class TestWhich(unittest.TestCase): rv = shutil.which(self.file, path=self.dir) self.assertEqual(rv, self.temp_file.name)
- def test_absolute_cmd(self): # When given the fully qualified path to an executable that exists, # it should be returned. rv = shutil.which(self.temp_file.name, path=self.temp_dir)
self.assertEqual(self.temp_file.name, rv)[](#l2.12)
self.assertEqual(rv, self.temp_file.name)[](#l2.13)
- def test_relative_cmd(self):
# When given the relative path with a directory part to an executable[](#l2.16)
# that exists, it should be returned.[](#l2.17)
base_dir, tail_dir = os.path.split(self.dir)[](#l2.18)
relpath = os.path.join(tail_dir, self.file)[](#l2.19)
with support.temp_cwd(path=base_dir):[](#l2.20)
rv = shutil.which(relpath, path=self.temp_dir)[](#l2.21)
self.assertEqual(rv, relpath)[](#l2.22)
# But it shouldn't be searched in PATH directories (issue #16957).[](#l2.23)
with support.temp_cwd(path=self.dir):[](#l2.24)
rv = shutil.which(relpath, path=base_dir)[](#l2.25)
self.assertIsNone(rv)[](#l2.26)
- def test_cwd(self):
# Issue #16957[](#l2.29)
base_dir = os.path.dirname(self.dir)[](#l2.30)
with support.temp_cwd(path=self.dir):[](#l2.31)
rv = shutil.which(self.file, path=base_dir)[](#l2.32)
if sys.platform == "win32":[](#l2.33)
# Windows: current directory implicitly on PATH[](#l2.34)
self.assertEqual(rv, os.path.join(os.curdir, self.file))[](#l2.35)
else:[](#l2.36)
# Other platforms: shouldn't match in the current directory.[](#l2.37)
self.assertIsNone(rv)[](#l2.38)
def test_non_matching_mode(self): # Set the file read-only and ask for writeable files. @@ -1308,15 +1333,11 @@ class TestWhich(unittest.TestCase): rv = shutil.which(self.file, path=self.dir, mode=os.W_OK) self.assertIsNone(rv)
os.chdir(base_dir)[](#l2.50)
try:[](#l2.51)
with support.temp_cwd(path=base_dir):[](#l2.52) rv = shutil.which(self.file, path=tail_dir)[](#l2.53) self.assertEqual(rv, os.path.join(tail_dir, self.file))[](#l2.54)
finally:[](#l2.55)
os.chdir(old_cwd)[](#l2.56)
def test_nonexistent_file(self): # Return None when no matching executable file is found on the path.
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -220,6 +220,10 @@ Core and Builtins Library ------- +- Issue #16957: shutil.which() no longer searches a bare file name in the