will be discarded."""
sep = _get_sep(a)
path = a
- for b in p:
- if b.startswith(sep):
- path = b
- elif not path or path.endswith(sep):
- path += b
+ try:
+ for b in p:
+ if b.startswith(sep):
+ path = b
+ elif not path or path.endswith(sep):
+ path += b
+ else:
+ path += sep + b
+ except TypeError:
+ strs = [isinstance(s, str) for s in (a, ) + p]
+ if any(strs) and not all(strs):
+ raise TypeError("Can't mix strings and bytes in path components.")
else:
- path += sep + b
+ raise
return path
self.assertEqual(posixpath.join(b"/foo/", b"bar/", b"baz/"),
b"/foo/bar/baz/")
- self.assertRaises(TypeError, posixpath.join, b"bytes", "str")
- self.assertRaises(TypeError, posixpath.join, "str", b"bytes")
+ with self.assertRaises(TypeError) as e:
+ posixpath.join(b'bytes', 'str')
+ self.assertIn("Can't mix strings and bytes", e.args[0])
+ with self.assertRaises(TypeError) as e:
+ posixpath.join('str', b'bytes')
+ self.assertIn("Can't mix strings and bytes", e.args[0])
+ with self.assertRaises(TypeError) as e:
+ posixpath.join('str', bytearray(b'bytes'))
+ self.assertIn("Can't mix strings and bytes", e.args[0])
def test_split(self):
self.assertEqual(posixpath.split("/foo/bar"), ("/foo", "bar"))