mirror of
https://github.com/python/cpython.git
synced 2025-07-30 06:34:15 +00:00
GH-111429: Speed up pathlib.PurePath.[is_]relative_to()
(#111431)
This commit is contained in:
parent
b2af50cb02
commit
d7cef7bc7e
2 changed files with 10 additions and 4 deletions
|
@ -647,9 +647,11 @@ class PurePath:
|
||||||
"scheduled for removal in Python {remove}")
|
"scheduled for removal in Python {remove}")
|
||||||
warnings._deprecated("pathlib.PurePath.relative_to(*args)", msg,
|
warnings._deprecated("pathlib.PurePath.relative_to(*args)", msg,
|
||||||
remove=(3, 14))
|
remove=(3, 14))
|
||||||
other = self.with_segments(other, *_deprecated)
|
other = self.with_segments(other, *_deprecated)
|
||||||
|
elif not isinstance(other, PurePath):
|
||||||
|
other = self.with_segments(other)
|
||||||
for step, path in enumerate([other] + list(other.parents)):
|
for step, path in enumerate([other] + list(other.parents)):
|
||||||
if self.is_relative_to(path):
|
if path == self or path in self.parents:
|
||||||
break
|
break
|
||||||
elif not walk_up:
|
elif not walk_up:
|
||||||
raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}")
|
raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}")
|
||||||
|
@ -658,7 +660,7 @@ class PurePath:
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors")
|
raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors")
|
||||||
parts = ['..'] * step + self._tail[len(path._tail):]
|
parts = ['..'] * step + self._tail[len(path._tail):]
|
||||||
return self.with_segments(*parts)
|
return self._from_parsed_parts('', '', parts)
|
||||||
|
|
||||||
def is_relative_to(self, other, /, *_deprecated):
|
def is_relative_to(self, other, /, *_deprecated):
|
||||||
"""Return True if the path is relative to another path or False.
|
"""Return True if the path is relative to another path or False.
|
||||||
|
@ -669,7 +671,9 @@ class PurePath:
|
||||||
"scheduled for removal in Python {remove}")
|
"scheduled for removal in Python {remove}")
|
||||||
warnings._deprecated("pathlib.PurePath.is_relative_to(*args)",
|
warnings._deprecated("pathlib.PurePath.is_relative_to(*args)",
|
||||||
msg, remove=(3, 14))
|
msg, remove=(3, 14))
|
||||||
other = self.with_segments(other, *_deprecated)
|
other = self.with_segments(other, *_deprecated)
|
||||||
|
elif not isinstance(other, PurePath):
|
||||||
|
other = self.with_segments(other)
|
||||||
return other == self or other in self.parents
|
return other == self or other in self.parents
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Speed up :meth:`pathlib.PurePath.relative_to` and
|
||||||
|
:meth:`~pathlib.PurePath.is_relative_to`.
|
Loading…
Add table
Add a link
Reference in a new issue