Skip to content

Commit 7475aa2

Browse files
bpo-33660: Fix PosixPath to resolve a relative path on root (pythonGH-21975)
(cherry picked from commit 94ad6c6) Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
1 parent 57b6988 commit 7475aa2

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

Lib/pathlib.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,10 @@ def _resolve(path, rest):
329329
# parent dir
330330
path, _, _ = path.rpartition(sep)
331331
continue
332-
newpath = path + sep + name
332+
if path.endswith(sep):
333+
newpath = path + name
334+
else:
335+
newpath = path + sep + name
333336
if newpath in seen:
334337
# Already seen this path
335338
path = seen[newpath]

Lib/test/test_pathlib.py

+9
Original file line numberDiff line numberDiff line change
@@ -2214,6 +2214,15 @@ def test_open_mode(self):
22142214
st = os.stat(join('other_new_file'))
22152215
self.assertEqual(stat.S_IMODE(st.st_mode), 0o644)
22162216

2217+
def test_resolve_root(self):
2218+
current_directory = os.getcwd()
2219+
try:
2220+
os.chdir('/')
2221+
p = self.cls('spam')
2222+
self.assertEqual(str(p.resolve()), '/spam')
2223+
finally:
2224+
os.chdir(current_directory)
2225+
22172226
def test_touch_mode(self):
22182227
old_mask = os.umask(0)
22192228
self.addCleanup(os.umask, old_mask)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix pathlib.PosixPath to resolve a relative path located on the root
2+
directory properly.

0 commit comments

Comments
 (0)