GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-127001: Fix PATHEXT issues in shutil.which() on Windows by serhiy-storchaka · Pull Request #127035 · python/cpython

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Nov 19, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
  • Name without a PATHEXT extension is only searched if the mode does not
    include X_OK.
  • Support multi-component PATHEXT extensions (e.g. ".foo.bar").
  • Support files without extensions in PATHEXT contains dot-only extension
    (".", "..", etc).
  • Support PATHEXT extensions that end with a dot (e.g. ".foo.").
  • Issue: shutil.which() can return non-executable path starting with 3.12 on Windows #127001

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka
pythongh-127001: Fix conflict with extensionless files in shutil.whic…
b2ec0f6
…h() on Windows

Name without a PATHEXT extension is only searched if the mode does not
include X_OK.
@serhiy-storchaka serhiy-storchaka added needs backport to 3.12 only security fixes needs backport to 3.13 only security fixes labels Nov 19, 2024
@bedevere-app bedevere-app Bot mentioned this pull request Nov 19, 2024
shutil.which() can return non-executable path starting with 3.12 on Windows #127001
Closed
@zooba

zooba commented Nov 19, 2024

Copy link
Copy Markdown
Member

Ah, nice. I approve of this logic. I have a vague feeling it was discussed at some point, but I think it makes sense to require an extension from PATHEXT if X is required.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Nov 19, 2024

Copy link
Copy Markdown
Member Author

I'll rewrite tests tomorrow.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka added 2 commits November 22, 2024 10:07
@serhiy-storchaka
Rewrite tests and support . in PATHEXT.
851c4c2
@serhiy-storchaka
Update comment.
2258182
@serhiy-storchaka
serhiy-storchaka marked this pull request as ready for review November 22, 2024 08:34
@bedevere-app bedevere-app Bot added the awaiting core review label Nov 22, 2024
@serhiy-storchaka
Add a NEWS entry.
e1b862b
@serhiy-storchaka

serhiy-storchaka commented Nov 22, 2024

Copy link
Copy Markdown
Member Author

Some of existing tests did not make sense because they are grounded on wrong model. I replaced them. Also I refactored other tests to make them more uniform and reuse the same files and directories.

Fixed also cases when PATHEXT contains a multicomponent extension (e.g. .foo.bar) or dots (., .., etc).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka
serhiy-storchaka requested a review from zooba November 22, 2024 08:46
serhiy-storchaka added 3 commits November 22, 2024 11:21
@serhiy-storchaka
Test full match with multidot extension
b72facb
@serhiy-storchaka
Polishing.
e494730
@serhiy-storchaka
Remove an outdated bugfix versionchanged.
db4ab95
zooba
zooba reviewed Nov 22, 2024
View reviewed changes

@zooba zooba left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment I'd like to see added, but otherwise looks good

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/shutil.py Outdated
pathext_source = os.getenv("PATHEXT") or _WIN_DEFAULT_PATHEXT
pathext = [ext for ext in pathext_source.split(os.pathsep) if ext]
pathext = pathext_source.split(os.pathsep)
pathext = [ext.rstrip('.') and ext for ext in pathext if ext]

@zooba zooba Nov 22, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really dislike using and for value selection. I trust that you've got it right here, but I can't read it easily. Can we maybe comment?

# Replace ext of '.' with empty string, but keep trailing dots on '.ext.'

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@serhiy-storchaka serhiy-storchaka Nov 22, 2024

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think that it is better to remove dots in all cases. The behavior of cmd.exe if PATHEXT contains .BAT. and there is a.bat in the path:

  • a -- hangs
  • a.bat -- found
  • a. -- not found
  • a.bat. -- not found

which() will now return the same result, except for the first case. I think this is the best option for now.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

zooba reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@zooba zooba Nov 22, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka
Support PATHEXT extension that ends with a dot.
e49c75f
@serhiy-storchaka
serhiy-storchaka merged commit 8899e85 into python:main Nov 22, 2024
@miss-islington-app

miss-islington-app Bot commented Nov 22, 2024

Copy link
Copy Markdown

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting core review label Nov 22, 2024
@serhiy-storchaka
serhiy-storchaka deleted the shutil-which branch November 22, 2024 15:52
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 22, 2024
@serhiy-storchaka @miss-islington
pythongh-127001: Fix PATHEXT issues in shutil.which() on Windows (pyt…
068f56f
…honGH-127035)

* Name without a PATHEXT extension is only searched if the mode does not
  include X_OK.
* Support multi-component PATHEXT extensions (e.g. ".foo.bar").
* Support files without extensions in PATHEXT contains dot-only extension
  (".", "..", etc).
* Support PATHEXT extensions that end with a dot (e.g. ".foo.").
(cherry picked from commit 8899e85)

Co-authored-by: Serhiy Storchaka <[email protected]>
@miss-islington-app

