diff -r e6cd37d4dad8 Lib/shutil.py --- a/Lib/shutil.py Fri Jan 18 00:10:37 2013 -0500 +++ b/Lib/shutil.py Fri Jan 18 11:18:45 2013 +0000 @@ -1078,8 +1078,11 @@ # Short circuit. If we're given a full path which matches the mode # and it exists, we're done here. - if _access_check(cmd, mode): - return cmd + if os.path.dirname(cmd): + if _access_check(cmd, mode): + return cmd + # Subdirectories are not accessible from PATH directories + return None path = (path or os.environ.get("PATH", os.defpath)).split(os.pathsep) diff -r e6cd37d4dad8 Lib/test/test_shutil.py --- a/Lib/test/test_shutil.py Fri Jan 18 00:10:37 2013 -0500 +++ b/Lib/test/test_shutil.py Fri Jan 18 11:18:45 2013 +0000 @@ -1316,6 +1316,33 @@ self.assertEqual(rv, os.path.join(tail_dir, self.file)) finally: os.chdir(old_cwd) + + def test_cwd(self): + old_cwd = os.getcwd() + base_dir = os.path.dirname(self.dir) + os.chdir(self.dir) + try: + rv = shutil.which(self.file, path=base_dir) + if sys.platform == "win32": + # Windows: current directory implicitly on PATH + self.assertEqual(rv, os.path.join(os.curdir, self.file)) + else: + # Other platforms: shouldn't match in the current directory. + self.assertIsNone(rv) + finally: + os.chdir(old_cwd) + + def test_cwd_subdir(self): + # From the current directory, we can run foo/bar.exe + old_cwd = os.getcwd() + base_dir, tail_dir = os.path.split(self.dir) + os.chdir(base_dir) + try: + relpath = os.path.join(tail_dir, self.file) + rv = shutil.which(relpath, path=self.dir) + self.assertEqual(rv, relpath) + finally: + os.chdir(old_cwd) def test_nonexistent_file(self): # Return None when no matching executable file is found on the path.