diff --git a/CHANGES/1382.feature.rst b/CHANGES/1382.feature.rst new file mode 120000 index 000000000..62a3dbc9b --- /dev/null +++ b/CHANGES/1382.feature.rst @@ -0,0 +1 @@ +1340.feature.rst \ No newline at end of file diff --git a/yarl/_path.py b/yarl/_path.py index f7995ddc5..838aa94cb 100644 --- a/yarl/_path.py +++ b/yarl/_path.py @@ -2,6 +2,7 @@ from collections.abc import Sequence from contextlib import suppress +from itertools import chain from pathlib import PurePosixPath @@ -54,11 +55,11 @@ def calculate_relative_path(target: str, base: str) -> str: target_path = PurePosixPath(target) base_path = PurePosixPath(base) - if not base[-1] == "/": + if base[-1] != "/": base_path = base_path.parent - for step, path in enumerate((base_path, *base_path.parents)): - if target_path.is_relative_to(path): + for step, path in enumerate(chain((base_path,), base_path.parents)): + if path == target_path or path in target_path.parents: break elif path.name == "..": raise ValueError(f"'..' segment in {str(base_path)!r} cannot be walked")