diff -r cb297930d2cf Lib/shutil.py --- a/Lib/shutil.py Thu Jan 17 23:36:08 2013 +0100 +++ b/Lib/shutil.py Fri Jan 18 22:27:59 2013 +0000 @@ -1076,10 +1076,13 @@ return (os.path.exists(fn) and os.access(fn, mode) and not os.path.isdir(fn)) - # 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 we're given a path with a directory part, look it up directly rather + # than referring to PATH directories. This includes checking relative to the + # current directory, e.g. ./script + if os.path.dirname(cmd): + if _access_check(cmd, mode): + return cmd + return None path = (path or os.environ.get("PATH", os.defpath)).split(os.pathsep) diff -r cb297930d2cf Lib/test/test_shutil.py --- a/Lib/test/test_shutil.py Thu Jan 17 23:36:08 2013 +0100 +++ b/Lib/test/test_shutil.py Fri Jan 18 22:27:59 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.