Skip to content

Commit 94ad6c6

Browse files
corona10ambv
authored andcommitted
bpo-33660: Fix PosixPath to resolve a relative path on root
1 parent 82e7948 commit 94ad6c6

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

Lib/pathlib.py

Lines changed: 4 additions & 1 deletion
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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,6 +2349,15 @@ def test_open_mode(self):
23492349
st = os.stat(join('other_new_file'))
23502350
self.assertEqual(stat.S_IMODE(st.st_mode), 0o644)
23512351

2352+
def test_resolve_root(self):
2353+
current_directory = os.getcwd()
2354+
try:
2355+
os.chdir('/')
2356+
p = self.cls('spam')
2357+
self.assertEqual(str(p.resolve()), '/spam')
2358+
finally:
2359+
os.chdir(current_directory)
2360+
23522361
def test_touch_mode(self):
23532362
old_mask = os.umask(0)
23542363
self.addCleanup(os.umask, old_mask)
Lines changed: 2 additions & 0 deletions
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)