diff options
author | Han <hangenuit@gmail.com> | 2011-10-17 12:21:10 +0200 |
---|---|---|
committer | Sebastian Berg <sebastian@sipsolutions.net> | 2013-04-11 16:41:15 +0200 |
commit | 4b8ef7270aa20d1be6c807284a15646fcf1a1677 (patch) | |
tree | 22fea1595214d3022e78fca77ac68cda3abdcade | |
parent | eab77ae891eaf08301de0c69f5b68021cd1d3ac9 (diff) | |
download | numpy-4b8ef7270aa20d1be6c807284a15646fcf1a1677.tar.gz |
ENH: Optimization in subscript_simple for arrays with size > 1
-rw-r--r-- | numpy/core/src/multiarray/mapping.c | 53 |
1 files changed, 31 insertions, 22 deletions
diff --git a/numpy/core/src/multiarray/mapping.c b/numpy/core/src/multiarray/mapping.c index c514cfe14..19cf8aad8 100644 --- a/numpy/core/src/multiarray/mapping.c +++ b/numpy/core/src/multiarray/mapping.c @@ -565,31 +565,40 @@ array_subscript_simple(PyArrayObject *self, PyObject *op) PyArrayObject *ret; npy_intp value; - /* - * PyNumber_Index was introduced in Python 2.5 because of NumPy. - * http://www.python.org/dev/peps/pep-0357/ - * Let's use it for indexing! - * - * Unfortunately, SciPy and possibly other code seems to rely - * on the lenient coercion. :( - */ + + if (!(PyArray_Check(op) && (PyArray_SIZE((PyArrayObject*)op) > 1))) { + /* + * PyNumber_Index was introduced in Python 2.5 because of NumPy. + * http://www.python.org/dev/peps/pep-0357/ + * Let's use it for indexing! + * + * Unfortunately, SciPy and possibly other code seems to rely + * on the lenient coercion. :( + */ #if 0 /*PY_VERSION_HEX >= 0x02050000*/ - PyObject *ind = PyNumber_Index(op); - if (ind != NULL) { - value = PyArray_PyIntAsIntp(ind); - Py_DECREF(ind); - } - else { - value = -1; - } + PyObject *ind = PyNumber_Index(op); + if (ind != NULL) { + value = PyArray_PyIntAsIntp(ind); + Py_DECREF(ind); + } + else { + value = -1; + } #else - value = PyArray_PyIntAsIntp(op); + value = PyArray_PyIntAsIntp(op); #endif - if (value == -1 && PyErr_Occurred()) { - PyErr_Clear(); - } - else { - return array_item_asarray(self, value); + if (value == -1 && PyErr_Occurred()) { + if (PyErr_ExceptionMatches(PyExc_TypeError)) { + /* Operand is not an integer type */ + PyErr_Clear(); + } + else { + return NULL; + } + } + else { + return array_item_asarray(self, value); + } } /* Standard (view-based) Indexing */ |