Skip to content

Commit 08d74cc

Browse files
committed
Move _relative_to compatibility to a compat module.
1 parent 4a3782d commit 08d74cc

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

importlib_metadata/__init__.py

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import posixpath
2020
import collections
2121

22+
from .compat.py38 import relative_fix
2223
from . import _adapters, _meta, _py39compat
2324
from ._collections import FreezableDefaultDict, Pair
2425
from ._compat import (
@@ -562,31 +563,14 @@ def _read_files_egginfo_installed(self):
562563
if not text or not subdir:
563564
return
564565

565-
site_path = self.locate_file('').resolve()
566566
paths = (
567-
self._relative_to(
568-
(subdir / name).resolve(),
569-
site_path,
570-
).as_posix()
567+
relative_fix((subdir / name).resolve())
568+
.relative_to(self.locate_file('').resolve(), walk_up=True)
569+
.as_posix()
571570
for name in text.splitlines()
572571
)
573572
return map('"{}"'.format, paths)
574573

575-
def _relative_to(self, path, root):
576-
"""
577-
Workaround for https://github.com/python/cpython/issues/67271 where ".."
578-
isn't added by pathlib.Path.relative_to() when path is not
579-
a subpath of root.
580-
581-
One example of such a package is dask-labextension, which uses
582-
jupyter-packaging to install JupyterLab javascript files outside
583-
of site-packages.
584-
"""
585-
try:
586-
return path.relative_to(root)
587-
except ValueError:
588-
return pathlib.Path(os.path.relpath(path, root))
589-
590574
def _read_files_egginfo_sources(self):
591575
"""
592576
Read SOURCES.txt and return lines in a similar CSV-parsable

importlib_metadata/compat/py38.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import os
2+
import pathlib
3+
import sys
4+
import types
5+
6+
7+
def wrap(path):
8+
"""
9+
Workaround for https://github.com/python/cpython/issues/67271 where ".."
10+
isn't added by pathlib.Path.relative_to() when path is not
11+
a subpath of root.
12+
One example of such a package is dask-labextension, which uses
13+
jupyter-packaging to install JupyterLab javascript files outside
14+
of site-packages.
15+
"""
16+
17+
def relative_to(root, *, walk_up=False):
18+
return pathlib.Path(os.path.relpath(path, root))
19+
20+
return types.SimpleNamespace(relative_to=relative_to)
21+
22+
23+
relative_fix = wrap if sys.version_info < (3, 9) else lambda x: x

0 commit comments

Comments
 (0)