mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Issue #444582: shutil.which() respects relative paths.
This commit is contained in:
parent
849349de05
commit
07c24d13ed
3 changed files with 13 additions and 3 deletions
|
@ -249,8 +249,8 @@ Directory and files operations
|
||||||
|
|
||||||
.. function:: which(cmd, mode=os.F_OK | os.X_OK, path=None)
|
.. function:: which(cmd, mode=os.F_OK | os.X_OK, path=None)
|
||||||
|
|
||||||
Return the full path to an executable which would be run if the given
|
Return the path to an executable which would be run if the given *cmd*
|
||||||
*cmd* was called. If no *cmd* would be called, return ``None``.
|
was called. If no *cmd* would be called, return ``None``.
|
||||||
|
|
||||||
*mode* is a permission mask passed a to :func:`os.access`, by default
|
*mode* is a permission mask passed a to :func:`os.access`, by default
|
||||||
determining if the file exists and executable.
|
determining if the file exists and executable.
|
||||||
|
|
|
@ -1000,7 +1000,7 @@ def which(cmd, mode=os.F_OK | os.X_OK, path=None):
|
||||||
|
|
||||||
seen = set()
|
seen = set()
|
||||||
for dir in path:
|
for dir in path:
|
||||||
dir = os.path.normcase(os.path.abspath(dir))
|
dir = os.path.normcase(dir)
|
||||||
if not dir in seen:
|
if not dir in seen:
|
||||||
seen.add(dir)
|
seen.add(dir)
|
||||||
for thefile in files:
|
for thefile in files:
|
||||||
|
|
|
@ -1157,6 +1157,16 @@ class TestWhich(unittest.TestCase):
|
||||||
rv = shutil.which(self.file, path=self.dir, mode=os.W_OK)
|
rv = shutil.which(self.file, path=self.dir, mode=os.W_OK)
|
||||||
self.assertIsNone(rv)
|
self.assertIsNone(rv)
|
||||||
|
|
||||||
|
def test_relative(self):
|
||||||
|
old_cwd = os.getcwd()
|
||||||
|
base_dir, tail_dir = os.path.split(self.dir)
|
||||||
|
os.chdir(base_dir)
|
||||||
|
try:
|
||||||
|
rv = shutil.which(self.file, path=tail_dir)
|
||||||
|
self.assertEqual(rv, os.path.join(tail_dir, self.file))
|
||||||
|
finally:
|
||||||
|
os.chdir(old_cwd)
|
||||||
|
|
||||||
def test_nonexistent_file(self):
|
def test_nonexistent_file(self):
|
||||||
# Return None when no matching executable file is found on the path.
|
# Return None when no matching executable file is found on the path.
|
||||||
rv = shutil.which("foo.exe", path=self.dir)
|
rv = shutil.which("foo.exe", path=self.dir)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue