GH-127807: pathlib ABCs: remove PurePathBase._raw_paths (#127883)

Remove the `PurePathBase` initializer, and make `with_segments()` and
`__str__()` abstract. This allows us to drop the `_raw_paths` attribute,
and also the `Parser.join()` protocol method.
This commit is contained in:
Barney Gale 2024-12-22 01:17:59 +00:00 committed by GitHub
parent 2a66dd33df
commit a959ea1b0a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 92 additions and 96 deletions

View file

@ -53,7 +53,15 @@ class PurePathBaseTest(unittest.TestCase):
class DummyPurePath(PurePathBase):
__slots__ = ()
__slots__ = ('_segments',)
def __init__(self, *segments):
self._segments = segments
def __str__(self):
if self._segments:
return self.parser.join(*self._segments)
return ''
def __eq__(self, other):
if not isinstance(other, DummyPurePath):
@ -66,6 +74,9 @@ class DummyPurePath(PurePathBase):
def __repr__(self):
return "{}({!r})".format(self.__class__.__name__, self.as_posix())
def with_segments(self, *pathsegments):
return type(self)(*pathsegments)
class DummyPurePathTest(unittest.TestCase):
cls = DummyPurePath
@ -97,30 +108,11 @@ class DummyPurePathTest(unittest.TestCase):
P('a/b/c')
P('/a/b/c')
def test_bytes(self):
P = self.cls
with self.assertRaises(TypeError):
P(b'a')
with self.assertRaises(TypeError):
P(b'a', 'b')
with self.assertRaises(TypeError):
P('a', b'b')
with self.assertRaises(TypeError):
P('a').joinpath(b'b')
with self.assertRaises(TypeError):
P('a') / b'b'
with self.assertRaises(TypeError):
b'a' / P('b')
with self.assertRaises(TypeError):
P('a').match(b'b')
with self.assertRaises(TypeError):
P('a').relative_to(b'b')
with self.assertRaises(TypeError):
P('a').with_name(b'b')
with self.assertRaises(TypeError):
P('a').with_stem(b'b')
with self.assertRaises(TypeError):
P('a').with_suffix(b'b')
def test_fspath_common(self):
self.assertRaises(TypeError, os.fspath, self.cls(''))
def test_as_bytes_common(self):
self.assertRaises(TypeError, bytes, self.cls(''))
def _check_str_subclass(self, *args):
# Issue #21127: it should be possible to construct a PurePath object
@ -1286,36 +1278,6 @@ class DummyPurePathTest(unittest.TestCase):
# Tests for the virtual classes.
#
class PathBaseTest(PurePathBaseTest):
cls = PathBase
def test_not_implemented_error(self):
p = self.cls('')
e = NotImplementedError
self.assertRaises(e, p.stat)
self.assertRaises(e, p.exists)
self.assertRaises(e, p.is_dir)
self.assertRaises(e, p.is_file)
self.assertRaises(e, p.is_symlink)
self.assertRaises(e, p.open)
self.assertRaises(e, p.read_bytes)
self.assertRaises(e, p.read_text)
self.assertRaises(e, p.write_bytes, b'foo')
self.assertRaises(e, p.write_text, 'foo')
self.assertRaises(e, p.iterdir)
self.assertRaises(e, lambda: list(p.glob('*')))
self.assertRaises(e, lambda: list(p.rglob('*')))
self.assertRaises(e, lambda: list(p.walk()))
self.assertRaises(e, p.readlink)
self.assertRaises(e, p.symlink_to, 'foo')
self.assertRaises(e, p.mkdir)
def test_fspath_common(self):
self.assertRaises(TypeError, os.fspath, self.cls(''))
def test_as_bytes_common(self):
self.assertRaises(TypeError, bytes, self.cls(''))
class DummyPathIO(io.BytesIO):
"""
@ -1342,11 +1304,19 @@ class DummyPath(PathBase):
Simple implementation of PathBase that keeps files and directories in
memory.
"""
__slots__ = ()
__slots__ = ('_segments')
_files = {}
_directories = {}
def __init__(self, *segments):
self._segments = segments
def __str__(self):
if self._segments:
return self.parser.join(*self._segments)
return ''
def __eq__(self, other):
if not isinstance(other, DummyPath):
return NotImplemented
@ -1358,6 +1328,9 @@ class DummyPath(PathBase):
def __repr__(self):
return "{}({!r})".format(self.__class__.__name__, self.as_posix())
def with_segments(self, *pathsegments):
return type(self)(*pathsegments)
def stat(self, *, follow_symlinks=True):
path = str(self).rstrip('/')
if path in self._files: