Skip to content

Commit

Permalink
Support PATHEXT extension that ends with a dot.
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka committed Nov 22, 2024
1 parent db4ab95 commit e49c75f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1551,7 +1551,7 @@ def which(cmd, mode=os.F_OK | os.X_OK, path=None):
# PATHEXT is necessary to check on Windows.
pathext_source = os.getenv("PATHEXT") or _WIN_DEFAULT_PATHEXT
pathext = pathext_source.split(os.pathsep)
pathext = [ext.rstrip('.') and ext for ext in pathext if ext]
pathext = [ext.rstrip('.') for ext in pathext if ext]

if use_bytes:
pathext = [os.fsencode(ext) for ext in pathext]
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -2546,6 +2546,21 @@ def test_pathext_with_null_extension(self):
self.assertEqual(shutil.which(cmddot, path=self.dir),
filepath + self.to_text_type('.'))

@unittest.skipUnless(sys.platform == "win32", 'test specific to Windows')
def test_pathext_extension_ends_with_dot(self):
ext = '.xyz'
cmd = self.to_text_type(TESTFN2)
cmdext = cmd + self.to_text_type(ext)
dot = self.to_text_type('.')
filepath = os.path.join(self.dir, cmdext)
self.create_file(filepath)
with os_helper.EnvironmentVarGuard() as env:
env['PATHEXT'] = ext + '.'
self.assertEqual(shutil.which(cmd, path=self.dir), filepath) # cmd.exe hangs here
self.assertEqual(shutil.which(cmdext, path=self.dir), filepath)
self.assertIsNone(shutil.which(cmd + dot, path=self.dir))
self.assertIsNone(shutil.which(cmdext + dot, path=self.dir))

# See GH-75586
@unittest.skipUnless(sys.platform == "win32", 'test specific to Windows')
def test_pathext_applied_on_files_in_path(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Fix :mod:`shutil.which` on Windows. Now it looks at direct match if and only
if the command ends with a PATHEXT extension or X_OK is not in mode. Support
extensionless files if "." is in PATHEXT.
extensionless files if "." is in PATHEXT. Support PATHEXT extensions that end
with a dot.

0 comments on commit e49c75f

Please sign in to comment.