Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions importlib_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,13 +418,30 @@ def _from_text(text):
)


def _normalize_record_path(path):
"""
RECORD files may separate directories with backslashes on Windows.
The paths describe package-relative locations, so treat backslashes
as separators everywhere for consistent, portable results.
"""
return os.fspath(path).replace('\\', '/')


class PackagePath(pathlib.PurePosixPath):
"""A reference to a path in a package"""

hash: FileHash | None
size: int
dist: IDistribution

def __new__(cls, *args):
return super().__new__(cls, *map(_normalize_record_path, args))

def __init__(self, *args):
# Python 3.12 moved path parsing from __new__ to __init__.
if pathlib.PurePosixPath.__init__ is not object.__init__:
super().__init__(*map(_normalize_record_path, args))

def read_text(self, encoding: str = 'utf-8') -> str:
return self.locate().read_text(encoding=encoding)

Expand Down
4 changes: 4 additions & 0 deletions newsfragments/528.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
``PackagePath`` now treats backslashes in RECORD entries as directory
separators on every platform. ``name``, ``parent``, ``suffix``, and
``as_posix()`` are therefore consistent for Windows-style paths, while POSIX
paths are unchanged.
50 changes: 50 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextlib
import importlib
import io
import pathlib
import pickle
import re
import subprocess
Expand All @@ -16,6 +17,7 @@
EntryPoint,
MetadataNotFound,
PackageNotFoundError,
PackagePath,
_unique,
diagnose,
distributions,
Expand Down Expand Up @@ -86,6 +88,54 @@ def test_invalid_inputs_to_from_name(self, name):
Distribution.from_name(name)


class PackagePathTests(unittest.TestCase):
@fixtures.parameterize(
dict(path='pkg\\module.py', expected='pkg/module.py'),
dict(path='pkg\\mixed/name.py', expected='pkg/mixed/name.py'),
dict(path='pkg/mixed/name.py', expected='pkg/mixed/name.py'),
dict(
path='C:\\venv\\Scripts\\sample.exe',
expected='C:/venv/Scripts/sample.exe',
),
)
def test_normalizes_windows_separators(self, path, expected):
package_path = PackagePath(path)
expected_path = pathlib.PurePosixPath(expected)

assert package_path.as_posix() == expected
assert str(package_path) == expected
assert package_path.parts == expected_path.parts
assert package_path.name == expected_path.name
assert package_path.parent.as_posix() == expected_path.parent.as_posix()
assert package_path.suffix == expected_path.suffix


class RecordPathTests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
def test_files_normalize_windows_separators(self):
fixtures.build_files(
{
'pkg': {
'module.py': '',
'mixed': {'name.py': ''},
},
'pkg-1.0.dist-info': {
'METADATA': 'Name: pkg\nVersion: 1.0\n',
'RECORD': 'pkg\\module.py,,\npkg/mixed\\name.py,,\n',
},
},
self.site_dir,
)

files = Distribution.from_name('pkg').files
assert files is not None

assert [path.as_posix() for path in files] == [
'pkg/module.py',
'pkg/mixed/name.py',
]
assert files[0].name == 'module.py'


class ImportTests(fixtures.DistInfoPkg, unittest.TestCase):
def test_import_nonexistent_module(self):
# Ensure that the MetadataPathFinder does not crash an import of a
Expand Down