miss-islington-app Bot commented Nov 22, 2024

Copy link
Copy Markdown

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 8899e85de100557899da05f0b37867a371a73800 3.12
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app miss-islington-app Bot assigned serhiy-storchaka Nov 22, 2024
@bedevere-app

bedevere-app Bot commented Nov 22, 2024

Copy link
Copy Markdown

GH-127156 is a backport of this pull request to the 3.13 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 only security fixes label Nov 22, 2024
@serhiy-storchaka serhiy-storchaka changed the title gh-127001: Fix conflict with extensionless files in shutil.which() on Windows gh-127001: Fix PATHEXT issues in shutil.which() on Windows Nov 22, 2024
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Nov 22, 2024
@serhiy-storchaka
[3.12] pythongh-127001: Fix PATHEXT issues in shutil.which() on Windo…
3dfcf12
…ws (pythonGH-127035)

* Name without a PATHEXT extension is only searched if the mode does not
  include X_OK.
* Support multi-component PATHEXT extensions (e.g. ".foo.bar").
* Support files without extensions in PATHEXT contains dot-only extension
  (".", "..", etc).
* Support PATHEXT extensions that end with a dot (e.g. ".foo.").
(cherry picked from commit 8899e85)

Co-authored-by: Serhiy Storchaka <[email protected]>
@bedevere-app

bedevere-app Bot commented Nov 22, 2024

Copy link
Copy Markdown

GH-127158 is a backport of this pull request to the 3.12 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.12 only security fixes label Nov 22, 2024
serhiy-storchaka added a commit that referenced this pull request Nov 22, 2024
@miss-islington @serhiy-storchaka
[3.13] gh-127001: Fix PATHEXT issues in shutil.which() on Windows (GH…
7f22b87
…-127035) (GH-127156)

* Name without a PATHEXT extension is only searched if the mode does not
  include X_OK.
* Support multi-component PATHEXT extensions (e.g. ".foo.bar").
* Support files without extensions in PATHEXT contains dot-only extension
  (".", "..", etc).
* Support PATHEXT extensions that end with a dot (e.g. ".foo.").
(cherry picked from commit 8899e85)

Co-authored-by: Serhiy Storchaka <[email protected]>
serhiy-storchaka added a commit that referenced this pull request Nov 22, 2024
@serhiy-storchaka
[3.12] gh-127001: Fix PATHEXT issues in shutil.which() on Windows (GH…
2964898
…-127035) (GH-127158)

* Name without a PATHEXT extension is only searched if the mode does not
  include X_OK.
* Support multi-component PATHEXT extensions (e.g. ".foo.bar").
* Support files without extensions in PATHEXT contains dot-only extension
  (".", "..", etc).
* Support PATHEXT extensions that end with a dot (e.g. ".foo.").
(cherry picked from commit 8899e85)
@martclanor martclanor mentioned this pull request Dec 27, 2024
fix(resolve_exe): allow shutil.which() to find exe without suffix in Windows modflowpy/flopy#2408
Merged
wpbonelli pushed a commit to modflowpy/flopy that referenced this pull request Jan 10, 2025
@martclanor
fix(resolve_exe): allow shutil.which() to find exe without suffix in …
4deabe1
…Windows (#2408)

This PR aims to address a behavior change in shutil.which (in Windows) introduced in python/cpython#127035.

For more context, in test_run_model_exe_rel_path, the mf6 executable is copied to a relative path, ../bin/mf6 (without extension). Within the test, shutil.which is used to locate the executable path. However, with the new changes in shutil.which, by default, it would only search for the path with an extension in PATHEXT, excluding paths without extension.
ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
@serhiy-storchaka @ebonnal
pythongh-127001: Fix PATHEXT issues in shutil.which() on Windows (pyt…
61713b2
…honGH-127035)

* Name without a PATHEXT extension is only searched if the mode does not
  include X_OK.
* Support multi-component PATHEXT extensions (e.g. ".foo.bar").
* Support files without extensions in PATHEXT contains dot-only extension
  (".", "..", etc).
* Support PATHEXT extensions that end with a dot (e.g. ".foo.").
@serhiy-storchaka serhiy-storchaka removed their assignment Jul 15, 2026
@coderabbitai coderabbitai Bot mentioned this pull request Aug 6, 2026
feat: measure onboarding postconditions baksohyeon/mogui-ADE-orchestrator#95
Merged
3 tasks done
@danielhanchen danielhanchen mentioned this pull request Aug 24, 2026
Prefer the .cmd sibling when Windows resolves an extensionless npm shim unslothai/unsloth#9238
Merged
@coderabbitai coderabbitai Bot mentioned this pull request Aug 24, 2026
[Dev Tooling] Consistent dev server port and Node version pin dimagi/open-chat-studio#4268
Merged
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@zooba zooba zooba left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@serhiy-storchaka @zooba
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.