summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2013-04-16 15:18:18 (GMT)
committerBarry Warsaw <barry@python.org>2013-04-16 15:18:18 (GMT)
commitecaefcf44eca8a77946a4d2cc390b6d782647bfc (patch)
treeefcde9deb4f4d7eed790a8dc32d601eb8c6f64cf
parent7c5e094cbfa6769bf4cabfa5f883f2dc5320667b (diff)
parent618738b921d9b3fa1f54304dd18236b3d465fbf8 (diff)
downloadcpython-ecaefcf44eca8a77946a4d2cc390b6d782647bfc.zip
cpython-ecaefcf44eca8a77946a4d2cc390b6d782647bfc.tar.gz
cpython-ecaefcf44eca8a77946a4d2cc390b6d782647bfc.tar.bz2
- Issue #17012: shutil.which() no longer fallbacks to the PATH environment
variable if empty path argument is specified. Patch by Serhiy Storchaka.
-rw-r--r--Lib/shutil.py6
-rw-r--r--Lib/test/test_shutil.py21
-rw-r--r--Misc/NEWS3
3 files changed, 29 insertions, 1 deletions
diff --git a/Lib/shutil.py b/Lib/shutil.py
index c2f0278..6879d8b 100644
--- a/Lib/shutil.py
+++ b/Lib/shutil.py
@@ -1090,7 +1090,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 5a78440..2907ad8 100644
--- a/Lib/test/test_shutil.py
+++ b/Lib/test/test_shutil.py
@@ -25,6 +25,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
@@ -1363,6 +1364,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):
diff --git a/Misc/NEWS b/Misc/NEWS
index 6393f6c..31166ce 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -42,6 +42,9 @@ Core and Builtins
Library
-------
+- Issue #17012: shutil.which() no longer fallbacks to the PATH environment
+ variable if empty path argument is specified. Patch by Serhiy Storchaka.
+
- Issue #17710: Fix pickle raising a SystemError on bogus input.
- Issue #17341: Include the invalid name in the error messages from re about