mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
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:
parent
564e4d8dc9
commit
8bea200b98
3 changed files with 40 additions and 12 deletions
|
@ -1077,10 +1077,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)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue