summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHynek Schlawack <hs@ox.cx>2012-07-15 14:21:30 (GMT)
committerHynek Schlawack <hs@ox.cx>2012-07-15 14:21:30 (GMT)
commit4774946c3b7564b4a1c93da4ba4dba443a36a708 (patch)
tree2c668f86fdb8f658f7eb73bf04f46139d92fb627
parenta3d1cac4b21ffbd8deed0c28f08bed4afa5e7b83 (diff)
downloadcpython-4774946c3b7564b4a1c93da4ba4dba443a36a708.zip
cpython-4774946c3b7564b4a1c93da4ba4dba443a36a708.tar.gz
cpython-4774946c3b7564b4a1c93da4ba4dba443a36a708.tar.bz2
#15180: Clarify posixpath.join() error message when mixing str & bytes
-rw-r--r--Lib/posixpath.py19
-rw-r--r--Lib/test/test_posixpath.py11
-rw-r--r--Misc/NEWS2
3 files changed, 24 insertions, 8 deletions
diff --git a/Lib/posixpath.py b/Lib/posixpath.py
index 9570a36..84bcc13 100644
--- a/Lib/posixpath.py
+++ b/Lib/posixpath.py
@@ -74,13 +74,20 @@ def join(a, *p):
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
diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py
index a7a3e4a..54de0cf 100644
--- a/Lib/test/test_posixpath.py
+++ b/Lib/test/test_posixpath.py
@@ -56,8 +56,15 @@ class PosixPathTest(unittest.TestCase):
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"))
diff --git a/Misc/NEWS b/Misc/NEWS
index 81665e3..74e4038 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -87,6 +87,8 @@ Core and Builtins
Library
-------
+- Issue #15180: Clarify posixpath.join() error message when mixing str & bytes
+
- Issue #15230: runpy.run_path now correctly sets __package__ as described
in the documentation