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 摘要。