python / python/cpython

Incorrect handling of backslashes in RECORD file by importlib.metadata

未关闭
#149,137 7 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib topic-importlib type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 importlib.metadata.PackagePath 开始,使用 RECORD 的内容和 issue 中的示例复现 Windows 情况。完成的标准是:对于使用反斜杠分隔的路径,PackagePath.name 仅返回文件名,同时所展示的使用正斜杠的情况继续生成预期的名称。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
tooling
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
描述清楚
新手友好度
30/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。