[Numpy-svn] r3352 - trunk/numpy/core/src

numpy-svn at scipy.org numpy-svn at scipy.org
Tue Oct 17 17:43:16 EDT 2006


Author: oliphant
Date: 2006-10-17 16:43:08 -0500 (Tue, 17 Oct 2006)
New Revision: 3352

Modified:
   trunk/numpy/core/src/arrayobject.c
   trunk/numpy/core/src/multiarraymodule.c
Log:
Allow ability to reset the string function to the builtin string function.

Modified: trunk/numpy/core/src/arrayobject.c
===================================================================
--- trunk/numpy/core/src/arrayobject.c	2006-10-17 17:19:29 UTC (rev 3351)
+++ trunk/numpy/core/src/arrayobject.c	2006-10-17 21:43:08 UTC (rev 3352)
@@ -4082,7 +4082,7 @@
 }
 
 static PyObject *
-array_repr_builtin(PyArrayObject *self)
+array_repr_builtin(PyArrayObject *self, int repr)
 {
         PyObject *ret;
         char *string;
@@ -4095,27 +4095,35 @@
                 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 @@
         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 @@
         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);

Modified: trunk/numpy/core/src/multiarraymodule.c
===================================================================
--- trunk/numpy/core/src/multiarraymodule.c	2006-10-17 17:19:29 UTC (rev 3351)
+++ trunk/numpy/core/src/multiarraymodule.c	2006-10-17 21:43:08 UTC (rev 3352)
@@ -6411,13 +6411,16 @@
 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;




More information about the Numpy-svn mailing list