Suppress a possible relative_to() failure, fixes #9894

This commit is contained in:
Stephan Loyd 2022-05-11 20:31:21 +08:00
parent a88c0f3bb0
commit 444e54deed
4 changed files with 13 additions and 2 deletions

View File

@ -312,6 +312,7 @@ Stefan Scherfke
Stefan Zimmermann
Stefano Taschini
Steffen Allner
Stephan Loyd
Stephan Obermann
Sven-Hendrik Haase
Sylvain Marié

View File

@ -0,0 +1 @@
Suppress a potential pathlib relative_to() error, which is possible to be raised from ExceptionInfo.getexpr() when running Python from double-slashed path (like //usr/bin/python3) and single-slashed tests path.

View File

@ -707,8 +707,12 @@ def bestrelpath(directory: Path, dest: Path) -> str:
# Can be the case for a relative path and an absolute path.
if not base:
return str(dest)
reldirectory = directory.relative_to(base)
reldest = dest.relative_to(base)
try:
reldirectory = directory.relative_to(base)
reldest = dest.relative_to(base)
except ValueError:
# GH 9894
return str(dest)
return os.path.join(
# Back from directory to base.
*([os.pardir] * len(reldirectory.parts)),

View File

@ -413,6 +413,11 @@ def test_bestrelpath() -> None:
assert bestrelpath(curdir, curdir.parent) == ".."
assert bestrelpath(curdir, Path("hello")) == "hello"
# GH 9894
directory = Path("/var/tmp/pytest-issue")
dest = Path("//usr/lib/python3.9/importlib/__init__.py")
assert bestrelpath(directory, dest) == str(dest)
def test_commonpath() -> None:
path = Path("/foo/bar/baz/path")