Skip to content

Commit 5e7b47e

Browse files
barneygaleGlyphack
authored andcommitted
pythonGH-113528: Deoptimise pathlib._abc.PathBase._make_child_relpath() (python#113532)
Call straight through to `joinpath()` in `PathBase._make_child_relpath()`. Move optimised/caching code to `pathlib.Path._make_child_relpath()`
1 parent 33b5824 commit 5e7b47e

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

Lib/pathlib/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,22 @@ def _make_child_entry(self, entry):
405405
path._tail_cached = self._tail + [entry.name]
406406
return path
407407

408+
def _make_child_relpath(self, name):
409+
path_str = str(self)
410+
tail = self._tail
411+
if tail:
412+
path_str = f'{path_str}{self.pathmod.sep}{name}'
413+
elif path_str != '.':
414+
path_str = f'{path_str}{name}'
415+
else:
416+
path_str = name
417+
path = self.with_segments(path_str)
418+
path._str = path_str
419+
path._drv = self.drive
420+
path._root = self.root
421+
path._tail_cached = tail + [name]
422+
return path
423+
408424
def glob(self, pattern, *, case_sensitive=None, follow_symlinks=None):
409425
"""Iterate over this subtree and yield all existing files (of any
410426
kind, including directories) matching the given relative pattern.

Lib/pathlib/_abc.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -753,20 +753,7 @@ def _make_child_entry(self, entry):
753753
return entry
754754

755755
def _make_child_relpath(self, name):
756-
path_str = str(self)
757-
tail = self._tail
758-
if tail:
759-
path_str = f'{path_str}{self.pathmod.sep}{name}'
760-
elif path_str != '.':
761-
path_str = f'{path_str}{name}'
762-
else:
763-
path_str = name
764-
path = self.with_segments(path_str)
765-
path._str = path_str
766-
path._drv = self.drive
767-
path._root = self.root
768-
path._tail_cached = tail + [name]
769-
return path
756+
return self.joinpath(name)
770757

771758
def glob(self, pattern, *, case_sensitive=None, follow_symlinks=None):
772759
"""Iterate over this subtree and yield all existing files (of any

0 commit comments

Comments
 (0)