]> granicus.if.org Git - python/commitdiff
Fix Unicode .join() method to raise a TypeError for sequence
authorMarc-André Lemburg <mal@egenix.com>
Thu, 20 Sep 2001 17:22:58 +0000 (17:22 +0000)
committerMarc-André Lemburg <mal@egenix.com>
Thu, 20 Sep 2001 17:22:58 +0000 (17:22 +0000)
elements which are not Unicode objects or strings. (This matches
the string.join() behaviour.)

Fix a memory leak in the .join() method which occurs in case
the Unicode resize fails.

Restore the test_unicode output.

Lib/test/output/test_unicode
Objects/unicodeobject.c

index 11c9c80d198e8d80ac4a164994fcabe446e499f4..783a4860ab4353a4d5e6b0ca7cd62c2f76d0280a 100644 (file)
@@ -1,5 +1,4 @@
 test_unicode
-* <built-in method join of unicode object at 0x81b3ba0> u' ' <class exceptions.TypeError at 0x80fac4c> u'7 hello 123'
 Testing Unicode comparisons... done.
 Testing Unicode contains method... done.
 Testing Unicode formatting strings... done.
index 896e80f794993621c085dee594548ab7b653907d..c8c07a613b1ce28bc0f9dcaccfd7ece3f0f43cf3 100644 (file)
@@ -3197,6 +3197,14 @@ PyObject *PyUnicode_Join(PyObject *separator,
        }
        if (!PyUnicode_Check(item)) {
            PyObject *v;
+           if (!PyString_Check(item)) {
+               PyErr_Format(PyExc_TypeError,
+                            "sequence item %i: expected string or Unicode,"
+                            " %.80s found",
+                            i, item->ob_type->tp_name);
+               Py_DECREF(item);
+               goto onError;
+           }
            v = PyUnicode_FromObject(item);
            Py_DECREF(item);
            item = v;
@@ -3205,8 +3213,10 @@ PyObject *PyUnicode_Join(PyObject *separator,
        }
        itemlen = PyUnicode_GET_SIZE(item);
        while (reslen + itemlen + seplen >= sz) {
-           if (_PyUnicode_Resize(&res, sz*2))
+           if (_PyUnicode_Resize(&res, sz*2)) {
+               Py_DECREF(item);
                goto onError;
+           }
            sz *= 2;
            p = PyUnicode_AS_UNICODE(res) + reslen;
        }