summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--numpy/core/src/multiarray/arraytypes.c.src47
-rw-r--r--numpy/core/src/multiarray/datetime.c15
-rw-r--r--numpy/core/src/multiarray/na_singleton.h15
3 files changed, 73 insertions, 4 deletions
diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src
index cb76d5af9..fb8cb0987 100644
--- a/numpy/core/src/multiarray/arraytypes.c.src
+++ b/numpy/core/src/multiarray/arraytypes.c.src
@@ -18,6 +18,7 @@
#include "ctors.h"
#include "usertypes.h"
#include "_datetime.h"
+#include "na_singleton.h"
#include "numpyos.h"
@@ -142,6 +143,12 @@ static int
if (PyArray_IsScalar(op, @kind@)) {
temp = ((Py@kind@ScalarObject *)op)->obval;
}
+ else if (NpyNA_Check(op) || NpyNA_IsZeroDimArrayNA(op)) {
+ PyErr_SetString(PyExc_ValueError,
+ "Cannot assign NA to an array which "
+ "does not support NAs");
+ return -1;
+ }
else {
temp = (@type@)@func2@(op);
}
@@ -204,7 +211,16 @@ static int
c@type@ temp;
int rsize;
- if (!(PyArray_IsScalar(op, @kind@))) {
+ if (PyArray_IsScalar(op, @kind@)){
+ temp = ((Py@kind@ScalarObject *)op)->obval;
+ }
+ else if (NpyNA_Check(op) || NpyNA_IsZeroDimArrayNA(op)) {
+ PyErr_SetString(PyExc_ValueError,
+ "Cannot assign NA to an array which "
+ "does not support NAs");
+ return -1;
+ }
+ else {
if (PyArray_Check(op) && (PyArray_NDIM((PyArrayObject *)op) == 0)) {
op2 = PyArray_DESCR((PyArrayObject *)op)->f->getitem(
PyArray_BYTES((PyArrayObject *)op),
@@ -227,9 +243,7 @@ static int
temp.real = (@type@) oop.real;
temp.imag = (@type@) oop.imag;
}
- else {
- temp = ((Py@kind@ScalarObject *)op)->obval;
- }
+
memcpy(ov, &temp, PyArray_DESCR(ap)->elsize);
if (!PyArray_ISNOTSWAPPED(ap)) {
byte_swap_vector(ov, 2, sizeof(@type@));
@@ -259,6 +273,12 @@ LONGDOUBLE_setitem(PyObject *op, char *ov, PyArrayObject *ap) {
if (PyArray_IsScalar(op, LongDouble)) {
temp = ((PyLongDoubleScalarObject *)op)->obval;
}
+ else if (NpyNA_Check(op) || NpyNA_IsZeroDimArrayNA(op)) {
+ PyErr_SetString(PyExc_ValueError,
+ "Cannot assign NA to an array which "
+ "does not support NAs");
+ return -1;
+ }
else {
temp = (longdouble) MyPyFloat_AsDouble(op);
}
@@ -353,6 +373,12 @@ UNICODE_setitem(PyObject *op, char *ov, PyArrayObject *ap)
"setting an array element with a sequence");
return -1;
}
+ if (NpyNA_Check(op) || NpyNA_IsZeroDimArrayNA(op)) {
+ PyErr_SetString(PyExc_ValueError,
+ "Cannot assign NA to an array which "
+ "does not support NAs");
+ return -1;
+ }
/* Sequence_Size might have returned an error */
if (PyErr_Occurred()) {
PyErr_Clear();
@@ -457,6 +483,12 @@ STRING_setitem(PyObject *op, char *ov, PyArrayObject *ap)
"cannot set an array element with a sequence");
return -1;
}
+ if (NpyNA_Check(op) || NpyNA_IsZeroDimArrayNA(op)) {
+ PyErr_SetString(PyExc_ValueError,
+ "Cannot assign NA to an array which "
+ "does not support NAs");
+ return -1;
+ }
#if defined(NPY_PY3K)
if (PyUnicode_Check(op)) {
/* Assume ASCII codec -- function similarly as Python 2 */
@@ -752,6 +784,13 @@ VOID_setitem(PyObject *op, char *ip, PyArrayObject *ap)
return res;
}
+ if (NpyNA_Check(op) || NpyNA_IsZeroDimArrayNA(op)) {
+ PyErr_SetString(PyExc_ValueError,
+ "Cannot assign NA to an array which "
+ "does not support NAs");
+ return -1;
+ }
+
/* Default is to use buffer interface to set item */
{
const void *buffer;
diff --git a/numpy/core/src/multiarray/datetime.c b/numpy/core/src/multiarray/datetime.c
index cc4fc9d63..985e88de5 100644
--- a/numpy/core/src/multiarray/datetime.c
+++ b/numpy/core/src/multiarray/datetime.c
@@ -24,6 +24,7 @@
#include "methods.h"
#include "_datetime.h"
#include "datetime_strings.h"
+#include "na_singleton.h"
/*
* Imports the PyDateTime functions so we can create these objects.
@@ -2655,6 +2656,13 @@ convert_pyobject_to_datetime(PyArray_DatetimeMetaData *meta, PyObject *obj,
*out = NPY_DATETIME_NAT;
return 0;
}
+ /* Check for NA */
+ else if (NpyNA_Check(obj) || NpyNA_IsZeroDimArrayNA(obj)) {
+ PyErr_SetString(PyExc_ValueError,
+ "Cannot assign NA to an array which "
+ "does not support NAs");
+ return -1;
+ }
else {
PyErr_SetString(PyExc_ValueError,
"Could not convert object to NumPy datetime");
@@ -2916,6 +2924,13 @@ convert_pyobject_to_timedelta(PyArray_DatetimeMetaData *meta, PyObject *obj,
*out = NPY_DATETIME_NAT;
return 0;
}
+ /* Check for NA */
+ else if (NpyNA_Check(obj) || NpyNA_IsZeroDimArrayNA(obj)) {
+ PyErr_SetString(PyExc_ValueError,
+ "Cannot assign NA to an array which "
+ "does not support NAs");
+ return -1;
+ }
else {
PyErr_SetString(PyExc_ValueError,
"Could not convert object to NumPy timedelta");
diff --git a/numpy/core/src/multiarray/na_singleton.h b/numpy/core/src/multiarray/na_singleton.h
index 0e3399c89..19be74f00 100644
--- a/numpy/core/src/multiarray/na_singleton.h
+++ b/numpy/core/src/multiarray/na_singleton.h
@@ -63,4 +63,19 @@ NpyNA_FromDTypeAndMaskValue(PyArray_Descr *dtype, npy_mask maskvalue,
NPY_NO_EXPORT npy_mask
NpyNA_AsMaskValue(NpyNA *na);
+/*
+ * Returns True if the object is an NA in the form of a 0-dimensional
+ * array.
+ */
+static NPY_INLINE npy_bool
+NpyNA_IsZeroDimArrayNA(PyObject *obj)
+{
+ return PyArray_Check(obj) &&
+ PyArray_NDIM((PyArrayObject *)obj) == 0 &&
+ PyArray_HASMASKNA((PyArrayObject *)obj) &&
+ !PyArray_HASFIELDS((PyArrayObject *)obj) &&
+ !NpyMaskValue_IsExposed((npy_mask)*PyArray_MASKNA_DATA(
+ (PyArrayObject *)obj));
+}
+
#endif