pexpect/pexpect — pull request #106 by jquast (2014-08-25 07:07:38+00:00)
Title: Issue 104 cannot exec setuids

Previously, misinterpreted that os.access(file, X_OK)
always returns True on Solaris. Yes, but only for
the uid of 0. Python issue #13706 closed "not a bug"
reads to "just use os.stat()", so we went to great
lengths to do so quite exhaustively.

But this is wrong -- *only* when root, should we check
the file modes -- os.access of X_OK works perfectly
fine for non-root users.

And, we should only check if any of the executable bits
are set. Alas, it is true, you may execute that which
you may not read -- because as root, you can always read
it anyway.

Verified similar solution in NetBSD test.c (/bin/test),
OpenBSD ksh for its built-in test, and what FreeBSD/Darwin
for their implementation of which.c.

Tested on OSX and Solaris, travis on Linux, of course.
Manually tested pexpect.which('sudo') on OSX and Solaris,
especially on Solaris as root and user nobody.