diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 639ee7168c3..cbf81664882 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -3469,31 +3469,24 @@ _PyBytes_FromTuple(PyObject *x) } static PyObject * -_PyBytes_FromIterator(PyObject *x) +_PyBytes_FromIterator(PyObject *it, PyObject *x) { char *str; - PyObject *it; Py_ssize_t i, size; _PyBytesWriter writer; - _PyBytesWriter_Init(&writer); - /* For iterator version, create a string object and resize as needed */ size = PyObject_LengthHint(x, 64); if (size == -1 && PyErr_Occurred()) return NULL; + _PyBytesWriter_Init(&writer); str = _PyBytesWriter_Alloc(&writer, size); if (str == NULL) return NULL; writer.overallocate = 1; size = writer.allocated; - /* Get the iterator */ - it = PyObject_GetIter(x); - if (it == NULL) - goto error; - /* Run the iterator to exhaustion */ for (i = 0; ; i++) { PyObject *item; @@ -3529,19 +3522,19 @@ _PyBytes_FromIterator(PyObject *x) } *str++ = (char) value; } - Py_DECREF(it); return _PyBytesWriter_Finish(&writer, str); error: _PyBytesWriter_Dealloc(&writer); - Py_XDECREF(it); return NULL; } PyObject * PyBytes_FromObject(PyObject *x) { + PyObject *it, *result; + if (x == NULL) { PyErr_BadInternalCall(); return NULL; @@ -3562,13 +3555,19 @@ PyBytes_FromObject(PyObject *x) if (PyTuple_CheckExact(x)) return _PyBytes_FromTuple(x); - if (PyUnicode_Check(x)) { - PyErr_SetString(PyExc_TypeError, - "cannot convert unicode object to bytes"); - return NULL; + if (!PyUnicode_Check(x)) { + it = PyObject_GetIter(x); + if (it != NULL) { + result = _PyBytes_FromIterator(it, x); + Py_DECREF(it); + return result; + } } - return _PyBytes_FromIterator(x); + PyErr_Format(PyExc_TypeError, + "cannot convert '%.200s' object to bytes", + x->ob_type->tp_name); + return NULL; } static PyObject *