gh-149137: Fix PackagePath to normalize backslashes from Windows RECORD files#149540
Open
starkk242 wants to merge 2 commits intopython:mainfrom
Open
gh-149137: Fix PackagePath to normalize backslashes from Windows RECORD files#149540starkk242 wants to merge 2 commits intopython:mainfrom
starkk242 wants to merge 2 commits intopython:mainfrom
Conversation
…s RECORD files Per the packaging spec, backslashes are valid path separators on Windows in RECORD files. However, PackagePath extends PurePosixPath which treats backslashes as literal characters rather than separators, causing .name and .parts to return incorrect values (e.g., the full path string instead of just the filename). Fix by overriding __init__ in PackagePath to replace backslashes with forward slashes before passing to PurePosixPath.
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Author
|
Thanks for the note! The NEWS file was already included in the initial commit but had an incorrect filename (missing the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #149137.
importlib.metadata.PackagePath extends pathlib.PurePosixPath, which treats backslashes as literal characters, not path separators. However, per the packaging spec, backslashes are valid path separators on Windows in RECORD files.
This means packages installed on Windows that use backslashes in their RECORD file (e.g. ruff) produce broken PackagePath objects:
`python
Fix
Override init in PackagePath to normalize backslashes to forward slashes before passing to PurePosixPath:
python def __init__(self, *args): normalized = tuple( arg.replace('\\', '/') if isinstance(arg, str) else arg for arg in args ) super().__init__(*normalized)After the fix:
`python
The normalization is idempotent - paths already using forward slashes are unaffected.
Test plan