static PyObject * array_repr_builtin(PyArrayObject *self, int repr) { PyObject *ret; char *string; int n, max_n; max_n = PyArray_NBYTES(self)*4*sizeof(char) + 7; if ((string = (char *)_pya_malloc(max_n)) == NULL) { PyErr_SetString(PyExc_MemoryError, "out of memory"); return NULL; } if (repr) { n = 6; sprintf(string, "array("); } else { n = 0; } if (dump_data(&string, &n, &max_n, self->data, self->nd, self->dimensions, self->strides, self) < 0) { _pya_free(string); return NULL; } if (repr) { if (PyArray_ISEXTENDED(self)) { char buf[100]; PyOS_snprintf(buf, sizeof(buf), "%d", self->descr->elsize); sprintf(string+n, ", '%c%s')", self->descr->type, buf); ret = PyUString_FromStringAndSize(string, n + 6 + strlen(buf)); } else { sprintf(string+n, ", '%c')", self->descr->type); ret = PyUString_FromStringAndSize(string, n+6); } } else { ret = PyUString_FromStringAndSize(string, n); } _pya_free(string); return ret; }
static PyObject * array_repr_builtin(PyArrayObject *self, int repr) { PyObject *ret; char *string; /* max_n initial value is arbitrary, dump_data will extend it */ Py_ssize_t n = 0, max_n = PyArray_NBYTES(self) * 4 + 7; if ((string = PyArray_malloc(max_n)) == NULL) { return PyErr_NoMemory(); } if (dump_data(&string, &n, &max_n, PyArray_DATA(self), PyArray_NDIM(self), PyArray_DIMS(self), PyArray_STRIDES(self), self) < 0) { PyArray_free(string); return NULL; } if (repr) { if (PyArray_ISEXTENDED(self)) { ret = PyUString_FromFormat("array(%s, '%c%d')", string, PyArray_DESCR(self)->type, PyArray_DESCR(self)->elsize); } else { ret = PyUString_FromFormat("array(%s, '%c')", string, PyArray_DESCR(self)->type); } } else { ret = PyUString_FromStringAndSize(string, n); } PyArray_free(string); return ret; }