mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
pathlib ABCs: drop partial, broken, untested support for bytes
paths. (#114777)
Methods like `full_match()`, `glob()`, etc, are difficult to make work with byte paths, and it's not worth the effort. This patch makes `PurePathBase` raise `TypeError` when given non-`str` path segments.
This commit is contained in:
parent
1667c28686
commit
574291963f
3 changed files with 29 additions and 21 deletions
|
@ -207,6 +207,9 @@ class PurePathBase:
|
||||||
|
|
||||||
def __init__(self, path, *paths):
|
def __init__(self, path, *paths):
|
||||||
self._raw_path = self.pathmod.join(path, *paths) if paths else path
|
self._raw_path = self.pathmod.join(path, *paths) if paths else path
|
||||||
|
if not isinstance(self._raw_path, str):
|
||||||
|
raise TypeError(
|
||||||
|
f"path should be a str, not {type(self._raw_path).__name__!r}")
|
||||||
self._resolving = False
|
self._resolving = False
|
||||||
|
|
||||||
def with_segments(self, *pathsegments):
|
def with_segments(self, *pathsegments):
|
||||||
|
@ -321,8 +324,6 @@ class PurePathBase:
|
||||||
other = self.with_segments(other)
|
other = self.with_segments(other)
|
||||||
anchor0, parts0 = self._stack
|
anchor0, parts0 = self._stack
|
||||||
anchor1, parts1 = other._stack
|
anchor1, parts1 = other._stack
|
||||||
if isinstance(anchor0, str) != isinstance(anchor1, str):
|
|
||||||
raise TypeError(f"{self._raw_path!r} and {other._raw_path!r} have different types")
|
|
||||||
if anchor0 != anchor1:
|
if anchor0 != anchor1:
|
||||||
raise ValueError(f"{self._raw_path!r} and {other._raw_path!r} have different anchors")
|
raise ValueError(f"{self._raw_path!r} and {other._raw_path!r} have different anchors")
|
||||||
while parts0 and parts1 and parts0[-1] == parts1[-1]:
|
while parts0 and parts1 and parts0[-1] == parts1[-1]:
|
||||||
|
@ -346,8 +347,6 @@ class PurePathBase:
|
||||||
other = self.with_segments(other)
|
other = self.with_segments(other)
|
||||||
anchor0, parts0 = self._stack
|
anchor0, parts0 = self._stack
|
||||||
anchor1, parts1 = other._stack
|
anchor1, parts1 = other._stack
|
||||||
if isinstance(anchor0, str) != isinstance(anchor1, str):
|
|
||||||
raise TypeError(f"{self._raw_path!r} and {other._raw_path!r} have different types")
|
|
||||||
if anchor0 != anchor1:
|
if anchor0 != anchor1:
|
||||||
return False
|
return False
|
||||||
while parts0 and parts1 and parts0[-1] == parts1[-1]:
|
while parts0 and parts1 and parts0[-1] == parts1[-1]:
|
||||||
|
|
|
@ -189,7 +189,7 @@ class PurePathTest(test_pathlib_abc.DummyPurePathTest):
|
||||||
self._check_str(p.__fspath__(), ('a/b',))
|
self._check_str(p.__fspath__(), ('a/b',))
|
||||||
self._check_str(os.fspath(p), ('a/b',))
|
self._check_str(os.fspath(p), ('a/b',))
|
||||||
|
|
||||||
def test_bytes(self):
|
def test_bytes_exc_message(self):
|
||||||
P = self.cls
|
P = self.cls
|
||||||
message = (r"argument should be a str or an os\.PathLike object "
|
message = (r"argument should be a str or an os\.PathLike object "
|
||||||
r"where __fspath__ returns a str, not 'bytes'")
|
r"where __fspath__ returns a str, not 'bytes'")
|
||||||
|
@ -199,22 +199,6 @@ class PurePathTest(test_pathlib_abc.DummyPurePathTest):
|
||||||
P(b'a', 'b')
|
P(b'a', 'b')
|
||||||
with self.assertRaisesRegex(TypeError, message):
|
with self.assertRaisesRegex(TypeError, message):
|
||||||
P('a', b'b')
|
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_as_bytes_common(self):
|
def test_as_bytes_common(self):
|
||||||
sep = os.fsencode(self.sep)
|
sep = os.fsencode(self.sep)
|
||||||
|
|
|
@ -155,6 +155,31 @@ class DummyPurePathTest(unittest.TestCase):
|
||||||
P('a/b/c')
|
P('a/b/c')
|
||||||
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 _check_str_subclass(self, *args):
|
def _check_str_subclass(self, *args):
|
||||||
# Issue #21127: it should be possible to construct a PurePath object
|
# Issue #21127: it should be possible to construct a PurePath object
|
||||||
# from a str subclass instance, and it then gets converted to
|
# from a str subclass instance, and it then gets converted to
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue