mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
[3.12] gh-109590: Update shutil.which on Windows to prefer a PATHEXT extension on executable files (GH-109995) (#110202)
gh-109590: Update shutil.which on Windows to prefer a PATHEXT extension on executable files (GH-109995)
The default arguments for shutil.which() request an executable file, but extensionless files are not executable on Windows and should be ignored.
(cherry picked from commit 29b875bb93
)
Co-authored-by: Charles Machalow <csm10495@gmail.com>
This commit is contained in:
parent
10af2242f4
commit
b61a4da459
4 changed files with 91 additions and 12 deletions
|
@ -1554,8 +1554,16 @@ def which(cmd, mode=os.F_OK | os.X_OK, path=None):
|
|||
if use_bytes:
|
||||
pathext = [os.fsencode(ext) for ext in pathext]
|
||||
|
||||
# Always try checking the originally given cmd, if it doesn't match, try pathext
|
||||
files = [cmd] + [cmd + ext for ext in pathext]
|
||||
files = ([cmd] + [cmd + ext for ext in pathext])
|
||||
|
||||
# gh-109590. If we are looking for an executable, we need to look
|
||||
# for a PATHEXT match. The first cmd is the direct match
|
||||
# (e.g. python.exe instead of python)
|
||||
# Check that direct match first if and only if the extension is in PATHEXT
|
||||
# Otherwise check it last
|
||||
suffix = os.path.splitext(files[0])[1].upper()
|
||||
if mode & os.X_OK and not any(suffix == ext.upper() for ext in pathext):
|
||||
files.append(files.pop(0))
|
||||
else:
|
||||
# On other platforms you don't have things like PATHEXT to tell you
|
||||
# what file suffixes are executable, so just pass on cmd as-is.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue