]> granicus.if.org Git - python/commitdiff
Patch # 1145 by Thomas Lee:
authorGuido van Rossum <guido@python.org>
Thu, 27 Sep 2007 18:01:22 +0000 (18:01 +0000)
committerGuido van Rossum <guido@python.org>
Thu, 27 Sep 2007 18:01:22 +0000 (18:01 +0000)
str.join(...) now applies str() to the sequence elements if they're
not strings alraedy, except for bytes, which still raise TypeError
(for the same reasons why ""==b"" raises it).

Doc/library/stdtypes.rst
Lib/test/string_tests.py
Lib/test/test_descr.py
Lib/test/test_unicode.py
Objects/unicodeobject.c

index 1a0bdf3ba7850548212fd083ffd4970dc473114f..136a8d5f7e274969d6ccefe33c354d6be47d6dd9 100644 (file)
@@ -789,8 +789,11 @@ functions based on regular expressions.
 
 .. method:: str.join(seq)
 
-   Return a string which is the concatenation of the strings in the sequence *seq*.
-   The separator between elements is the string providing this method.
+   Return a string which is the concatenation of the values in the sequence
+   *seq*. Non-string values in *seq* will be converted to a string using their
+   respective ``str()`` value. If there are any :class:`bytes` objects in
+   *seq*, a :exc:`TypeError` will be raised. The separator between elements is
+   the string providing this method.
 
 
 .. method:: str.ljust(width[, fillchar])
index 9e178caf7b530ba3e7b6eca97ecd833b0a0b8a35..cb8900d03f3f17ecd12c19758d453b85ae530fd1 100644 (file)
@@ -13,6 +13,7 @@ class Sequence:
 
 class BadSeq1(Sequence):
     def __init__(self): self.seq = [7, 'hello', 123]
+    def __str__(self): return '{0} {1} {2}'.format(*self.seq)
 
 class BadSeq2(Sequence):
     def __init__(self): self.seq = ['a', 'b', 'c']
@@ -987,19 +988,19 @@ class MixinStrUnicodeUserStringTest:
         self.checkequal('abc', 'a', 'join', ('abc',))
         self.checkequal('z', 'a', 'join', UserList(['z']))
         self.checkequal('a.b.c', '.', 'join', ['a', 'b', 'c'])
-        self.checkraises(TypeError, '.', 'join', ['a', 'b', 3])
+        self.checkequal('a.b.3', '.', 'join', ['a', 'b', 3])
         for i in [5, 25, 125]:
             self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
                  ['a' * i] * i)
             self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
                  ('a' * i,) * i)
 
-        self.checkraises(TypeError, ' ', 'join', BadSeq1())
+        self.checkequal(str(BadSeq1()), ' ', 'join', BadSeq1())
         self.checkequal('a b c', ' ', 'join', BadSeq2())
 
         self.checkraises(TypeError, ' ', 'join')
         self.checkraises(TypeError, ' ', 'join', 7)
-        self.checkraises(TypeError, ' ', 'join', Sequence([7, 'hello', 123]))
+        self.checkraises(TypeError, ' ', 'join', [1, 2, bytes()])
         try:
             def f():
                 yield 4 + ""
index 12bec1a3042352dd82bcc2a8b75be7dc62b20c54..67ae239b6899498e57901ef79251f6c7c1f33088 100644 (file)
@@ -3238,10 +3238,6 @@ def strops():
     except ValueError: pass
     else: raise TestFailed("''.split('') doesn't raise ValueError")
 
-    try: ''.join([0])
-    except TypeError: pass
-    else: raise TestFailed("''.join([0]) doesn't raise TypeError")
-
     try: ''.rindex('5')
     except ValueError: pass
     else: raise TestFailed("''.rindex('5') doesn't raise ValueError")
index 64cca3f18fd0a187f197510bfd669888405dc614..1358419407fa3c4f68d19811b9de8577ae1653cf 100644 (file)
@@ -178,6 +178,10 @@ class UnicodeTest(
     def test_join(self):
         string_tests.MixinStrUnicodeUserStringTest.test_join(self)
 
+        class MyWrapper:
+            def __init__(self, sval): self.sval = sval
+            def __str__(self): return self.sval
+
         # mixed arguments
         self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
         self.checkequalnofix('abcd', '', 'join', ('a', 'b', 'c', 'd'))
@@ -186,6 +190,8 @@ class UnicodeTest(
         self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
         self.checkequalnofix('abcd', '', 'join', ('a', 'b', 'c', 'd'))
         self.checkequalnofix('w x y z', ' ', 'join', string_tests.Sequence('wxyz'))
+        self.checkequalnofix('1 2 foo', ' ', 'join', [1, 2, MyWrapper('foo')])
+        self.checkraises(TypeError, ' ', 'join', [1, 2, 3, bytes()])
 
     def test_replace(self):
         string_tests.CommonTest.test_replace(self)
index c40f0bec4dcdca318f32f4359829db929c664437..ad784924190a08eaf5dc784e4b28d175946340e0 100644 (file)
@@ -5412,14 +5412,20 @@ PyUnicode_Join(PyObject *separator, PyObject *seq)
 
        item = PySequence_Fast_GET_ITEM(fseq, i);
        /* Convert item to Unicode. */
-       if (! PyUnicode_Check(item) && ! PyString_Check(item)) {
-           PyErr_Format(PyExc_TypeError,
-                        "sequence item %zd: expected string or Unicode,"
-                        " %.80s found",
-                        i, Py_Type(item)->tp_name);
-           goto onError;
+       if (!PyString_Check(item) && !PyUnicode_Check(item))
+       {
+               if (PyBytes_Check(item))
+               {
+                       PyErr_Format(PyExc_TypeError,
+                            "sequence item %d: join() will not operate on "
+                            "bytes objects", i);
+                       goto onError;
+               }
+               item = PyObject_Unicode(item);
        }
-       item = PyUnicode_FromObject(item);
+       else
+               item = PyUnicode_FromObject(item);
+
        if (item == NULL)
            goto onError;
        /* We own a reference to item from here on. */