Skip to content

Commit a15a773

Browse files
authored
pythonGH-113528: Deoptimise pathlib._abc.PurePathBase.relative_to() (python#113529)
Replace use of `_from_parsed_parts()` with `with_segments()` in `PurePathBase.relative_to()`, and move the assignment of `_drv`, `_root` and `_tail_cached` slots into `PurePath.relative_to()`.
1 parent 37bd893 commit a15a773

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

Lib/pathlib/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ def relative_to(self, other, /, *_deprecated, walk_up=False):
245245
"scheduled for removal in Python 3.14")
246246
warnings.warn(msg, DeprecationWarning, stacklevel=2)
247247
other = self.with_segments(other, *_deprecated)
248-
return _abc.PurePathBase.relative_to(self, other, walk_up=walk_up)
248+
path = _abc.PurePathBase.relative_to(self, other, walk_up=walk_up)
249+
path._drv = path._root = ''
250+
path._tail_cached = path._raw_paths.copy()
251+
return path
249252

250253
def is_relative_to(self, other, /, *_deprecated):
251254
"""Return True if the path is relative to another path or False.

Lib/pathlib/_abc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ def relative_to(self, other, *, walk_up=False):
371371
else:
372372
raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors")
373373
parts = ['..'] * step + self._tail[len(path._tail):]
374-
return self._from_parsed_parts('', '', parts)
374+
return self.with_segments(*parts)
375375

376376
def is_relative_to(self, other):
377377
"""Return True if the path is relative to another path or False.

0 commit comments

Comments
 (0)