[Python-checkins] cpython (3.3): - Issue #17012: shutil.which() no longer fallbacks to the PATH environment

barry.warsaw python-checkins at python.org
Tue Apr 16 17:19:31 CEST 2013


http://hg.python.org/cpython/rev/eb8c575fa781
changeset:   83405:eb8c575fa781
branch:      3.3
parent:      83402:aec657f11b66
user:        Barry Warsaw <barry at python.org>
date:        Tue Apr 16 11:05:03 2013 -0400
summary:
  - Issue #17012: shutil.which() no longer fallbacks to the PATH environment
  variable if empty path argument is specified.  Patch by Serhiy Storchaka.

files:
  Lib/shutil.py           |   6 +++++-
  Lib/test/test_shutil.py |  21 +++++++++++++++++++++
  Misc/NEWS               |   3 +++
  3 files changed, 29 insertions(+), 1 deletions(-)


diff --git a/Lib/shutil.py b/Lib/shutil.py
--- a/Lib/shutil.py
+++ b/Lib/shutil.py
@@ -1091,7 +1091,11 @@
             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
--- a/Lib/test/test_shutil.py
+++ b/Lib/test/test_shutil.py
@@ -24,6 +24,7 @@
 
 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 @@
         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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,9 @@
 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

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list