From 0464764e592872a14360b758c7bd43a8a88423e4 Mon Sep 17 00:00:00 2001 From: hansu650 <2788086371@qq.com> Date: Tue, 19 May 2026 17:48:43 +0800 Subject: [PATCH] Normalize RECORD paths when writing --- src/installer/records.py | 5 ++--- tests/test_records.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/installer/records.py b/src/installer/records.py index 9bb45c75..a2ea5604 100644 --- a/src/installer/records.py +++ b/src/installer/records.py @@ -6,7 +6,7 @@ import os from collections.abc import Iterable, Iterator from dataclasses import dataclass -from pathlib import Path +from pathlib import Path, PureWindowsPath from typing import BinaryIO, cast from installer.utils import copyfileobj_with_hashing, get_stream_length @@ -106,8 +106,7 @@ def to_row(self, path_prefix: str | None = None) -> tuple[str, str, str]: path = self.path # Convert Windows paths to use / for consistency - if os.sep == "\\": - path = path.replace("\\", "/") # pragma: no cover + path = PureWindowsPath(path).as_posix() return ( path, diff --git a/tests/test_records.py b/tests/test_records.py index a6816081..2de2a658 100644 --- a/tests/test_records.py +++ b/tests/test_records.py @@ -3,6 +3,7 @@ import pytest +import installer.records from installer.records import Hash, InvalidRecordEntry, RecordEntry, parse_record_file @@ -176,6 +177,34 @@ def test_string_representation_with_prefix( ) assert record.to_row("prefix/") == expected_row + def test_string_representation_with_backslash_path(self, monkeypatch): + monkeypatch.setattr(installer.records.os, "sep", "/") + record = RecordEntry.from_elements( + r"distribution-1.0.dist-info\RECORD", + "", + "", + ) + + assert record.to_row() == ( + "distribution-1.0.dist-info/RECORD", + "", + "", + ) + + def test_string_representation_with_backslash_path_and_prefix(self, monkeypatch): + monkeypatch.setattr(installer.records.os, "sep", "/") + record = RecordEntry.from_elements( + r"bin\script.py", + "", + "", + ) + + assert record.to_row("prefix/") == ( + "prefix/bin/script.py", + "", + "", + ) + def test_equality(self): record = RecordEntry.from_elements( "file.py",