summaryrefslogtreecommitdiffstats
path: root/Lib/pathlib.py
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2023-05-06 18:03:07 (GMT)
committerGitHub <noreply@github.com>2023-05-06 18:03:07 (GMT)
commitde7f694e3c92797fe65f04cd2c6941ed0446bb24 (patch)
tree362c32facc52502df7e993d51e6c84ae6de851f3 /Lib/pathlib.py
parent376137f6ec73e0800e49cec6100e401f6154b693 (diff)
downloadcpython-de7f694e3c92797fe65f04cd2c6941ed0446bb24.zip
cpython-de7f694e3c92797fe65f04cd2c6941ed0446bb24.tar.gz
cpython-de7f694e3c92797fe65f04cd2c6941ed0446bb24.tar.bz2
GH-103548: Improve performance of `pathlib.Path.[is_]absolute()` (GH-103549)
Improve performance of `pathlib.Path.absolute()` and `cwd()` by joining paths only when necessary. Also improve performance of `PurePath.is_absolute()` on Posix by skipping path parsing and normalization.
Diffstat (limited to 'Lib/pathlib.py')
-rw-r--r--Lib/pathlib.py11
1 files changed, 10 insertions, 1 deletions
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 9aa3c1e..480c354 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -664,7 +664,7 @@ class PurePath(object):
# ntpath.isabs() is defective - see GH-44626 .
if self._flavour is ntpath:
return bool(self.drive and self.root)
- return self._flavour.isabs(self)
+ return self._flavour.isabs(self._raw_path)
def is_reserved(self):
"""Return True if the path contains one of the special names reserved
@@ -873,6 +873,15 @@ class Path(PurePath):
cwd = self._flavour.abspath(self.drive)
else:
cwd = os.getcwd()
+ # Fast path for "empty" paths, e.g. Path("."), Path("") or Path().
+ # We pass only one argument to with_segments() to avoid the cost
+ # of joining, and we exploit the fact that getcwd() returns a
+ # fully-normalized string by storing it in _str. This is used to
+ # implement Path.cwd().
+ if not self.root and not self._tail:
+ result = self.with_segments(cwd)
+ result._str = cwd
+ return result
return self.with_segments(cwd, self)
def resolve(self, strict=False):