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)