8000 gh-127001: Fix PATHEXT issues in shutil.which() on Windows by serhiy-storchaka · Pull Request #127035 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-127001: Fix PATHEXT issues in shutil.which() on Windows #127035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Support PATHEXT extension that ends with a dot.
  • Loading branch information
serhiy-storchaka committed Nov 22, 2024
commit e49c75fd3b3a2f1ceaeb399dae1bb5946da947b7
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.
Loading
0