[issue41740] Improve error message for string concatenation via `sum`

Marco Paolini report at bugs.python.org
Mon Sep 7 18:32:09 EDT 2020


Marco Paolini <markopaolini at gmail.com> added the comment:

I was thinking to just clarify a bit the error message that results from Py_NumberAdd. This won't make it slower in the "hot" path

doing something like (not compile tested, sorry)

--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -2451,8 +2451,13 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
         Py_DECREF(result);
         Py_DECREF(item);
         result = temp;
-        if (result == NULL)
+        if (result == NULL) {
+         if (PyUnicode_Check(item) || PyBytes_Check(item) || PyByteArray_Check(item))
+             PyErr_SetString(PyExc_TypeError,
+                   "sum() can't sum bytes, strings or byte-arrays [use .join(seq) instead]");
+           }
             break;
+       }
     }
     Py_DECREF(iter);
     return result;

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41740>
_______________________________________


More information about the Python-bugs-list mailing list