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.

classification
Title: `Shutil.which` incosistent with windows's `where`
Type: Stage: resolved
Components: Library (Lib), Windows Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: alkuzad, miss-islington, paul.moore, peanutlord, steve.dower, tim.golden, zach.ware
Priority: normal Keywords: easy, newcomer friendly, patch

Created on 2020-05-11 08:50 by alkuzad, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 20088 merged peanutlord, 2020-05-14 15:56
PR 22912 merged miss-islington, 2020-10-23 10:08
PR 22913 merged miss-islington, 2020-10-23 10:08
Messages (9)
msg368620 - (view) Author: Dawid Gosławski (alkuzad) Date: 2020-05-11 08:50
Shutil's which implementation does not work correctly when someone set's empty item in `PATHEXT` environment variable. Example:

set PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.RB;.RBW;

I'm not 100% sure how I got this in my PATHEXT config, I wasn't changing that so maybe some bugged uninstaller not removed it's extension correctly.

This makes things confusing as Windows will find correctly binary, but Python will return nothing, due to this part:

```
if any(cmd.lower().endswith(ext.lower()) for ext in pathext):
            files = [cmd]
```

pathext is initialized as `pathext = os.environ.get("PATHEXT", "").split(os.pathsep)`, which ends producing '' as last element

Because any string ends with empty string (''), files list will have plain version added like `git`, which will then fail executable check.

Workaround is to use full name `git.exe`

Filtering out empty strings would fix that.
msg368745 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2020-05-12 22:29
Sounds like a good opportunity for someone to make their first contribution.

Post a message if you'd like to work on this (and a test), and we can help get through the PR process.
msg368838 - (view) Author: Christopher Marchfelder (peanutlord) * Date: 2020-05-14 13:42
@steve.dower I would really love to work on this and make my first contribution. Never did one, so I would some help doing this one :)
msg379218 - (view) Author: Christopher Marchfelder (peanutlord) * Date: 2020-10-21 16:30
@steve.dower Added the changes in the PR - could you please re-check? Thank you!
msg379347 - (view) Author: Christopher Marchfelder (peanutlord) * Date: 2020-10-22 21:07
@steve.dower Your advice did it, is fixed and green! :) Thank you again
msg379426 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2020-10-23 10:08
New changeset da6f098188c9825f10ae60db8987056b3a54c2e8 by Christopher Marchfelder in branch 'master':
bpo-40592: shutil.which will not return None anymore if ; is the last char in PATHEXT (GH-20088)
https://github.com/python/cpython/commit/da6f098188c9825f10ae60db8987056b3a54c2e8
msg379427 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2020-10-23 10:09
Thanks for the contribution! Looking forward to your next one :)
msg379477 - (view) Author: miss-islington (miss-islington) Date: 2020-10-23 21:38
New changeset 8b4842b7a830299870067bba59f53529b13647a2 by Miss Skeleton (bot) in branch '3.8':
[3.8] bpo-40592: shutil.which will not return None anymore if ; is the last char in PATHEXT (GH-20088) (GH-22913)
https://github.com/python/cpython/commit/8b4842b7a830299870067bba59f53529b13647a2
msg379478 - (view) Author: miss-islington (miss-islington) Date: 2020-10-23 21:38
New changeset c437fe39cf5318f27f3381a983fbf320f65064bc by Miss Skeleton (bot) in branch '3.9':
[3.9] bpo-40592: shutil.which will not return None anymore if ; is the last char in PATHEXT (GH-20088) (GH-22912)
https://github.com/python/cpython/commit/c437fe39cf5318f27f3381a983fbf320f65064bc
History
Date User Action Args
2022-04-11 14:59:30adminsetgithub: 84772
2020-10-23 21:38:08miss-islingtonsetmessages: + msg379478
2020-10-23 21:38:07miss-islingtonsetnosy: + miss-islington
messages: + msg379477
2020-10-23 10:09:45steve.dowersetstatus: open -> closed

versions: + Python 3.8, Python 3.9, Python 3.10
nosy: - miss-islington

messages: + msg379427
resolution: fixed
stage: patch review -> resolved
2020-10-23 10:08:49miss-islingtonsetpull_requests: + pull_request21844
2020-10-23 10:08:40miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request21843
2020-10-23 10:08:35steve.dowersetmessages: + msg379426
2020-10-22 21:07:25peanutlordsetmessages: + msg379347
2020-10-21 16:30:22peanutlordsetmessages: + msg379218
2020-05-14 15:56:44peanutlordsetkeywords: + patch
stage: patch review
pull_requests: + pull_request19392
2020-05-14 13:42:18peanutlordsetnosy: + peanutlord
messages: + msg368838
2020-05-12 22:29:22steve.dowersetkeywords: + easy, newcomer friendly

messages: + msg368745
2020-05-11 10:05:51serhiy.storchakasetnosy: + paul.moore, tim.golden, zach.ware, steve.dower
components: + Windows
2020-05-11 08:50:15alkuzadcreate