From ca7119eb4d279a62731d6b72dfafd41e12e46c54 Mon Sep 17 00:00:00 2001 From: barneygale Date: Mon, 4 Dec 2023 22:11:41 +0000 Subject: [PATCH] Fix `WindowsPath('/').absolute()` --- Lib/pathlib.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index c4b266650725a6..965f449f1b92c4 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -1416,10 +1416,8 @@ def absolute(self): if self.is_absolute(): return self elif self.root: - cwd = os.getcwd() - if self._tail: - cwd_drv = os.path.splitroot(cwd)[0] - return self._from_parsed_parts(cwd_drv, self.root, self._tail) + cwd_drv = os.path.splitroot(os.getcwd())[0] + return self._from_parsed_parts(cwd_drv, self.root, self._tail) else: cwd = os.path.abspath(self.drive) if self.drive else os.getcwd() if self._tail: @@ -1432,11 +1430,12 @@ def absolute(self): else: cwd_tail = self._tail return self._from_parsed_parts(cwd_drv, cwd_root, cwd_tail) - # Optimization: store the result of os.getcwd() as the normalized - # string representation of the path. - result = self.with_segments(cwd) - result._str = cwd - return result + else: + # Optimization: store the result of os.getcwd() as the normalized + # string representation of the path. + result = self.with_segments(cwd) + result._str = cwd + return result def resolve(self, strict=False): """