summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTravis Oliphant <oliphant@enthought.com>2006-01-25 08:19:26 +0000
committerTravis Oliphant <oliphant@enthought.com>2006-01-25 08:19:26 +0000
commit14cc42d233c139c1146398e880e38dfe246b12fb (patch)
treeff13ad3bd7032f8ce35e85a0c7e3a2c20698768f
parent6a355bbee8dbfdbc33f4254a4bcdc35ec1c401f7 (diff)
downloadnumpy-14cc42d233c139c1146398e880e38dfe246b12fb.tar.gz
Fix so that string and unicode types don't print NULL bytes
-rw-r--r--numpy/core/src/arrayobject.c2
-rw-r--r--numpy/core/src/arraytypes.inc.src19
-rw-r--r--numpy/core/src/scalartypes.inc.src41
3 files changed, 53 insertions, 9 deletions
diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c
index f79e78aa4..0257fe476 100644
--- a/numpy/core/src/arrayobject.c
+++ b/numpy/core/src/arrayobject.c
@@ -4391,7 +4391,7 @@ array_descr_set(PyArrayObject *self, PyObject *arg)
if (!(PyArray_DescrConverter(arg, &newtype)) ||
newtype == NULL) {
- PyErr_SetString(PyExc_TypeError, "invalid type for array");
+ PyErr_SetString(PyExc_TypeError, "invalid data-type for array");
return -1;
}
if (newtype->type_num == PyArray_OBJECT || \
diff --git a/numpy/core/src/arraytypes.inc.src b/numpy/core/src/arraytypes.inc.src
index a973a9dac..4fdb2f99b 100644
--- a/numpy/core/src/arraytypes.inc.src
+++ b/numpy/core/src/arraytypes.inc.src
@@ -35,12 +35,12 @@ _getNAN(void) {
#ifdef NAN
return NAN;
#else
- double mul = 1e100;
- double tmp = 0.0;
- double pinf=0;
static double nan=0;
if (nan == 0) {
+ double mul = 1e100;
+ double tmp = 0.0;
+ double pinf=0;
pinf = mul;
for (;;) {
pinf *= mul;
@@ -225,13 +225,16 @@ UNICODE_getitem(char *ip, PyArrayObject *ap)
{
PyObject *obj;
size_t size = sizeof(Py_UNICODE);
+ int mysize;
+ Py_UNICODE *dptr;
-
- obj = PyUnicode_FromUnicode((const Py_UNICODE *)ip,
- ap->descr->elsize / size);
+ mysize = ap->descr->elsize / size;
+ dptr = (Py_UNICODE *)ip + mysize-1;
+ while(mysize > 0 && *dptr-- == 0) mysize--;
+ obj = PyUnicode_FromUnicode((const Py_UNICODE *)ip, mysize);
if (!PyArray_ISNOTSWAPPED(ap) && (obj != NULL)) {
byte_swap_vector(PyUnicode_AS_UNICODE(obj),
- ap->descr->elsize / size, size);
+ mysize, size);
}
return obj;
}
@@ -1861,7 +1864,7 @@ PyArray_DescrFromType(int type)
}
if (ret==NULL) {
PyErr_SetString(PyExc_ValueError,
- "Invalid type for array");
+ "Invalid data-type for array");
}
else Py_INCREF(ret);
return ret;
diff --git a/numpy/core/src/scalartypes.inc.src b/numpy/core/src/scalartypes.inc.src
index 2e1e7f1d1..7a39d0ad1 100644
--- a/numpy/core/src/scalartypes.inc.src
+++ b/numpy/core/src/scalartypes.inc.src
@@ -530,6 +530,41 @@ format_longdouble(char *buf, size_t buflen, longdouble val, int precision)
}
}
+/* over-ride repr and str of array-scalar strings and unicode to
+ remove NULL bytes and then call the corresponding functions
+ of string and unicode.
+ */
+
+/**begin repeat
+#name=string*2,unicode*2#
+#form=(repr,str)*2#
+#Name=String*2,Unicode*2#
+#NAME=STRING*2,UNICODE*2#
+#extra=AndSize*2,,#
+#type=char*2, Py_UNICODE*2#
+*/
+static PyObject *
+@name@type_@form@(PyObject *self)
+{
+ const @type@ *dptr, *ip;
+ int len;
+ PyObject *new;
+ PyObject *ret;
+
+ ip = dptr = Py@Name@_AS_@NAME@(self);
+ len = Py@Name@_GET_SIZE(self);
+ dptr += len-1;
+ while(len > 0 && *dptr-- == 0) len--;
+ new = Py@Name@_From@Name@@extra@(ip, len);
+ if (new == NULL) return PyString_FromString("");
+ ret = Py@Name@_Type.tp_@form@(new);
+ Py_DECREF(new);
+ return ret;
+}
+/**end repeat**/
+
+
+
#if SIZEOF_LONGDOUBLE == SIZEOF_DOUBLE
#define PREC_REPR 15
#define PREC_STR 15
@@ -2133,6 +2168,12 @@ initialize_numeric_types(void)
PyStringArrType_Type.tp_alloc = NULL;
PyStringArrType_Type.tp_free = NULL;
+
+ PyStringArrType_Type.tp_repr = stringtype_repr;
+ PyStringArrType_Type.tp_str = stringtype_str;
+
+ PyUnicodeArrType_Type.tp_repr = unicodetype_repr;
+ PyUnicodeArrType_Type.tp_str = unicodetype_str;
PyVoidArrType_Type.tp_methods = voidtype_methods;
PyVoidArrType_Type.tp_getset = voidtype_getsets;