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.
This commit is contained in:
Serhiy Storchaka 2013-01-23 10:45:33 +02:00
commit e9a63600b3
3 changed files with 40 additions and 12 deletions

View file

@ -1076,10 +1076,13 @@ def which(cmd, mode=os.F_OK | os.X_OK, path=None):
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
# 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):
return cmd
return None
path = (path or os.environ.get("PATH", os.defpath)).split(os.pathsep)