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%
This commit is contained in:
Barney Gale 2023-03-05 22:00:56 +00:00 committed by GitHub
parent 96e1022929
commit 3572c861d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 1 deletions

View file

@ -275,9 +275,12 @@ class PurePath(object):
def _parse_parts(cls, parts):
if not parts:
return '', '', []
elif len(parts) == 1:
path = os.fspath(parts[0])
else:
path = cls._flavour.join(*parts)
sep = cls._flavour.sep
altsep = cls._flavour.altsep
path = cls._flavour.join(*parts)
if altsep:
path = path.replace(altsep, sep)
drv, root, rel = cls._flavour.splitroot(path)

View file

@ -0,0 +1,2 @@
Speed up :class:`pathlib.PurePath` construction by calling
:func:`os.path.join` only when two or more arguments are given.