Skip to content

Commit

Permalink
Make execute permission check more explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
takluyver committed Jun 10, 2014
1 parent 11af2e9 commit c5b744c
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions pexpect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1958,9 +1958,15 @@ def search(self, buffer, freshlen, searchwindowsize=None):
return best_index


def is_exe(fname):
def is_executable_file(path):
"""Checks that path is an executable regular file (or a symlink to a file).
This is roughly ``os.path isfile(path) and os.access(path, os.X_OK)``, but
on some platforms :func:`os.access` gives us the wrong answer, so this
checks permission bits directly.
"""
# follow symlinks,
fpath = os.path.realpath(fname)
fpath = os.path.realpath(path)

# return False for non-files (directories, fifo, etc.)
if not os.path.isfile(fpath):
Expand Down Expand Up @@ -1992,14 +1998,15 @@ def is_exe(fname):
mode & stat.S_IRUSR and mode & stat.S_IXUSR):
return True

return False

def which(filename):
'''This takes a given filename; tries to find it in the environment path;
then checks if it is executable. This returns the full path to the filename
if found and executable. Otherwise this returns None.'''

# Special case where filename contains an explicit path.
if os.path.dirname(filename) != '' and is_exe(filename):
if os.path.dirname(filename) != '' and is_executable_file(filename):
return filename
if 'PATH' not in os.environ or os.environ['PATH'] == '':
p = os.defpath
Expand All @@ -2008,7 +2015,7 @@ def which(filename):
pathlist = p.split(os.pathsep)
for path in pathlist:
ff = os.path.join(path, filename)
if is_exe(ff):
if is_executable_file(ff):
return ff
return None

Expand Down

0 comments on commit c5b744c

Please sign in to comment.