diff -urp Python-2.7.5.orig/Python/bltinmodule.c Python-2.7.5/Python/bltinmodule.c --- Python-2.7.5.orig/Python/bltinmodule.c 2013-05-12 06:32:53.000000000 +0300 +++ Python-2.7.5/Python/bltinmodule.c 2013-07-04 10:38:53.000000000 +0300 @@ -2399,6 +2392,65 @@ builtin_sum(PyObject *self, PyObject *ar } } } + + if (PyList_CheckExact(result) || PyTuple_CheckExact(result)) { + PyTypeObject *optimized_for = Py_TYPE(result); + PyObject *l_result = PyList_New(0); + if (l_result == NULL) { + Py_DECREF(result); + Py_DECREF(iter); + return NULL; + } + temp = _PyList_Extend((PyListObject*)l_result, result); + Py_DECREF(result); + result = NULL; + if (temp == NULL) { + Py_DECREF(l_result); + Py_DECREF(iter); + return NULL; + } + Py_DECREF(temp); + for (;;) { + item = PyIter_Next(iter); + if (item == NULL) { + if (PyErr_Occurred()) { + Py_DECREF(l_result); + return NULL; + } + break; + } + if (Py_TYPE(item) != optimized_for) + break; + temp = _PyList_Extend((PyListObject*)l_result, item); + if (temp == NULL) + break; + Py_DECREF(item); + Py_DECREF(temp); + } + /* Different class, could not extend, or end of sequence. Restore real objects ... */ + if (optimized_for == &PyList_Type) + result = l_result; + else /* optimized_for == &PyTuple_Type */ { + result = PyList_AsTuple(l_result); + Py_DECREF(l_result); + if (result == NULL) { + Py_DECREF(item); + Py_DECREF(iter); + return NULL; + } + } + /* ... and process normally */ + if (item != NULL) { + temp = PyNumber_Add(result, item); + Py_DECREF(result); + Py_DECREF(item); + result = temp; + if (result == NULL) { + Py_DECREF(iter); + return NULL; + } + } + } #endif for(;;) {