]> granicus.if.org Git - python/commitdiff
- Issue #17012: shutil.which() no longer fallbacks to the PATH environment
authorBarry Warsaw <barry@python.org>
Tue, 16 Apr 2013 15:05:03 +0000 (11:05 -0400)
committerBarry Warsaw <barry@python.org>
Tue, 16 Apr 2013 15:05:03 +0000 (11:05 -0400)
  variable if empty path argument is specified.  Patch by Serhiy Storchaka.

Lib/shutil.py
Lib/test/test_shutil.py
Misc/NEWS

index a1884083087073512c1f8f63278798e755cb567e..3904f43400cf0de313f6c9c76c1ca0c623a0445a 100644 (file)
@@ -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.
index 3b1b6949d066fc5a83393f5b86f358fceabeb49e..a3e3a52264cee1543dac4601720b22a3991d0b85 100644 (file)
@@ -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):
 
index bf64dfdea0d57d0faa2a11cafd40b66b39b195ac..c085fbaa89158279eb6ca55714ea735114e246e7 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,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