diff options
author | Barry Warsaw <barry@python.org> | 2013-04-16 15:05:03 (GMT) |
---|---|---|
committer | Barry Warsaw <barry@python.org> | 2013-04-16 15:05:03 (GMT) |
commit | 618738b921d9b3fa1f54304dd18236b3d465fbf8 (patch) | |
tree | c67ddc91545260b661eb170a58c0ba03668ec9af /Lib | |
parent | 51ce29c530f598d306de27724875cf37007422fc (diff) | |
download | cpython-618738b921d9b3fa1f54304dd18236b3d465fbf8.zip cpython-618738b921d9b3fa1f54304dd18236b3d465fbf8.tar.gz cpython-618738b921d9b3fa1f54304dd18236b3d465fbf8.tar.bz2 |
- Issue #17012: shutil.which() no longer fallbacks to the PATH environment
variable if empty path argument is specified. Patch by Serhiy Storchaka.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/shutil.py | 6 | ||||
-rw-r--r-- | Lib/test/test_shutil.py | 21 |
2 files changed, 26 insertions, 1 deletions
diff --git a/Lib/shutil.py b/Lib/shutil.py index a188408..3904f43 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -1091,7 +1091,11 @@ def which(cmd, mode=os.F_OK | os.X_OK, path=None): return cmd return None - path = (path or os.environ.get("PATH", os.defpath)).split(os.pathsep) + if path is None: + path = os.environ.get("PATH", os.defpath) + if not path: + return None + path = path.split(os.pathsep) if sys.platform == "win32": # The current directory takes precedence on Windows. diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index 3b1b694..a3e3a52 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -24,6 +24,7 @@ import warnings from test import support from test.support import TESTFN, check_warnings, captured_stdout, requires_zlib +from unittest.mock import patch try: import bz2 @@ -1352,6 +1353,26 @@ class TestWhich(unittest.TestCase): rv = shutil.which(self.file[:-4], path=self.dir) self.assertEqual(rv, self.temp_file.name[:-4] + ".EXE") + def test_environ_path(self): + with support.EnvironmentVarGuard() as env: + env['PATH'] = self.dir + rv = shutil.which(self.file) + self.assertEqual(rv, self.temp_file.name) + + def test_empty_path(self): + base_dir = os.path.dirname(self.dir) + with support.temp_cwd(path=self.dir), \ + support.EnvironmentVarGuard() as env: + env['PATH'] = self.dir + rv = shutil.which(self.file, path='') + self.assertIsNone(rv) + + def test_empty_path_no_PATH(self): + with support.EnvironmentVarGuard() as env: + env.pop('PATH', None) + rv = shutil.which(self.file) + self.assertIsNone(rv) + class TestMove(unittest.TestCase): |