Skip to content

Commit 8b4842b

Browse files
[3.8] bpo-40592: shutil.which will not return None anymore if ; is the last char in PATHEXT (GH-20088) (GH-22913)
shutil.which will not return None anymore for empty str in PATHEXT Empty PATHEXT will now be defaulted to _WIN_DEFAULT_PATHEXT (cherry picked from commit da6f098) Co-authored-by: Christopher Marchfelder <[email protected]>
1 parent cd894b1 commit 8b4842b

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Lib/shutil.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
_USE_CP_SENDFILE = hasattr(os, "sendfile") and sys.platform.startswith("linux")
5454
_HAS_FCOPYFILE = posix and hasattr(posix, "_fcopyfile") # macOS
5555

56+
# CMD defaults in Windows 10
57+
_WIN_DEFAULT_PATHEXT = ".COM;.EXE;.BAT;.CMD;.VBS;.JS;.WS;.MSC"
58+
5659
__all__ = ["copyfileobj", "copyfile", "copymode", "copystat", "copy", "copy2",
5760
"copytree", "move", "rmtree", "Error", "SpecialFileError",
5861
"ExecError", "make_archive", "get_archive_formats",
@@ -1400,7 +1403,9 @@ def which(cmd, mode=os.F_OK | os.X_OK, path=None):
14001403
path.insert(0, curdir)
14011404

14021405
# PATHEXT is necessary to check on Windows.
1403-
pathext = os.environ.get("PATHEXT", "").split(os.pathsep)
1406+
pathext_source = os.getenv("PATHEXT") or _WIN_DEFAULT_PATHEXT
1407+
pathext = [ext for ext in pathext_source.split(os.pathsep) if ext]
1408+
14041409
if use_bytes:
14051410
pathext = [os.fsencode(ext) for ext in pathext]
14061411
# See if the given file matches any of the expected path extensions.

Lib/test/test_shutil.py

+17
Original file line numberDiff line numberDiff line change
@@ -1830,6 +1830,23 @@ def test_pathext(self):
18301830
rv = shutil.which(program, path=self.temp_dir)
18311831
self.assertEqual(rv, temp_filexyz.name)
18321832

1833+
# Issue 40592: See https://bugs.python.org/issue40592
1834+
@unittest.skipUnless(sys.platform == "win32", 'test specific to Windows')
1835+
def test_pathext_with_empty_str(self):
1836+
ext = ".xyz"
1837+
temp_filexyz = tempfile.NamedTemporaryFile(dir=self.temp_dir,
1838+
prefix="Tmp2", suffix=ext)
1839+
self.addCleanup(temp_filexyz.close)
1840+
1841+
# strip path and extension
1842+
program = os.path.basename(temp_filexyz.name)
1843+
program = os.path.splitext(program)[0]
1844+
1845+
with support.EnvironmentVarGuard() as env:
1846+
env['PATHEXT'] = f"{ext};" # note the ;
1847+
rv = shutil.which(program, path=self.temp_dir)
1848+
self.assertEqual(rv, temp_filexyz.name)
1849+
18331850

18341851
class TestWhichBytes(TestWhich):
18351852
def setUp(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:func:`shutil.which` now ignores empty entries in :envvar:`PATHEXT` instead of treating them as a match.

0 commit comments

Comments
 (0)