8000 GH-101362: Call join() only when >1 argument supplied to pathlib.Pure… · python/cpython@3572c86 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3572c86

Browse files
authored
GH-101362: Call join() only when >1 argument supplied to pathlib.PurePath() (#101665)
GH-101362: Call join() only when >1 argument supplied to pathlib.PurePath This reduces the time taken to run `PurePath("foo")` by ~15%
1 parent 96e1022 commit 3572c86

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

Lib/pathlib.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,12 @@ def __reduce__(self):
275275
def _parse_parts(cls, parts):
276276
if not parts:
277277
return '', '', []
278+
elif len(parts) == 1:
279+
path = os.fspath(parts[0])
280+
else:
281+
path = cls._flavour.join(*parts)
278282
sep = cls._flavour.sep
279283
altsep = cls._flavour.altsep
280-
path = cls._flavour.join(*parts)
281284
if altsep:
282285
path = path.replace(altsep, sep)
283286
drv, root, rel = cls._flavour.splitroot(path)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Speed up :class:`pathlib.PurePath` construction by calling
2+
:func:`os.path.join` only when two or more arguments are given.

0 commit comments

Comments
 (0)
0