Mercurial > cpython-lru_cache-weakref
comparison Modules/arraymodule.c @ 28742:bd678e65f784 legacy-trunk
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.
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Wed, 23 Apr 2003 17:27:00 +0000 |
| parents | fd5ef7003469 |
| children | 22d177682ae1 |
comparison
equal
deleted
inserted
replaced
| 28741:66cceb7562b5 | 28742:bd678e65f784 |
|---|---|
| 1454 | 1454 |
| 1455 static PyObject * | 1455 static PyObject * |
| 1456 array_repr(arrayobject *a) | 1456 array_repr(arrayobject *a) |
| 1457 { | 1457 { |
| 1458 char buf[256], typecode; | 1458 char buf[256], typecode; |
| 1459 PyObject *s, *t, *comma, *v; | 1459 PyObject *s, *t, *v = NULL; |
| 1460 int i, len; | 1460 int len; |
| 1461 | 1461 |
| 1462 len = a->ob_size; | 1462 len = a->ob_size; |
| 1463 typecode = a->ob_descr->typecode; | 1463 typecode = a->ob_descr->typecode; |
| 1464 if (len == 0) { | 1464 if (len == 0) { |
| 1465 PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode); | 1465 PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode); |
| 1466 return PyString_FromString(buf); | 1466 return PyString_FromString(buf); |
| 1467 } | 1467 } |
| 1468 | 1468 |
| 1469 if (typecode == 'c' || typecode == 'u') { | 1469 if (typecode == 'c') |
| 1470 PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode); | 1470 v = array_tostring(a, NULL); |
| 1471 s = PyString_FromString(buf); | |
| 1472 #ifdef Py_USING_UNICODE | 1471 #ifdef Py_USING_UNICODE |
| 1473 if (typecode == 'c') | 1472 else if (typecode == 'u') |
| 1473 v = array_tounicode(a, NULL); | |
| 1474 #endif | 1474 #endif |
| 1475 v = array_tostring(a, NULL); | 1475 else |
| 1476 #ifdef Py_USING_UNICODE | 1476 v = array_tolist(a, NULL); |
| 1477 else | 1477 t = PyObject_Repr(v); |
| 1478 v = array_tounicode(a, NULL); | 1478 Py_XDECREF(v); |
| 1479 #endif | 1479 |
| 1480 t = PyObject_Repr(v); | 1480 PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode); |
| 1481 Py_XDECREF(v); | |
| 1482 PyString_ConcatAndDel(&s, t); | |
| 1483 PyString_ConcatAndDel(&s, PyString_FromString(")")); | |
| 1484 return s; | |
| 1485 } | |
| 1486 PyOS_snprintf(buf, sizeof(buf), "array('%c', [", typecode); | |
| 1487 s = PyString_FromString(buf); | 1481 s = PyString_FromString(buf); |
| 1488 comma = PyString_FromString(", "); | 1482 PyString_ConcatAndDel(&s, t); |
| 1489 for (i = 0; i < len && !PyErr_Occurred(); i++) { | 1483 PyString_ConcatAndDel(&s, PyString_FromString(")")); |
| 1490 if (i > 0) | |
| 1491 PyString_Concat(&s, comma); | |
| 1492 v = (a->ob_descr->getitem)(a, i); | |
| 1493 t = PyObject_Repr(v); | |
| 1494 Py_XDECREF(v); | |
| 1495 PyString_ConcatAndDel(&s, t); | |
| 1496 } | |
| 1497 Py_XDECREF(comma); | |
| 1498 PyString_ConcatAndDel(&s, PyString_FromString("])")); | |
| 1499 return s; | 1484 return s; |
| 1500 } | 1485 } |
| 1501 | 1486 |
| 1502 static PyObject* | 1487 static PyObject* |
| 1503 array_subscr(arrayobject* self, PyObject* item) | 1488 array_subscr(arrayobject* self, PyObject* item) |
