Issue 17012: Differences between /usr/bin/which and shutil.which() (original) (raw)

Issue17012

Created on 2013-01-22 12:14 by serhiy.storchaka, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
shutil_which_empty_path.patch serhiy.storchaka,2013-01-23 14:23 review
Messages (15)
msg180376 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-01-22 12:14
$ PATH= /usr/bin/which python $ PATH=: /usr/bin/which python ./python $ PATH=/usr: /usr/bin/which python ./python >>> shutil.which('python', path='') '/usr/bin/python' >>> shutil.which('python', path=':') 'python' >>> shutil.which('python', path='/usr:') 'python' First, I propose interpret path='' as an empty path, not as a default path (we have None for this). However the interpreting of an empty directory in non-empty PATH can be platform-depending.
msg180378 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-01-22 12:18
I'm not sure reproducing the quirks of /usr/bin/which is a good idea. shutil.which() is meant to be useful and easy to understand, not to be 100% bash-compatible. And, anyway, what would be the point of passing an empty path, if the return value is guaranteed to be None?
msg180380 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-01-22 12:36
/usr/bin/which is not a Bash. ;) The path can be unexpectedly empty. If we got None then we can detect the error, but if we got something out of the path then we can miss our fault.
msg180398 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-01-22 15:11
What I think it is suppose to do (the user expects it to do) is find the program that would be run if the command were typed at the command prompt. rdmurray@hey:~>which python /usr/bin/python rdmurray@hey:~>export PATH= rdmurray@hey:~>which python python not found rdmurray@hey:~>python zsh: command not found: python As Serhiy noted, this result may be platform dependent. Which is unfortunate.
msg180400 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-01-22 15:25
No, I noted that result of PATH=: or PATH=$PATH: can be platform dependent (I'm not sure).
msg180404 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-01-22 15:58
I was speaking in general of 'which program would be executed if the command is typed at the prompt' as being system dependent, which it demonstrably is since the behavior on unix and windows differs with regards to the current directory.
msg180405 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-01-22 15:59
And no, what I wrote wasn't clear :)
msg180413 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2013-01-22 17:17
FWIW, the POSIX standard gives some guidance on how PATH is to be interpreted for conforming systems, including: "A zero-length prefix is a legacy feature that indicates the current working directory. It appears as two adjacent characters ( "::" ), as an initial preceding the rest of the list, or as a trailing following the rest of the list. A strictly conforming application shall use an actual pathname (such as .) to represent the current working directory in PATH." http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html
msg180464 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-01-23 14:23
Thank you, Ned, for information. Here is a patch which remove the first difference (processing an empty path). The second difference is not semantically significant and I'm not sure whether we need to get rid of it.
msg182285 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-02-17 21:32
Ping.
msg182303 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2013-02-18 08:06
The result of PATH= is also platform dependent. Testing on OS X which has a BSD heritage rather a Linux one: $ PATH= /usr/bin/which python ./python # without patch $ PATH= ./python -c 'import shutil; print(shutil.which("python"))' python $ ./python -c 'import shutil; print(shutil.which("python", path=""))' /usr/bin/python # with the patch: $ PATH= ./python -c 'import shutil; print(shutil.which("python"))' None $ ./python -c 'import shutil; print(shutil.which("python", path=""))' None So, for OS X, shutil.which doesn't match /usr/bin/which behavior for the PATH= case either with or without the patch. FreeBSD (8.2) /usr/bin/which is the same. The other cases are the same as Linux. I suppose the patched behavior is preferable, though. In any case, the shutil.which docs also need to be updated.
msg187039 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2013-04-15 22:48
From a documentation standpoint, path='' is not the same as "When no path is specified", so indeed it should return None when path=''. Serhiy's patch looks good to me.
msg187045 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2013-04-16 01:39
Serhiy, I'd say go ahead and commit it. +1 from me.
msg187091 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-04-16 15:19
New changeset eb8c575fa781 by Barry Warsaw in branch '3.3': - Issue #17012: shutil.which() no longer fallbacks to the PATH environment http://hg.python.org/cpython/rev/eb8c575fa781 New changeset 8f5b37f8f964 by Barry Warsaw in branch 'default': - Issue #17012: shutil.which() no longer fallbacks to the PATH environment http://hg.python.org/cpython/rev/8f5b37f8f964
msg187093 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2013-04-16 15:21
I couldn't wait. :)
History
Date User Action Args
2022-04-11 14:57:40 admin set github: 61214
2013-04-16 15:22:58 barry set status: open -> closedresolution: fixed
2013-04-16 15:21:11 barry set assignee: serhiy.storchaka -> barrymessages: +
2013-04-16 15:19:30 python-dev set nosy: + python-devmessages: +
2013-04-16 01:39:45 barry set messages: +
2013-04-16 01:39:26 barry set assignee: barry -> serhiy.storchaka
2013-04-15 22:57:15 barry set assignee: barry
2013-04-15 22:48:58 barry set messages: +
2013-04-15 22:47:46 barry set nosy: + barry
2013-02-18 08:06:39 ned.deily set messages: +
2013-02-17 21:32:46 serhiy.storchaka set messages: +
2013-01-23 14:24:02 serhiy.storchaka set stage: patch review
2013-01-23 14:23:38 serhiy.storchaka set files: + shutil_which_empty_path.patchkeywords: + patchmessages: +
2013-01-22 17:17:52 ned.deily set nosy: + ned.deilymessages: +
2013-01-22 15:59:13 r.david.murray set messages: +
2013-01-22 15:58:47 r.david.murray set messages: +
2013-01-22 15:25:26 serhiy.storchaka set messages: +
2013-01-22 15:12:00 r.david.murray set nosy: + r.david.murraymessages: +
2013-01-22 12:36:30 serhiy.storchaka set messages: +
2013-01-22 12🔞10 pitrou set messages: +
2013-01-22 12:14:00 serhiy.storchaka create