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):
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]:
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]:
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'")
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)
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