summaryrefslogtreecommitdiff
path: root/numpy/core/src
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/core/src')
-rw-r--r--numpy/core/src/multiarray/arrayobject.c28
-rw-r--r--numpy/core/src/multiarray/convert_datatype.c16
-rw-r--r--numpy/core/src/multiarray/descriptor.c27
-rw-r--r--numpy/core/src/scalarmathmodule.c.src35
-rw-r--r--numpy/core/src/umath/loops.c.src6
5 files changed, 66 insertions, 46 deletions
diff --git a/numpy/core/src/multiarray/arrayobject.c b/numpy/core/src/multiarray/arrayobject.c
index ab1e824be..587e45d26 100644
--- a/numpy/core/src/multiarray/arrayobject.c
+++ b/numpy/core/src/multiarray/arrayobject.c
@@ -907,16 +907,15 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
array_other = PyArray_FromObject(other,
typenum, 0, 0);
/*
- * If not successful, then return False. This fixes code
- * that used to allow equality comparisons between arrays
- * and other objects which would give a result of False.
+ * If not successful, indicate that the items cannot be compared
+ * this way.
*/
if ((array_other == NULL) ||
(array_other == Py_None)) {
Py_XDECREF(array_other);
PyErr_Clear();
- Py_INCREF(Py_False);
- return Py_False;
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
}
}
else {
@@ -952,14 +951,14 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
}
/*
* If the comparison results in NULL, then the
- * two array objects can not be compared together so
- * return zero
+ * two array objects can not be compared together;
+ * indicate that
*/
Py_DECREF(array_other);
if (result == NULL) {
PyErr_Clear();
- Py_INCREF(Py_False);
- return Py_False;
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
}
break;
case Py_NE:
@@ -976,14 +975,13 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
array_other = PyArray_FromObject(other, typenum, 0, 0);
/*
* If not successful, then objects cannot be
- * compared and cannot be equal, therefore,
- * return True;
+ * compared this way
*/
if ((array_other == NULL) || (array_other == Py_None)) {
Py_XDECREF(array_other);
PyErr_Clear();
- Py_INCREF(Py_True);
- return Py_True;
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
}
}
else {
@@ -1021,8 +1019,8 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
Py_DECREF(array_other);
if (result == NULL) {
PyErr_Clear();
- Py_INCREF(Py_True);
- return Py_True;
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
}
break;
case Py_GT:
diff --git a/numpy/core/src/multiarray/convert_datatype.c b/numpy/core/src/multiarray/convert_datatype.c
index 37b0e5e18..5cb62b82f 100644
--- a/numpy/core/src/multiarray/convert_datatype.c
+++ b/numpy/core/src/multiarray/convert_datatype.c
@@ -112,21 +112,25 @@ PyArray_GetCastFunc(PyArray_Descr *descr, int type_num)
PyTypeNum_ISNUMBER(type_num) &&
!PyTypeNum_ISBOOL(type_num)) {
PyObject *cls = NULL, *obj = NULL;
+ int ret;
obj = PyImport_ImportModule("numpy.core");
if (obj) {
cls = PyObject_GetAttrString(obj, "ComplexWarning");
Py_DECREF(obj);
}
#if PY_VERSION_HEX >= 0x02050000
- PyErr_WarnEx(cls,
- "Casting complex values to real discards the imaginary "
- "part", 0);
+ ret = PyErr_WarnEx(cls,
+ "Casting complex values to real discards the imaginary "
+ "part", 0);
#else
- PyErr_Warn(cls,
- "Casting complex values to real discards the imaginary "
- "part");
+ ret = PyErr_Warn(cls,
+ "Casting complex values to real discards the imaginary "
+ "part");
#endif
Py_XDECREF(cls);
+ if (ret < 0) {
+ return NULL;
+ }
}
if (castfunc) {
return castfunc;
diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c
index 74cb7da7a..f635e1c48 100644
--- a/numpy/core/src/multiarray/descriptor.c
+++ b/numpy/core/src/multiarray/descriptor.c
@@ -1828,6 +1828,7 @@ arraydescr_names_set(PyArray_Descr *self, PyObject *val)
int N = 0;
int i;
PyObject *new_names;
+ PyObject *new_fields;
if (self->names == NULL) {
PyErr_SetString(PyExc_ValueError,
"there are no fields defined");
@@ -1857,26 +1858,38 @@ arraydescr_names_set(PyArray_Descr *self, PyObject *val)
}
/* Update dictionary keys in fields */
new_names = PySequence_Tuple(val);
+ new_fields = PyDict_New();
for (i = 0; i < N; i++) {
PyObject *key;
PyObject *item;
PyObject *new_key;
+ int ret;
key = PyTuple_GET_ITEM(self->names, i);
- /* Borrowed reference to item */
+ /* Borrowed references to item and new_key */
item = PyDict_GetItem(self->fields, key);
- /* Hold on to it even through DelItem */
- Py_INCREF(item);
new_key = PyTuple_GET_ITEM(new_names, i);
- PyDict_DelItem(self->fields, key);
- PyDict_SetItem(self->fields, new_key, item);
- /* self->fields now holds reference */
- Py_DECREF(item);
+ /* Check for duplicates */
+ ret = PyDict_Contains(new_fields, new_key);
+ if (ret != 0) {
+ if (ret < 0) {
+ PyErr_Clear();
+ }
+ PyErr_SetString(PyExc_ValueError, "Duplicate field names given.");
+ Py_DECREF(new_names);
+ Py_DECREF(new_fields);
+ return -1;
+ }
+ PyDict_SetItem(new_fields, new_key, item);
}
/* Replace names */
Py_DECREF(self->names);
self->names = new_names;
+ /* Replace fields */
+ Py_DECREF(self->fields);
+ self->fields = new_fields;
+
return 0;
}
diff --git a/numpy/core/src/scalarmathmodule.c.src b/numpy/core/src/scalarmathmodule.c.src
index f3ee6724e..6261c4c73 100644
--- a/numpy/core/src/scalarmathmodule.c.src
+++ b/numpy/core/src/scalarmathmodule.c.src
@@ -974,10 +974,11 @@ NONZERO_NAME(@name@_,)(PyObject *a)
/**end repeat**/
-static void
+static int
emit_complexwarning()
{
static PyObject *cls = NULL;
+ int ret;
if (cls == NULL) {
PyObject *mod;
mod = PyImport_ImportModule("numpy.core");
@@ -987,13 +988,13 @@ emit_complexwarning()
Py_DECREF(mod);
}
#if PY_VERSION_HEX >= 0x02050000
- PyErr_WarnEx(cls,
- "Casting complex values to real discards the imaginary "
- "part", 0);
+ return PyErr_WarnEx(cls,
+ "Casting complex values to real discards the imaginary "
+ "part", 0);
#else
- PyErr_Warn(cls,
- "Casting complex values to real discards the imaginary "
- "part");
+ return PyErr_Warn(cls,
+ "Casting complex values to real discards the imaginary "
+ "part");
#endif
}
@@ -1003,7 +1004,7 @@ emit_complexwarning()
* #Name=Byte,UByte,Short,UShort,Int,UInt,Long,ULong,LongLong,ULongLong,Float,Double,LongDouble,CFloat,CDouble,CLongDouble#
* #cmplx=0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1#
* #sign=(signed,unsigned)*5,,,,,,#
- * #unsigntyp=0,1,0,1,0,1,0,1,0,1,1*6#
+ * #unsigntyp=0,1,0,1,0,1,0,1,0,1,0*6#
* #ctype=long*8,PY_LONG_LONG*2,double*6#
* #realtyp=0*10,1*6#
* #func=(PyLong_FromLong,PyLong_FromUnsignedLong)*4,PyLong_FromLongLong,PyLong_FromUnsignedLongLong,PyLong_FromDouble*6#
@@ -1013,6 +1014,7 @@ static PyObject *
{
#if @cmplx@
@sign@ @ctype@ x= PyArrayScalar_VAL(obj, @Name@).real;
+ int ret;
#else
@sign@ @ctype@ x= PyArrayScalar_VAL(obj, @Name@);
#endif
@@ -1022,15 +1024,14 @@ static PyObject *
x = ix;
#endif
#if @cmplx@
- emit_complexwarning();
+ ret = emit_complexwarning();
+ if (ret < 0) {
+ return NULL;
+ }
#endif
-/*
- * For unsigned type, the (@ctype@) cast just does what is implicitely done by
- * the compiler.
- */
#if @unsigntyp@
- if(LONG_MIN < (@ctype@)x && (@ctype@)x < LONG_MAX)
+ if(x < LONG_MAX)
return PyInt_FromLong(x);
#else
if(LONG_MIN < x && x < LONG_MAX)
@@ -1052,7 +1053,11 @@ static PyObject *
@name@_@which@(PyObject *obj)
{
#if @cmplx@
- emit_complexwarning();
+ int ret;
+ ret = emit_complexwarning();
+ if (ret < 0) {
+ return NULL;
+ }
return @func@((PyArrayScalar_VAL(obj, @Name@)).real);
#else
return @func@((PyArrayScalar_VAL(obj, @Name@)));
diff --git a/numpy/core/src/umath/loops.c.src b/numpy/core/src/umath/loops.c.src
index e77da0691..850126482 100644
--- a/numpy/core/src/umath/loops.c.src
+++ b/numpy/core/src/umath/loops.c.src
@@ -1546,9 +1546,9 @@ C@TYPE@_sign(char **args, intp *dimensions, intp *steps, void *NPY_UNUSED(func))
UNARY_LOOP {
const @type@ in1r = ((@type@ *)ip1)[0];
const @type@ in1i = ((@type@ *)ip1)[1];
- ((@type@ *)op1)[0] = CGT(in1r, in1i, 0, 0) ? 1 :
- (CLT(in1r, in1i, 0, 0) ? -1 :
- (CEQ(in1r, in1i, 0, 0) ? 0 : NPY_NAN@C@));
+ ((@type@ *)op1)[0] = CGT(in1r, in1i, 0.0, 0.0) ? 1 :
+ (CLT(in1r, in1i, 0.0, 0.0) ? -1 :
+ (CEQ(in1r, in1i, 0.0, 0.0) ? 0 : NPY_NAN@C@));
((@type@ *)op1)[1] = 0;
}
}