summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTravis Oliphant <oliphant@enthought.com>2006-09-04 19:31:30 +0000
committerTravis Oliphant <oliphant@enthought.com>2006-09-04 19:31:30 +0000
commitb1e4fe0ae77bc204c3ecbe85e606f846f42d189a (patch)
treebc865f0e4691da849d58a73fbc7fd5ad5e0e879c
parentcd71708a570b7eb770e54ba08ccbf7b414b26201 (diff)
downloadnumpy-b1e4fe0ae77bc204c3ecbe85e606f846f42d189a.tar.gz
Add Index checking for integer conversion and Python 2.5
-rw-r--r--numpy/core/include/numpy/ndarrayobject.h1
-rw-r--r--numpy/core/src/arrayobject.c19
2 files changed, 17 insertions, 3 deletions
diff --git a/numpy/core/include/numpy/ndarrayobject.h b/numpy/core/include/numpy/ndarrayobject.h
index d3211ddd2..7e8b48dc0 100644
--- a/numpy/core/include/numpy/ndarrayobject.h
+++ b/numpy/core/include/numpy/ndarrayobject.h
@@ -645,6 +645,7 @@ typedef int Py_ssize_t;
#define PY_SSIZE_T_MIN INT_MIN
#define NPY_SSIZE_T_PYFMT "i"
#define constchar const char
+#define PyIndex_Check(op) 0
#else
#define NPY_SSIZE_T_PYFMT "n"
#define constchar char
diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c
index 0bfb5a06d..316dc57cd 100644
--- a/numpy/core/src/arrayobject.c
+++ b/numpy/core/src/arrayobject.c
@@ -560,6 +560,12 @@ PyArray_PyIntAsIntp(PyObject *o)
Py_DECREF(arr);
return ret;
}
+#if (PY_VERSION_HEX >= 0x02050000)
+ if (PyIndex_Check(op)) {
+ long_value = (longlong) PyNumber_Index(op);
+ goto finish;
+ }
+#endif
if (o->ob_type->tp_as_number != NULL && \
o->ob_type->tp_as_number->nb_long != NULL) {
obj = o->ob_type->tp_as_number->nb_long(o);
@@ -643,6 +649,12 @@ PyArray_PyIntAsInt(PyObject *o)
Py_DECREF(arr);
return ret;
}
+#if (PY_VERSION_HEX >= 0x02050000)
+ if (PyIndex_Check(o)) {
+ long_value = (longlong) PyNumber_Index(o);
+ goto finish;
+ }
+#endif
if (o->ob_type->tp_as_number != NULL && \
o->ob_type->tp_as_number->nb_int != NULL) {
obj = o->ob_type->tp_as_number->nb_int(o);
@@ -2679,7 +2691,7 @@ array_subscript(PyArrayObject *self, PyObject *op)
}
if (PyInt_Check(op) || PyArray_IsScalar(op, Integer) ||
- PyLong_Check(op)) {
+ PyLong_Check(op) || PyIndex_Check(index)) {
intp value;
value = PyArray_PyIntAsIntp(op);
if (!PyErr_Occurred()) {
@@ -2792,7 +2804,7 @@ array_ass_sub(PyArrayObject *self, PyObject *index, PyObject *op)
}
if (PyInt_Check(index) || PyArray_IsScalar(index, Integer) ||
- PyLong_Check(index)) {
+ PyLong_Check(index) || PyIndex_Check(index)) {
intp value;
value = PyArray_PyIntAsIntp(index);
if (PyErr_Occurred())
@@ -2890,6 +2902,7 @@ array_ass_sub(PyArrayObject *self, PyObject *index, PyObject *op)
Python so that 0-dim arrays are passed as scalars
*/
+
static PyObject *
array_subscript_nice(PyArrayObject *self, PyObject *op)
{
@@ -2898,7 +2911,7 @@ array_subscript_nice(PyArrayObject *self, PyObject *op)
intp vals[MAX_DIMS];
if (PyInt_Check(op) || PyArray_IsScalar(op, Integer) || \
- PyLong_Check(op)) {
+ PyLong_Check(op) || PyIndex_Check(op)) {
intp value;
value = PyArray_PyIntAsIntp(op);
if (PyErr_Occurred())