Skip to content

Commit ec72e03

Browse files
committed
pythonGH-113528: Deoptimise pathlib._abc.PathBase._make_child_relpath()
Call straight through to `joinpath()` in `PathBase._make_child_relpath()`. Move optimised/caching code to `pathlib.Path._make_child_relpath()`
1 parent 1b19d73 commit ec72e03

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
@@ -299,6 +299,22 @@ def iterdir(self):
299299
def _scandir(self):
300300
return os.scandir(self)
301301

302+
def _make_child_relpath(self, name):
303+
path_str = str(self)
304+
tail = self._tail
305+
if tail:
306+
path_str = f'{path_str}{self.pathmod.sep}{name}'
307+
elif path_str != '.':
308+
path_str = f'{path_str}{name}'
309+
else:
310+
path_str = name
311+
path = self.with_segments(path_str)
312+
path._str = path_str
313+
path._drv = self.drive
314+
path._root = self.root
315+
path._tail_cached = tail + [name]
316+
return path
317+
302318
def absolute(self):
303319
"""Return an absolute version of this path
304320
No normalization or symlink resolution is performed.

Lib/pathlib/_abc.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -789,20 +789,7 @@ def _scandir(self):
789789
return nullcontext(self.iterdir())
790790

791791
def _make_child_relpath(self, name):
792-
path_str = str(self)
793-
tail = self._tail
794-
if tail:
795-
path_str = f'{path_str}{self.pathmod.sep}{name}'
796-
elif path_str != '.':
797-
path_str = f'{path_str}{name}'
798-
else:
799-
path_str = name
800-
path = self.with_segments(path_str)
801-
path._str = path_str
802-
path._drv = self.drive
803-
path._root = self.root
804-
path._tail_cached = tail + [name]
805-
return path
792+
return self.joinpath(name)
806793

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

0 commit comments

Comments
 (0)