Commit 88ba1e39 authored by Raymond Hettinger's avatar Raymond Hettinger

SF Patch 685051: fix for 680789: reprs in arraymodule

(contributed by logistix; substantially reworked by rhettinger).

To create a representation of non-string arrays, array_repr() was
starting with a base Python string object and repeatedly using +=
to concatenate the representation of individual objects.

Logistix had the idea to convert to an intermediate tuple form and
then join it all at once.  I took advantage of existing tools and
formed a list with array_tolist() and got its representation through
PyObject_Repr(v) which already has a fast implementation for lists.
parent 444e4340
...@@ -1456,8 +1456,8 @@ static PyObject * ...@@ -1456,8 +1456,8 @@ static PyObject *
array_repr(arrayobject *a) array_repr(arrayobject *a)
{ {
char buf[256], typecode; char buf[256], typecode;
PyObject *s, *t, *comma, *v; PyObject *s, *t, *v = NULL;
int i, len; int len;
len = a->ob_size; len = a->ob_size;
typecode = a->ob_descr->typecode; typecode = a->ob_descr->typecode;
...@@ -1465,37 +1465,22 @@ array_repr(arrayobject *a) ...@@ -1465,37 +1465,22 @@ array_repr(arrayobject *a)
PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode); PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode);
return PyString_FromString(buf); return PyString_FromString(buf);
} }
if (typecode == 'c' || typecode == 'u') { if (typecode == 'c')
PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode); v = array_tostring(a, NULL);
s = PyString_FromString(buf);
#ifdef Py_USING_UNICODE
if (typecode == 'c')
#endif
v = array_tostring(a, NULL);
#ifdef Py_USING_UNICODE #ifdef Py_USING_UNICODE
else else if (typecode == 'u')
v = array_tounicode(a, NULL); v = array_tounicode(a, NULL);
#endif #endif
t = PyObject_Repr(v); else
Py_XDECREF(v); v = array_tolist(a, NULL);
PyString_ConcatAndDel(&s, t); t = PyObject_Repr(v);
PyString_ConcatAndDel(&s, PyString_FromString(")")); Py_XDECREF(v);
return s;
} PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode);
PyOS_snprintf(buf, sizeof(buf), "array('%c', [", typecode);
s = PyString_FromString(buf); s = PyString_FromString(buf);
comma = PyString_FromString(", "); PyString_ConcatAndDel(&s, t);
for (i = 0; i < len && !PyErr_Occurred(); i++) { PyString_ConcatAndDel(&s, PyString_FromString(")"));
if (i > 0)
PyString_Concat(&s, comma);
v = (a->ob_descr->getitem)(a, i);
t = PyObject_Repr(v);
Py_XDECREF(v);
PyString_ConcatAndDel(&s, t);
}
Py_XDECREF(comma);
PyString_ConcatAndDel(&s, PyString_FromString("])"));
return s; return s;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment