Skip to content

Commit 7a88c12

Browse files
committed
gh-94909: fix joining of absolute and relative Windows paths in pathlib
1 parent d92b19e commit 7a88c12

File tree

3 files changed

+8
-18
lines changed

3 files changed

+8
-18
lines changed

Lib/pathlib.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,8 @@ def parse_parts(self, parts):
6565
sep = self.sep
6666
altsep = self.altsep
6767
drv = root = ''
68-
it = reversed(parts)
69-
for part in it:
70-
if not part:
71-
continue
68+
if parts:
69+
part = self.pathmod.join(*parts)
7270
if altsep:
7371
part = part.replace(altsep, sep)
7472
drv, root, rel = self.splitroot(part)
@@ -79,20 +77,6 @@ def parse_parts(self, parts):
7977
else:
8078
if rel and rel != '.':
8179
parsed.append(sys.intern(rel))
82-
if drv or root:
83-
if not drv:
84-
# If no drive is present, try to find one in the previous
85-
# parts. This makes the result of parsing e.g.
86-
# ("C:", "/", "a") reasonably intuitive.
87-
for part in it:
88-
if not part:
89-
continue
90-
if altsep:
91-
part = part.replace(altsep, sep)
92-
drv = self.splitroot(part)[0]
93-
if drv:
94-
break
95-
break
9680
if drv or root:
9781
parsed.append(drv + root)
9882
parsed.reverse()

Lib/test/test_pathlib.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ def test_parse_parts(self):
136136
check(['a', '/b', 'c'], ('', '\\', ['\\', 'b', 'c']))
137137
check(['Z:/a', '/b', 'c'], ('Z:', '\\', ['Z:\\', 'b', 'c']))
138138
check(['//?/Z:/a', '/b', 'c'], ('\\\\?\\Z:', '\\', ['\\\\?\\Z:\\', 'b', 'c']))
139+
# Joining with the same drive => the first path is appended to if
140+
# the second path is relative.
141+
check(['c:/a/b', 'c:x/y'], ('c:', '\\', ['c:\\', 'a', 'b', 'x', 'y']))
142+
check(['c:/a/b', 'c:/x/y'], ('c:', '\\', ['c:\\', 'x', 'y']))
139143

140144
def test_splitroot(self):
141145
f = self.flavour.splitroot
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix incorrect joining of relative Windows paths with drives in
2+
:class:`pathlib.PurePath` initializer.

0 commit comments

Comments
 (0)