From efad230ab73eae745272f251f6861a17885362fb Mon Sep 17 00:00:00 2001 From: "Mads R. B. Kristensen" Date: Thu, 22 Oct 2015 00:01:30 +0200 Subject: [PATCH] npbackend: added a indexing with array check in SetItem() --- bridge/npbackend/src/_bhmodule.c | 85 ++++++++++++++++++++++++-------- bridge/npbackend/stdviews.py | 4 +- 2 files changed, 67 insertions(+), 22 deletions(-) diff --git a/bridge/npbackend/src/_bhmodule.c b/bridge/npbackend/src/_bhmodule.c index 16c31b54c..baa9ae888 100644 --- a/bridge/npbackend/src/_bhmodule.c +++ b/bridge/npbackend/src/_bhmodule.c @@ -606,26 +606,6 @@ BhArray_SetSlice(PyObject *o, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) return 0; } -static int -BhArray_SetItem(PyObject *o, PyObject *key, PyObject *v) -{ - if(v == NULL) - { - PyErr_SetString(PyExc_ValueError, "cannot delete array elements"); - return -1; - } - if(!PyArray_ISWRITEABLE((PyArrayObject *)o)) - { - PyErr_SetString(PyExc_ValueError, "assignment destination is read-only"); - return -1; - } - PyObject *ret = PyObject_CallMethod(ufunc, "setitem", "OOO", o, key, v); - if(ret == NULL) - return -1; - Py_XDECREF(ret); - return 0; -} - //Help function that returns True when 'o' contains a list or array static int obj_contains_a_list_or_ary(PyObject *o) { @@ -647,6 +627,71 @@ static int obj_contains_a_list_or_ary(PyObject *o) return 0; } +static int +BhArray_SetItem(PyObject *o, PyObject *k, PyObject *v) +{ + Py_ssize_t i; + assert(k != NULL); + if(v == NULL) + { + PyErr_SetString(PyExc_ValueError, "cannot delete array elements"); + return -1; + } + if(!PyArray_ISWRITEABLE((PyArrayObject *)o)) + { + PyErr_SetString(PyExc_ValueError, "assignment destination is read-only"); + return -1; + } + + //We do not support indexing with arrays + if(obj_contains_a_list_or_ary(k) == 1) + { + PyErr_WarnEx(NULL,"Bohrium does not support indexing with arrays. " + "It will be handled by the original NumPy.",1); + + //Let's make sure that 'k' is a NumPy array + if(BhArray_CheckExact(k)) + { + k = BhArray_copy2numpy(k, NULL); + if(k == NULL) + return -1; + } + if(PyTuple_Check(k)) + { + for(i=0; imp_ass_subscript(o, k, v); + } + //It is a regular SetItem call, let's do it in Python + PyObject *ret = PyObject_CallMethod(ufunc, "setitem", "OOO", o, k, v); + if(ret == NULL) + return -1; + Py_XDECREF(ret); + return 0; +} + static PyObject * BhArray_GetItem(PyObject *o, PyObject *k) { diff --git a/bridge/npbackend/stdviews.py b/bridge/npbackend/stdviews.py index 89085a74d..118d73602 100644 --- a/bridge/npbackend/stdviews.py +++ b/bridge/npbackend/stdviews.py @@ -13,13 +13,13 @@ def cartesian(x, size): stop = -size+1+i if stop==0: stop = None dist.append((start,stop)) - stencil = [x[s] for s in [map((lambda se : slice(se[0],se[1])),i) + stencil = [x[tuple(s)] for s in [map((lambda se : slice(se[0],se[1])),i) for i in itertools.product(dist, repeat=len(x.shape))]] return stencil def no_border(x, boarder): - stencil = [x[s] for s in [map((lambda se : slice(se[0],se[1])),i) + stencil = [x[tuple(s)] for s in [map((lambda se : slice(se[0],se[1])),i) for i in itertools.product([(boarder,-boarder)], repeat=len(x.shape))]] return stencil[0]