Skip to content

Commit 2964898

Browse files
[3.12] gh-127001: Fix PATHEXT issues in shutil.which() on Windows (GH-127035) (GH-127158)
* Name without a PATHEXT extension is only searched if the mode does not include X_OK. * Support multi-component PATHEXT extensions (e.g. ".foo.bar"). * Support files without extensions in PATHEXT contains dot-only extension (".", "..", etc). * Support PATHEXT extensions that end with a dot (e.g. ".foo."). (cherry picked from commit 8899e85)
1 parent 32bc8e8 commit 2964898

File tree

4 files changed

+301
-246
lines changed

4 files changed

+301
-246
lines changed

Doc/library/shutil.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,6 @@ Directory and files operations
481481
or ends with an extension that is in ``PATHEXT``; and filenames that
482482
have no extension can now be found.
483483

484-
.. versionchanged:: 3.12.1
485-
On Windows, if *mode* includes ``os.X_OK``, executables with an
486-
extension in ``PATHEXT`` will be preferred over executables without a
487-
matching extension.
488-
This brings behavior closer to that of Python 3.11.
489-
490484
.. exception:: Error
491485

492486
This exception collects exceptions that are raised during a multi-file

Lib/shutil.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,21 +1534,21 @@ def which(cmd, mode=os.F_OK | os.X_OK, path=None):
15341534
if sys.platform == "win32":
15351535
# PATHEXT is necessary to check on Windows.
15361536
pathext_source = os.getenv("PATHEXT") or _WIN_DEFAULT_PATHEXT
1537-
pathext = [ext for ext in pathext_source.split(os.pathsep) if ext]
1537+
pathext = pathext_source.split(os.pathsep)
1538+
pathext = [ext.rstrip('.') for ext in pathext if ext]
15381539

15391540
if use_bytes:
15401541
pathext = [os.fsencode(ext) for ext in pathext]
15411542

1542-
files = ([cmd] + [cmd + ext for ext in pathext])
1543+
files = [cmd + ext for ext in pathext]
15431544

1544-
# gh-109590. If we are looking for an executable, we need to look
1545-
# for a PATHEXT match. The first cmd is the direct match
1546-
# (e.g. python.exe instead of python)
1547-
# Check that direct match first if and only if the extension is in PATHEXT
1548-
# Otherwise check it last
1549-
suffix = os.path.splitext(files[0])[1].upper()
1550-
if mode & os.X_OK and not any(suffix == ext.upper() for ext in pathext):
1551-
files.append(files.pop(0))
1545+
# If X_OK in mode, simulate the cmd.exe behavior: look at direct
1546+
# match if and only if the extension is in PATHEXT.
1547+
# If X_OK not in mode, simulate the first result of where.exe:
1548+
# always look at direct match before a PATHEXT match.
1549+
normcmd = cmd.upper()
1550+
if not (mode & os.X_OK) or any(normcmd.endswith(ext.upper()) for ext in pathext):
1551+
files.insert(0, cmd)
15521552
else:
15531553
# On other platforms you don't have things like PATHEXT to tell you
15541554
# what file suffixes are executable, so just pass on cmd as-is.
@@ -1557,7 +1557,7 @@ def which(cmd, mode=os.F_OK | os.X_OK, path=None):
15571557
seen = set()
15581558
for dir in path:
15591559
normdir = os.path.normcase(dir)
1560-
if not normdir in seen:
1560+
if normdir not in seen:
15611561
seen.add(normdir)
15621562
for thefile in files:
15631563
name = os.path.join(dir, thefile)

0 commit comments

Comments
 (0)