https://github.com/python/cpython/commit/574291963f6b0eb7da3fde1ae9763236e7ece306
commit: 574291963f6b0eb7da3fde1ae9763236e7ece306
branch: main
author: Barney Gale <[email protected]>
committer: barneygale <[email protected]>
date: 2024-01-31T00:59:33Z
summary:
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.
files:
M Lib/pathlib/_abc.py
M Lib/test/test_pathlib/test_pathlib.py
M Lib/test/test_pathlib/test_pathlib_abc.py
diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py
index 85884bc4b4cb47..91f5cd6c01e9d0 100644
--- a/Lib/pathlib/_abc.py
+++ b/Lib/pathlib/_abc.py
@@ -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):
@@ -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]:
@@ -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]:
diff --git a/Lib/test/test_pathlib/test_pathlib.py
b/Lib/test/test_pathlib/test_pathlib.py
index 3bee9b8c762b80..2b166451243775 100644
--- a/Lib/test/test_pathlib/test_pathlib.py
+++ b/Lib/test/test_pathlib/test_pathlib.py
@@ -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'")
@@ -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)
diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py
b/Lib/test/test_pathlib/test_pathlib_abc.py
index 0e12182c162c14..207579ccbf443b 100644
--- a/Lib/test/test_pathlib/test_pathlib_abc.py
+++ b/Lib/test/test_pathlib/test_pathlib_abc.py
@@ -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
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]