diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-01-30 07:49:07 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-01-30 07:49:07 +0000 |
commit | f097ed4fa79aca45adf867a3f01bf677679559d7 (patch) | |
tree | e0e45a4e22ca3b4ed080499c25ab718285511f2b | |
parent | 0245ddea0b3a7ccf8dfc1a8366cbacb2232cefb8 (diff) | |
download | numpy-f097ed4fa79aca45adf867a3f01bf677679559d7.tar.gz |
Fix bugs relating to adding hasobject. Add scalar-loops for some operations to see if they are faster on some platforms.
-rw-r--r-- | numpy/core/src/arrayobject.c | 43 | ||||
-rw-r--r-- | numpy/core/src/multiarraymodule.c | 15 | ||||
-rw-r--r-- | numpy/core/src/umathmodule.c.src | 20 |
3 files changed, 53 insertions, 25 deletions
diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c index 049049664..fef9ddf1a 100644 --- a/numpy/core/src/arrayobject.c +++ b/numpy/core/src/arrayobject.c @@ -5265,6 +5265,12 @@ Array_FromScalar(PyObject *op, PyArray_Descr *typecode) NULL, NULL, 0, NULL); if (ret == NULL) return NULL; + if (ret->nd > 0) { + PyErr_SetString(PyExc_ValueError, + "shape-mismatch on array construction"); + Py_DECREF(ret); + return NULL; + } ret->descr->f->setitem(op, ret->data, ret); @@ -5277,7 +5283,7 @@ Array_FromScalar(PyObject *op, PyArray_Descr *typecode) } -/* steals reference to typecode unless return value is NULL*/ +/* steals reference to typecode */ static PyObject * Array_FromSequence(PyObject *s, PyArray_Descr *typecode, int fortran, int min_depth, int max_depth) @@ -5289,7 +5295,6 @@ Array_FromSequence(PyObject *s, PyArray_Descr *typecode, int fortran, int stop_at_tuple; int type = typecode->type_num; int itemsize = typecode->elsize; - PyArray_Descr *savetype=typecode; stop_at_string = ((type == PyArray_OBJECT) || \ (type == PyArray_STRING) || \ @@ -5306,23 +5311,20 @@ Array_FromSequence(PyObject *s, PyArray_Descr *typecode, int fortran, return Array_FromScalar(s, typecode); PyErr_SetString(PyExc_ValueError, "invalid input sequence"); - return NULL; + goto fail; } if ((max_depth && nd > max_depth) || \ (min_depth && nd < min_depth)) { PyErr_SetString(PyExc_ValueError, "invalid number of dimensions"); - return NULL; + goto fail; } - if(discover_dimensions(s,nd,d, !stop_at_string) == -1) { - return NULL; - } + if(discover_dimensions(s,nd,d, !stop_at_string) == -1) goto fail; + if (itemsize == 0 && PyTypeNum_ISEXTENDED(type)) { - if (discover_itemsize(s, nd, &itemsize) == -1) { - return NULL; - } + if (discover_itemsize(s, nd, &itemsize) == -1) goto fail; if (type == PyArray_UNICODE) itemsize*=sizeof(Py_UNICODE); } @@ -5336,13 +5338,16 @@ Array_FromSequence(PyObject *s, PyArray_Descr *typecode, int fortran, NULL, NULL, fortran, NULL); - if(!r) {Py_XINCREF(savetype); return NULL;} + if(!r) return NULL; if(Assign_Array(r,s) == -1) { - Py_XINCREF(savetype); Py_DECREF(r); return NULL; } return (PyObject*)r; + + fail: + Py_DECREF(typecode); + return NULL; } @@ -6092,15 +6097,19 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth, } if (PySequence_Check(op)) { /* necessary but not sufficient */ - + + Py_INCREF(newtype); r = Array_FromSequence(op, newtype, flags & FORTRAN, min_depth, max_depth); - if (PyErr_Occurred() && r == NULL) + if (PyErr_Occurred() && r == NULL) { /* It wasn't really a sequence after all. * Try interpreting it as a scalar */ - PyErr_Clear(); - else - seq = TRUE; + PyErr_Clear(); + } + else { + seq = TRUE; + Py_DECREF(newtype); + } } if (!seq) r = Array_FromScalar(op, newtype); diff --git a/numpy/core/src/multiarraymodule.c b/numpy/core/src/multiarraymodule.c index dd588d4ae..1ba38eadf 100644 --- a/numpy/core/src/multiarraymodule.c +++ b/numpy/core/src/multiarraymodule.c @@ -3393,6 +3393,7 @@ _convert_from_tuple(PyObject *obj) PyDimMem_FREE(shape.ptr); newdescr->subarray = _pya_malloc(sizeof(PyArray_ArrayDescr)); newdescr->subarray->base = type; + if (type->hasobject) newdescr->hasobject = 1; Py_INCREF(val); newdescr->subarray->shape = val; Py_XDECREF(newdescr->fields); @@ -3517,8 +3518,13 @@ _convert_from_list(PyObject *obj, int align, int try_descr) tup = PyTuple_New(2); key = PyString_FromFormat("f%d", i+1); ret = PyArray_DescrConverter(PyList_GET_ITEM(obj, i), &conv); - if (!hasobject && conv->hasobject) - hasobject=1; + if (ret == PY_FAIL) { + Py_DECREF(tup); + Py_DECREF(key); + goto fail; + } + if (!hasobject && conv->hasobject) + hasobject=1; PyTuple_SET_ITEM(tup, 0, (PyObject *)conv); if (align) { int _align; @@ -3531,7 +3537,6 @@ _convert_from_list(PyObject *obj, int align, int try_descr) PyDict_SetItem(fields, key, tup); Py_DECREF(tup); PyList_SET_ITEM(nameslist, i, key); - if (ret == PY_FAIL) goto fail; totalsize += conv->elsize; } key = PyInt_FromLong(-1); @@ -3686,8 +3691,6 @@ _convert_from_dict(PyObject *obj, int align) tup = PyTuple_New(len); descr = PyObject_GetItem(descrs, index); ret = PyArray_DescrConverter(descr, &newdescr); - if (!hasobject && newdescr->hasobject) - hasobject = 1; Py_DECREF(descr); PyTuple_SET_ITEM(tup, 0, (PyObject *)newdescr); if (offsets) { @@ -3727,6 +3730,8 @@ _convert_from_dict(PyObject *obj, int align) if (len == 3) PyDict_SetItem(fields, item, tup); Py_DECREF(tup); if ((ret == PY_FAIL) || (newdescr->elsize == 0)) goto fail; + if (!hasobject && newdescr->hasobject) + hasobject = 1; totalsize += newdescr->elsize; } diff --git a/numpy/core/src/umathmodule.c.src b/numpy/core/src/umathmodule.c.src index a3e6d346e..78690dead 100644 --- a/numpy/core/src/umathmodule.c.src +++ b/numpy/core/src/umathmodule.c.src @@ -1616,9 +1616,23 @@ static void { register intp i; intp is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0]; - char *i1=args[0], *i2=args[1], *op=args[2]; - for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) { - *((@typ@ *)op)=*((@typ@ *)i1) @OP@ *((@typ@ *)i2); + register char *i1=args[0], *i2=args[1], *op=args[2]; + if (is1 == 0) { + register @typ@ t1 = *((@typ@ *)i1); + for (i=0; i<n; i++, i2+=is2, op+=os) { + *((@typ@ *)op) = t1 @OP@ *((@typ@ *)i2); + } + } + else if (is2 == 0) { + register @typ@ t2 = *((@typ@ *)i2); + for (i=0; i<n; i++, i1+=is1, op+=os) { + *((@typ@ *)op) = *((@typ@ *)i1) @OP@ t2; + } + } + else { + for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) { + *((@typ@ *)op)=*((@typ@ *)i1) @OP@ *((@typ@ *)i2); + } } } /**end repeat**/ |