python / python/cpython

Incorrect handling of backslashes in RECORD file by importlib.metadata

Open
#149,137 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-importlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

The class importlib.metadata.PackagePath does not correctly handle backslashes in the RECORD dist-info file.
According to the specs, backslashes are allowed:

The path may be either absolute, or relative to the directory containing the .dist-info directory (commonly, the site-packages directory). On Windows, directories may be separated either by forward- or backslashes (/ or \).

This leads to PackagePath.name wrongfully returning the entire path instead of just the filename.

Steps for Reprocution

As an example, take ruff 0.14.14 on Windows

RECORD file contents
..\..\Scripts\ruff.exe,sha256=HJzLwAeBnSdcuXo602XCKCTuDMMrn5dZPIEzmZ0w6QY,31781376
ruff-0.14.14.dist-info/INSTALLER,sha256=5hhM4Q4mYTT9z6QB6PGpUAW81PGNFrYrdXMj4oM_6ak,2
ruff-0.14.14.dist-info/REQUESTED,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
ruff-0.14.14.dist-info\METADATA,sha256=QD17c7tQSny5DBVcDewbxp32FVd0m05EfaH_AQvKuhQ,26651
ruff-0.14.14.dist-info\RECORD,,
ruff-0.14.14.dist-info\WHEEL,sha256=jsSEiVNsW1dJj5gDaReR40i7mhgBjWtms6nAD6EViXU,94
ruff-0.14.14.dist-info\licenses\LICENSE,sha256=JZfYVBIrd93HGXFWTKI1CjdghXXOMkrcVlCisgUcjxg,20731
ruff\__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
ruff\__main__.py,sha256=w1Ehfzzy8tMM7r7dFrKmi3DkmTZyV_xJ40K_sxoO6X0,2908
pip install "ruff==0.14.14"
import importlib.metadata
from pathlib import Path

for pkg in importlib.metadata.distributions():
    if pkg.name == "ruff":
        for file in pkg.files:
            print(file.name, Path(file).name)
Expected Output
ruff.exe ruff.exe
INSTALLER INSTALLER
REQUESTED REQUESTED
METADATA METADATA
RECORD RECORD
WHEEL WHEEL
LICENSE LICENSE
__init__.py __init__.py
__main__.py __main__.py
Actual Output
..\..\Scripts\ruff.exe ruff.exe
INSTALLER INSTALLER
REQUESTED REQUESTED
ruff-0.14.14.dist-info\METADATA METADATA
ruff-0.14.14.dist-info\RECORD RECORD
ruff-0.14.14.dist-info\WHEEL WHEEL
ruff-0.14.14.dist-info\licenses\LICENSE LICENSE
ruff\__init__.py __init__.py
ruff\__main__.py __main__.py
CPython versions tested on:

3.14

Operating systems tested on:

Windows

Linked PRs
  • gh-149540

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with importlib.metadata.PackagePath and reproduce the Windows case using the RECORD contents and example in the issue. Done means PackagePath.name returns only the filename for backslash-separated paths, while the demonstrated forward-slash cases continue to produce the expected names.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.