Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-46148: Optimize pathlib #30226

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import warnings
from _collections_abc import Sequence
from errno import ENOENT, ENOTDIR, EBADF, ELOOP
from operator import attrgetter
from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO
from urllib.parse import quote_from_bytes as urlquote_from_bytes

Expand Down Expand Up @@ -364,7 +363,7 @@ def group(self, path):
#
# Globbing helpers
#

@functools.lru_cache
def _make_selector(pattern_parts, flavour):
pat = pattern_parts[0]
child_parts = pattern_parts[1:]
Expand All @@ -378,9 +377,6 @@ def _make_selector(pattern_parts, flavour):
cls = _PreciseSelector
return cls(pat, child_parts, flavour)

if hasattr(functools, "lru_cache"):
_make_selector = functools.lru_cache()(_make_selector)


class _Selector:
"""A selector matches a specific glob pattern part against the children
Expand Down Expand Up @@ -565,6 +561,7 @@ def __reduce__(self):
return (self.__class__, tuple(self._parts))

@classmethod
@functools.cache
def _parse_args(cls, args):
# This is useful when you don't want to create an instance, just
# canonicalize some constructor arguments.
Expand Down Expand Up @@ -693,11 +690,15 @@ def __ge__(self, other):
def __class_getitem__(cls, type):
return cls

drive = property(attrgetter('_drv'),
doc="""The drive prefix (letter or UNC path), if any.""")
@property
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved
def drive(self):
"""The drive prefix (letter or UNC path), if any."""
return self._drv

root = property(attrgetter('_root'),
doc="""The root of the path, if any.""")
@property
def root(self):
"""The root of the path, if any."""
return self._root

@property
def anchor(self):
Expand Down Expand Up @@ -856,7 +857,7 @@ def __truediv__(self, key):

def __rtruediv__(self, key):
try:
return self._from_parts([key] + self._parts)
return self._from_parts((key, *self._parts))
except TypeError:
return NotImplemented

Expand Down Expand Up @@ -1057,7 +1058,7 @@ def absolute(self):
return self
# FIXME this must defer to the specific flavour (and, under Windows,
# use nt._getfullpathname())
return self._from_parts([self._accessor.getcwd()] + self._parts)
return self._from_parts((self._accessor.getcwd(), *self._parts))

def resolve(self, strict=False):
"""
Expand Down Expand Up @@ -1436,7 +1437,7 @@ def expanduser(self):
homedir = self._accessor.expanduser(self._parts[0])
if homedir[:1] == "~":
raise RuntimeError("Could not determine home directory.")
return self._from_parts([homedir] + self._parts[1:])
return self._from_parts((homedir, *self._parts[1:]))

return self

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Speedup :class:`~pathlib.Path` creation up to ``3x``. Patch by Kumar Aditya.