[Python-checkins] cpython (merge 3.2 -> default): #15180: Clarify posixpath.join() error message when mixing str & bytes

hynek.schlawack python-checkins at python.org
Sun Jul 15 16:47:59 CEST 2012


http://hg.python.org/cpython/rev/1462b963e5ce
changeset:   78119:1462b963e5ce
parent:      78117:27e13497d545
parent:      78118:cf1ac0c9e753
user:        Hynek Schlawack <hs at ox.cx>
date:        Sun Jul 15 16:46:23 2012 +0200
summary:
  #15180: Clarify posixpath.join() error message when mixing str & bytes

files:
  Lib/posixpath.py           |  20 ++++++++++++++------
  Lib/test/test_posixpath.py |  11 +++++++++--
  Misc/NEWS                  |   2 ++
  3 files changed, 25 insertions(+), 8 deletions(-)


diff --git a/Lib/posixpath.py b/Lib/posixpath.py
--- a/Lib/posixpath.py
+++ b/Lib/posixpath.py
@@ -74,13 +74,21 @@
     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.") from None
         else:
-            path += sep + b
+            raise
     return path
 
 
diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py
--- a/Lib/test/test_posixpath.py
+++ b/Lib/test/test_posixpath.py
@@ -56,8 +56,15 @@
         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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -38,6 +38,8 @@
 Library
 -------
 
+- Issue #15180: Clarify posixpath.join() error message when mixing str & bytes
+
 - Issue #15343: pkgutil now includes an iter_importer_modules implementation
   for importlib.machinery.FileFinder (similar to the way it already handled
   zipimport.zipimporter)

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list