Skip to content

Commit

Permalink
pathlib ABCs: drop partial, broken, untested support for bytes path…
Browse files Browse the repository at this point in the history
…s. (python#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.
  • Loading branch information
barneygale authored Jan 31, 2024
1 parent 1667c28 commit 5742919
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
7 changes: 3 additions & 4 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ class PurePathBase:

def __init__(self, path, *paths):
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

def with_segments(self, *pathsegments):
Expand Down Expand Up @@ -321,8 +324,6 @@ def relative_to(self, other, *, walk_up=False):
other = self.with_segments(other)
anchor0, parts0 = self._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:
raise ValueError(f"{self._raw_path!r} and {other._raw_path!r} have different anchors")
while parts0 and parts1 and parts0[-1] == parts1[-1]:
Expand All @@ -346,8 +347,6 @@ def is_relative_to(self, other):
other = self.with_segments(other)
anchor0, parts0 = self._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:
return False
while parts0 and parts1 and parts0[-1] == parts1[-1]:
Expand Down
18 changes: 1 addition & 17 deletions Lib/test/test_pathlib/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def test_fspath_common(self):
self._check_str(p.__fspath__(), ('a/b',))
self._check_str(os.fspath(p), ('a/b',))

def test_bytes(self):
def test_bytes_exc_message(self):
P = self.cls
message = (r"argument should be a str or an os\.PathLike object "
r"where __fspath__ returns a str, not 'bytes'")
Expand All @@ -199,22 +199,6 @@ def test_bytes(self):
P(b'a', 'b')
with self.assertRaisesRegex(TypeError, message):
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):
sep = os.fsencode(self.sep)
Expand Down
25 changes: 25 additions & 0 deletions Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,31 @@ def test_constructor_common(self):
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):
# Issue #21127: it should be possible to construct a PurePath object
# from a str subclass instance, and it then gets converted to
Expand Down

0 comments on commit 5742919

Please sign in to comment.