Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-94909: fix joining of absolute and relative Windows paths in pathlib
  • Loading branch information
barneygale committed Jul 29, 2022
commit 7a88c124f96f2c3acd9b8dff0bf57c56783872f5
20 changes: 2 additions & 18 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ def parse_parts(self, parts):
sep = self.sep
altsep = self.altsep
drv = root = ''
it = reversed(parts)
for part in it:
if not part:
continue
if parts:
part = self.pathmod.join(*parts)
if altsep:
part = part.replace(altsep, sep)
drv, root, rel = self.splitroot(part)
Expand All @@ -79,20 +77,6 @@ def parse_parts(self, parts):
else:
if rel and rel != '.':
parsed.append(sys.intern(rel))
if drv or root:
if not drv:
# If no drive is present, try to find one in the previous
# parts. This makes the result of parsing e.g.
# ("C:", "/", "a") reasonably intuitive.
for part in it:
if not part:
continue
if altsep:
part = part.replace(altsep, sep)
drv = self.splitroot(part)[0]
if drv:
break
break
if drv or root:
parsed.append(drv + root)
parsed.reverse()
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ def test_parse_parts(self):
check(['a', '/b', 'c'], ('', '\\', ['\\', 'b', 'c']))
check(['Z:/a', '/b', 'c'], ('Z:', '\\', ['Z:\\', 'b', 'c']))
check(['//?/Z:/a', '/b', 'c'], ('\\\\?\\Z:', '\\', ['\\\\?\\Z:\\', 'b', 'c']))
# Joining with the same drive => the first path is appended to if
# the second path is relative.
check(['c:/a/b', 'c:x/y'], ('c:', '\\', ['c:\\', 'a', 'b', 'x', 'y']))
check(['c:/a/b', 'c:/x/y'], ('c:', '\\', ['c:\\', 'x', 'y']))

def test_splitroot(self):
f = self.flavour.splitroot
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix incorrect joining of relative Windows paths with drives in
:class:`pathlib.PurePath` initializer.