diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-10-17 21:43:08 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-10-17 21:43:08 +0000 |
commit | 8a142f7dafd5a820190e61e7173de1fde8acee7a (patch) | |
tree | 27f2fa185cab35bd63e4c9b76c3381da29ca66de /numpy/core | |
parent | 32b16bbd43214d80362530a4c6dfd156dcab6b04 (diff) | |
download | numpy-8a142f7dafd5a820190e61e7173de1fde8acee7a.tar.gz |
Allow ability to reset the string function to the builtin string function.
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/src/arrayobject.c | 36 | ||||
-rw-r--r-- | numpy/core/src/multiarraymodule.c | 9 |
2 files changed, 28 insertions, 17 deletions
diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c index acac4810d..ededf70a0 100644 --- a/numpy/core/src/arrayobject.c +++ b/numpy/core/src/arrayobject.c @@ -4082,7 +4082,7 @@ dump_data(char **string, int *n, int *max_n, char *data, int nd, } static PyObject * -array_repr_builtin(PyArrayObject *self) +array_repr_builtin(PyArrayObject *self, int repr) { PyObject *ret; char *string; @@ -4095,27 +4095,35 @@ array_repr_builtin(PyArrayObject *self) return NULL; } - n = 6; - sprintf(string, "array("); - + 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 (PyArray_ISEXTENDED(self)) { - char buf[100]; - snprintf(buf, sizeof(buf), "%d", self->descr->elsize); - sprintf(string+n, ", '%c%s')", self->descr->type, buf); - ret = PyString_FromStringAndSize(string, n+6+strlen(buf)); + if (repr) { + if (PyArray_ISEXTENDED(self)) { + char buf[100]; + snprintf(buf, sizeof(buf), "%d", self->descr->elsize); + sprintf(string+n, ", '%c%s')", self->descr->type, buf); + ret = PyString_FromStringAndSize(string, n+6+strlen(buf)); + } + else { + sprintf(string+n, ", '%c')", self->descr->type); + ret = PyString_FromStringAndSize(string, n+6); + } } else { - sprintf(string+n, ", '%c')", self->descr->type); - ret = PyString_FromStringAndSize(string, n+6); + ret = PyString_FromStringAndSize(string, n); } - _pya_free(string); return ret; } @@ -4152,7 +4160,7 @@ array_repr(PyArrayObject *self) PyObject *s, *arglist; if (PyArray_ReprFunction == NULL) { - s = array_repr_builtin(self); + s = array_repr_builtin(self, 1); } else { arglist = Py_BuildValue("(O)", self); s = PyEval_CallObject(PyArray_ReprFunction, arglist); @@ -4167,7 +4175,7 @@ array_str(PyArrayObject *self) PyObject *s, *arglist; if (PyArray_StrFunction == NULL) { - s = array_repr(self); + s = array_repr_builtin(self, 0); } else { arglist = Py_BuildValue("(O)", self); s = PyEval_CallObject(PyArray_StrFunction, arglist); diff --git a/numpy/core/src/multiarraymodule.c b/numpy/core/src/multiarraymodule.c index 1e4af7a72..eba18e3d3 100644 --- a/numpy/core/src/multiarraymodule.c +++ b/numpy/core/src/multiarraymodule.c @@ -6411,13 +6411,16 @@ array__reconstruct(PyObject *dummy, PyObject *args) static PyObject * array_set_string_function(PyObject *dummy, PyObject *args, PyObject *kwds) { - PyObject *op; + PyObject *op=NULL; int repr=1; static char *kwlist[] = {"f", "repr", NULL}; - if(!PyArg_ParseTupleAndKeywords(args, kwds, "O|i", kwlist, + if(!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi", kwlist, &op, &repr)) return NULL; - if (!PyCallable_Check(op)) { + + /* reset the array_repr function to built-in */ + if (op == Py_None) op = NULL; + if (op != NULL && !PyCallable_Check(op)) { PyErr_SetString(PyExc_TypeError, "Argument must be callable."); return NULL; |