test whether the py3.12 relative_to works on windows

This commit is contained in:
sneakers-the-rat 2024-09-23 16:22:02 -07:00
parent 6253c47e37
commit 3d326dfc8b
Signed by untrusted user who does not match committer: jonny
GPG key ID: 6DCB96EF1E4D232D

View file

@ -27,9 +27,9 @@ def jsonize_array(value: Any, info: SerializationInfo) -> Union[list, dict]:
if info.context.get("absolute_paths", False): if info.context.get("absolute_paths", False):
array = _absolutize_paths(array) array = _absolutize_paths(array)
# else: else:
# relative_to = info.context.get("relative_to", ".") relative_to = info.context.get("relative_to", ".")
# array = _relativize_paths(array, relative_to) array = _relativize_paths(array, relative_to)
return array return array
@ -97,4 +97,45 @@ def relative_path(target: Path, origin: Path) -> Path:
return Path(target).resolve().relative_to(Path(origin).resolve()) return Path(target).resolve().relative_to(Path(origin).resolve())
except ValueError: # target does not start with origin except ValueError: # target does not start with origin
# recursion with origin (eventually origin is root so try will succeed) # recursion with origin (eventually origin is root so try will succeed)
return Path("..").joinpath(relative_path(target, Path(origin).parent)) try:
return Path("..").joinpath(relative_path(target, Path(origin).parent))
except ValueError:
# break recursion in windows when
pass
def relative_to(self: Path, other: Path, walk_up=True) -> Path:
"""
"Backport" of :meth:`pathlib.Path.relative_to` with ``walk_up=True``
that's not available pre 3.12.
Return the relative path to another path identified by the passed
arguments. If the operation is not possible (because this is not
related to the other path), raise ValueError.
The *walk_up* parameter controls whether `..` may be used to resolve
the path.
"""
if not isinstance(other, Path):
other = self.with_segments(other)
anchor0, parts0 = self._stack
anchor1, parts1 = other._stack
if anchor0 != anchor1:
raise ValueError(
f"{self._raw_path!r} and {other._raw_path!r} have different anchors"
)
while parts0 and parts1 and parts0[-1] == parts1[-1]:
parts0.pop()
parts1.pop()
for part in parts1:
if not part or part == ".":
pass
elif not walk_up:
raise ValueError(
f"{self._raw_path!r} is not in the subpath of {other._raw_path!r}"
)
elif part == "..":
raise ValueError(f"'..' segment in {other._raw_path!r} cannot be walked")
else:
parts0.append("..")
return self.with_segments("", *reversed(parts0))