This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients christian.heimes, giampaolo.rodola, gregory.p.smith, izbyshev, serhiy.storchaka, vstinner
Date 2019-01-17.09:46:20
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1547718380.66.0.69709856053.issue35755@roundup.psfhosted.org>
In-reply-to
Content
Alexey Izbyshev:
> Would it make sense to use os.confstr('CS_PATH') instead of a hardcoded path, or is identical behavior on all POSIX platforms preferred to that?

I didn't know this variable. man confstr says:

_CS_PATH: A value for the PATH variable  which  indicates  where  all  the POSIX.2 standard utilities can be found.

On my Fedora 29, it only returns '/usr/bin':

$ python3
>>> import os; os.confstr("CS_PATH")
'/usr/bin'

On Fedora 29, /bin is a symlink to /usr/bin:

$ ls -ld /bin
lrwxrwxrwx. 1 root root 7 13 juil.  2018 /bin -> usr/bin/

So it makes sense to omit /bin from the default search path :-)

On Debian Sid where /bin is still a distinct directory than /usr/bin, CS_PATH is equal to "/bin:/usr/bin".

On Fedora, using confstr() would have the advantage of avoiding one useless syscall stat("/bin/program") in addition to stat("/usr/bin/program") in shutil.which() if the program doesn't exist... It's really a micro optimization which has no impact on the correctness, for the specific case of Fedora.

About the correctness, FreeBSD has a different value:

>>> os.confstr("CS_PATH")
'/usr/bin:/bin:/usr/sbin:/sbin'

Not only it also includes /usr/sbin and /sbin, but /usr/bin has the preference over /bin (posixpath of Python 3 checks /bin before /usr/bin). I'm not sure if the different order has an impact about correctness. I only found two programs which are in /bin and /usr/bin, but the programs in /usr/bin are symbolic links to a program with the same name in /bin :-)

vstinner@freebsd$ python3
Python 3.6.6 (default, Nov 20 2018, 01:57:10) 
>>> import os
>>> usr=os.listdir("/usr/bin")
>>> bin=os.listdir("/bin")
>>> set(usr) & set(bin)
{'pkill', 'pgrep'}

vstinner@freebsd$ file /usr/bin/pkill
/usr/bin/pkill: symbolic link to ../../bin/pkill
History
Date User Action Args
2019-01-17 09:46:22vstinnersetrecipients: + vstinner, gregory.p.smith, giampaolo.rodola, christian.heimes, serhiy.storchaka, izbyshev
2019-01-17 09:46:20vstinnersetmessageid: <1547718380.66.0.69709856053.issue35755@roundup.psfhosted.org>
2019-01-17 09:46:20vstinnerlinkissue35755 messages
2019-01-17 09:46:20vstinnercreate