close
Skip to content

gh-149137: Fix PackagePath to normalize backslashes from Windows RECORD files#149540

Open
starkk242 wants to merge 2 commits intopython:mainfrom
starkk242:fix-packagepath-backslash-gh149137
Open

gh-149137: Fix PackagePath to normalize backslashes from Windows RECORD files#149540
starkk242 wants to merge 2 commits intopython:mainfrom
starkk242:fix-packagepath-backslash-gh149137

Conversation

@starkk242
Copy link
Copy Markdown

@starkk242 starkk242 commented May 8, 2026

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

PackagePath('ruff-0.14.14.dist-info\METADATA').name
'ruff-0.14.14.dist-info\METADATA' # wrong - should be 'METADATA'
`

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

PackagePath('ruff-0.14.14.dist-info\METADATA').name
'METADATA' # correct
`

The normalization is idempotent - paths already using forward slashes are unaffected.

Test plan

  • Added PackagePathTests in Lib/test/test_importlib/metadata/test_main.py covering:
    • Backslash path .name returns only the filename
    • Backslash path .parts splits correctly
    • Forward-slash path is unchanged

…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.
@starkk242 starkk242 requested review from FFY00, jaraco and warsaw as code owners May 8, 2026 06:41
@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 8, 2026

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 skip news label instead.

@python-cla-bot
Copy link
Copy Markdown

python-cla-bot Bot commented May 8, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@starkk242
Copy link
Copy Markdown
Author

Thanks for the note! The NEWS file was already included in the initial commit but had an incorrect filename (missing the HH-MM-SS timestamp component required by blurb). Fixed in the latest push - the file is now named 2026-05-08-11-59-00.gh-issue-149137.Bp3xQz.rst following the correct blurb format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect handling of backslashes in RECORD file by importlib.metadata

1 participant