From 211e4c6e9c1ab60bb2577dda6587fbec79f679b2 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Wed, 26 Aug 2020 17:51:44 -0700 Subject: [PATCH] bpo-33660: Fix PosixPath to resolve a relative path on root (#21974) (cherry picked from commit 94ad6c674f7687ef22853cb8d42b440d6b42ddc8) Co-authored-by: Dong-hee Na --- Lib/pathlib.py | 5 ++++- Lib/test/test_pathlib.py | 9 +++++++++ .../Library/2018-06-12-23-30-41.bpo-33660.AdDn5Z.rst | 2 ++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2018-06-12-23-30-41.bpo-33660.AdDn5Z.rst diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 9f5e27b9117..babc443dd3b 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -329,7 +329,10 @@ class _PosixFlavour(_Flavour): # parent dir path, _, _ = path.rpartition(sep) continue - newpath = path + sep + name + if path.endswith(sep): + 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 1589282886b..3da35710b9d 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -2347,6 +2347,15 @@ class PosixPathTest(_BasePathTest, unittest.TestCase): st = os.stat(join('other_new_file')) self.assertEqual(stat.S_IMODE(st.st_mode), 0o644) + def test_resolve_root(self): + current_directory = os.getcwd() + try: + os.chdir('/') + p = self.cls('spam') + self.assertEqual(str(p.resolve()), '/spam') + finally: + os.chdir(current_directory) + def test_touch_mode(self): old_mask = os.umask(0) self.addCleanup(os.umask, old_mask) diff --git a/Misc/NEWS.d/next/Library/2018-06-12-23-30-41.bpo-33660.AdDn5Z.rst b/Misc/NEWS.d/next/Library/2018-06-12-23-30-41.bpo-33660.AdDn5Z.rst new file mode 100644 index 00000000000..cce3dbb1c6e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-06-12-23-30-41.bpo-33660.AdDn5Z.rst @@ -0,0 +1,2 @@ +Fix pathlib.PosixPath to resolve a relative path located on the root +directory properly.