From cbce4e6565e7a7fbe065502b264622ce7d64740a Mon Sep 17 00:00:00 2001 From: "Nathaniel J. Smith" Date: Mon, 14 May 2012 23:43:45 +0100 Subject: Consolidate all array writeability checking in new PyArray_RequireWriteable This is mostly a code cleanup, but it does have a user-visible effect in that attempting to write to a unwriteable array now consistently raises ValueError. (It used to randomly raise either ValueError or RuntimeError.) Passes numpy.test("full"). --- numpy/numarray/_capi.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'numpy/numarray') diff --git a/numpy/numarray/_capi.c b/numpy/numarray/_capi.c index fee07d79d..b6e5c1a3c 100644 --- a/numpy/numarray/_capi.c +++ b/numpy/numarray/_capi.c @@ -1077,9 +1077,12 @@ NA_OutputArray(PyObject *a, NumarrayType t, int requires) PyArray_Descr *dtype; PyArrayObject *ret; - if (!PyArray_Check(a) || !PyArray_ISWRITEABLE((PyArrayObject *)a)) { + if (!PyArray_Check(a)) { PyErr_Format(PyExc_TypeError, - "NA_OutputArray: only writeable arrays work for output."); + "NA_OutputArray: only arrays work for output."); + return NULL; + } + if (PyArray_RequireWriteable((PyArrayObject *)a, NULL) < 0) { return NULL; } @@ -1127,9 +1130,7 @@ NA_IoArray(PyObject *a, NumarrayType t, int requires) /* Guard against non-writable, but otherwise satisfying requires. In this case, shadow == a. */ - if (!PyArray_ISWRITABLE(shadow)) { - PyErr_Format(PyExc_TypeError, - "NA_IoArray: I/O array must be writable array"); + if (!PyArray_RequireWriteable(shadow, NULL)) { PyArray_XDECREF_ERR(shadow); return NULL; } @@ -2488,13 +2489,10 @@ _setFromPythonScalarCore(PyArrayObject *a, long offset, PyObject*value, int entr static int NA_setFromPythonScalar(PyArrayObject *a, long offset, PyObject *value) { - if (PyArray_FLAGS(a) & NPY_ARRAY_WRITEABLE) - return _setFromPythonScalarCore(a, offset, value, 0); - else { - PyErr_Format( - PyExc_ValueError, "NA_setFromPythonScalar: assigment to readonly array buffer"); + if (PyArray_RequireWriteable(a, NULL) < 0) { return -1; } + return _setFromPythonScalarCore(a, offset, value, 0); } -- cgit v1.2.1