Skip to content

Commit 8899e85

Browse files
gh-127001: Fix PATHEXT issues in shutil.which() on Windows (GH-127035)
* 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.").
1 parent 615abb9 commit 8899e85

File tree

4 files changed

+303
-248
lines changed

4 files changed

+303
-248
lines changed

Doc/library/shutil.rst

-6
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,6 @@ Directory and files operations
491491
or ends with an extension that is in ``PATHEXT``; and filenames that
492492
have no extension can now be found.
493493

494-
.. versionchanged:: 3.12.1
495-
On Windows, if *mode* includes ``os.X_OK``, executables with an
496-
extension in ``PATHEXT`` will be preferred over executables without a
497-
matching extension.
498-
This brings behavior closer to that of Python 3.11.
499-
500494
.. exception:: Error
501495

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

Lib/shutil.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -1550,21 +1550,21 @@ def which(cmd, mode=os.F_OK | os.X_OK, path=None):
15501550
if sys.platform == "win32":
15511551
# PATHEXT is necessary to check on Windows.
15521552
pathext_source = os.getenv("PATHEXT") or _WIN_DEFAULT_PATHEXT
1553-
pathext = [ext for ext in pathext_source.split(os.pathsep) if ext]
1553+
pathext = pathext_source.split(os.pathsep)
1554+
pathext = [ext.rstrip('.') for ext in pathext if ext]
15541555

15551556
if use_bytes:
15561557
pathext = [os.fsencode(ext) for ext in pathext]
15571558

1558-
files = ([cmd] + [cmd + ext for ext in pathext])
1559+
files = [cmd + ext for ext in pathext]
15591560

1560-
# gh-109590. If we are looking for an executable, we need to look
1561-
# for a PATHEXT match. The first cmd is the direct match
1562-
# (e.g. python.exe instead of python)
1563-
# Check that direct match first if and only if the extension is in PATHEXT
1564-
# Otherwise check it last
1565-
suffix = os.path.splitext(files[0])[1].upper()
1566-
if mode & os.X_OK and not any(suffix == ext.upper() for ext in pathext):
1567-
files.append(files.pop(0))
1561+
# If X_OK in mode, simulate the cmd.exe behavior: look at direct
1562+
# match if and only if the extension is in PATHEXT.
1563+
# If X_OK not in mode, simulate the first result of where.exe:
1564+
# always look at direct match before a PATHEXT match.
1565+
normcmd = cmd.upper()
1566+
if not (mode & os.X_OK) or any(normcmd.endswith(ext.upper()) for ext in pathext):
1567+
files.insert(0, cmd)
15681568
else:
15691569
# On other platforms you don't have things like PATHEXT to tell you
15701570
# what file suffixes are executable, so just pass on cmd as-is.
@@ -1573,7 +1573,7 @@ def which(cmd, mode=os.F_OK | os.X_OK, path=None):
15731573
seen = set()
15741574
for dir in path:
15751575
normdir = os.path.normcase(dir)
1576-
if not normdir in seen:
1576+
if normdir not in seen:
15771577
seen.add(normdir)
15781578
for thefile in files:
15791579
name = os.path.join(dir, thefile)

0 commit comments

Comments
 (0)