Skip to content

Commit 68c1643

Browse files
barneygalekulikjak
authored andcommitted
Replace pathlib._abc.PathModuleBase.splitroot() with splitdrive() (python#114065)
This allows users of the `pathlib-abc` PyPI package to use `posixpath` or `ntpath` as a path module in versions of Python lacking `os.path.splitroot()` (3.11 and before).
1 parent 0f4729c commit 68c1643

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

Lib/pathlib/_abc.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,11 @@ def split(self, path):
165165
"""
166166
self._unsupported('split()')
167167

168-
def splitroot(self, path):
169-
"""Split the pathname path into a 3-item tuple (drive, root, tail),
170-
where *drive* is a device name or mount point, *root* is a string of
171-
separators after the drive, and *tail* is everything after the root.
172-
Any part may be empty."""
173-
self._unsupported('splitroot()')
168+
def splitdrive(self, path):
169+
"""Split the path into a 2-item tuple (drive, tail), where *drive* is
170+
a device name or mount point, and *tail* is everything after the
171+
drive. Either part may be empty."""
172+
self._unsupported('splitdrive()')
174173

175174
def normcase(self, path):
176175
"""Normalize the case of the path."""
@@ -227,18 +226,17 @@ def as_posix(self):
227226
@property
228227
def drive(self):
229228
"""The drive prefix (letter or UNC path), if any."""
230-
return self.pathmod.splitroot(self._raw_path)[0]
229+
return self.pathmod.splitdrive(self.anchor)[0]
231230

232231
@property
233232
def root(self):
234233
"""The root of the path, if any."""
235-
return self.pathmod.splitroot(self._raw_path)[1]
234+
return self.pathmod.splitdrive(self.anchor)[1]
236235

237236
@property
238237
def anchor(self):
239238
"""The concatenation of the drive and root, or ''."""
240-
drive, root, _ = self.pathmod.splitroot(self._raw_path)
241-
return drive + root
239+
return self._stack[0]
242240

243241
@property
244242
def name(self):

Lib/test/test_pathlib/test_pathlib_abc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test_unsupported_operation(self):
2727
m.sep
2828
self.assertRaises(e, m.join, 'foo')
2929
self.assertRaises(e, m.split, 'foo')
30-
self.assertRaises(e, m.splitroot, 'foo')
30+
self.assertRaises(e, m.splitdrive, 'foo')
3131
self.assertRaises(e, m.normcase, 'foo')
3232
self.assertRaises(e, m.isabs, 'foo')
3333

0 commit comments

Comments
 (0)