summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/pathlib.py12
-rw-r--r--Misc/NEWS.d/next/Library/2022-12-28-00-28-43.gh-issue-100562.Hic0Z0.rst3
2 files changed, 10 insertions, 5 deletions
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index b959e85..a0678f6 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -748,10 +748,12 @@ class Path(PurePath):
@classmethod
def cwd(cls):
- """Return a new path pointing to the current working directory
- (as returned by os.getcwd()).
- """
- return cls(os.getcwd())
+ """Return a new path pointing to the current working directory."""
+ # We call 'absolute()' rather than using 'os.getcwd()' directly to
+ # enable users to replace the implementation of 'absolute()' in a
+ # subclass and benefit from the new behaviour here. This works because
+ # os.path.abspath('.') == os.getcwd().
+ return cls().absolute()
@classmethod
def home(cls):
@@ -825,7 +827,7 @@ class Path(PurePath):
"""
if self.is_absolute():
return self
- return self._from_parts([self.cwd()] + self._parts)
+ return self._from_parts([os.getcwd()] + self._parts)
def resolve(self, strict=False):
"""
diff --git a/Misc/NEWS.d/next/Library/2022-12-28-00-28-43.gh-issue-100562.Hic0Z0.rst b/Misc/NEWS.d/next/Library/2022-12-28-00-28-43.gh-issue-100562.Hic0Z0.rst
new file mode 100644
index 0000000..56a4265
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-12-28-00-28-43.gh-issue-100562.Hic0Z0.rst
@@ -0,0 +1,3 @@
+Improve performance of :meth:`pathlib.Path.absolute` by nearly 2x. This comes
+at the cost of a performance regression in :meth:`pathlib.Path.cwd`, which is
+generally used less frequently in user code.