8000 shutil.which will not return None anymore for empty str in PATHEXT · python/cpython@2981e84 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2981e84

Browse files
author
Christopher Marchfelder
committed
shutil.which will not return None anymore for empty str in PATHEXT
1 parent 7443d42 commit 2981e84

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Lib/shutil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,7 @@ def which(cmd, mode=os.F_OK | os.X_OK, path=None):
14151415
path.insert(0, curdir)
14161416

14171417
# PATHEXT is necessary to check on Windows.
1418-
pathext = os.environ.get("PATHEXT", "").split(os.pathsep)
1418+
pathext = [ext for ext in os.environ.get("PATHEXT", "").split(os.pathsep) if ext]
14191419
if use_bytes:
14201420
pathext = [os.fsencode(ext) for ext in pathext]
14211421
# See if the given file matches any of the expected path extensions.

Lib/test/test_shutil.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,23 @@ def test_pathext(self):
18481848
rv = shutil.which(program, path=self.temp_dir)
18491849
self.assertEqual(rv, temp_filexyz.name)
18501850

< 8000 /td>1851+
@unittest.skipUnless(sys.platform == "win32", 'test specific to Windows')
1852+
def test_pathext_with_empty_str(self):
1853+
ext = ".xyz"
1854+
temp_filexyz = tempfile.NamedTemporaryFile(dir=self.temp_dir,
1855+
prefix="Tmp2", suffix=ext)
1856+
os.chmod(temp_filexyz.name, stat.S_IXUSR)
1857+
self.addCleanup(temp_filexyz.close)
1858+
1859+
# strip path and extension
1860+
program = os.path.basename(temp_filexyz.name)
1861+
program = os.path.splitext(program)[0]
1862+
1863+
with support.EnvironmentVarGuard() as env:
1864+
env['PATHEXT'] = f"{ext};" # note the ;
1865+
rv = shutil.which(program, path=self.temp_dir)
1866+
self.assertEqual(rv, temp_filexyz.name)
1867+
18511868

18521869
class TestWhichBytes(TestWhich):
18531870
def setUp(self):

0 commit comments

Comments
 (0)
0