Skip to content

Commit

Permalink
Improve performance of calculate_relative_path (#1382)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 22, 2024
1 parent ef0b5e3 commit 9348e88
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES/1382.feature.rst
7 changes: 4 additions & 3 deletions yarl/_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from collections.abc import Sequence
from contextlib import suppress
from itertools import chain
from pathlib import PurePosixPath


Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 9348e88

Please sign in to comment.