From 9b0724c221f0b7e7a2634c4ec89daec32dc376e3 Mon Sep 17 00:00:00 2001 From: Manuel Barkhau Date: Wed, 26 Aug 2020 22:41:49 +0000 Subject: [PATCH] initial fix and test Path.resolve() at / On posix, when `Path.cwd() == "/"`, the `Path("var").resolve()` produced `"//var"`. This is because the sep usually needs to be added together with a prefix/base to a relative path, as the prefix/base does not usually end with the sep itself. In this respect, the root directory is a corner-case that was previously missed. --- Lib/pathlib.py | 8 +++++++- Lib/test/test_pathlib.py | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 9f5e27b91178e6..af332e9f105b13 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -329,7 +329,13 @@ def _resolve(path, rest): # parent dir path, _, _ = path.rpartition(sep) continue - newpath = path + sep + name + + if path.endswith(sep): + # special case for "/" root directory + newpath = path + name + else: + newpath = path + sep + name + if newpath in seen: # Already seen this path path = seen[newpath] diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index 04f7c3d86671bf..904626c38f028a 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -2385,6 +2385,19 @@ def test_resolve_loop(self): # Non-strict self._check_symlink_loop(BASE, 'linkW', 'foo', strict=False) + def test_resolve_pwd_root(self): + P = self.cls + + # Resolve relative path in root directory. + old_path = os.getcwd() + os.chdir("/") + try: + given = P("var").resolve() + expect = P("/var") + self.assertEqual(given, expect) + finally: + os.chdir(old_path) + def test_glob(self): P = self.cls p = P(BASE)