diff options
author | Nathaniel J. Smith <njs@pobox.com> | 2012-05-14 23:43:45 +0100 |
---|---|---|
committer | Nathaniel J. Smith <njs@pobox.com> | 2012-05-15 15:02:24 +0100 |
commit | cbce4e6565e7a7fbe065502b264622ce7d64740a (patch) | |
tree | 62d357798d50da349e8e377ac0449ece2e0c23af /numpy/numarray/_capi.c | |
parent | 3bbbbd416a0ae68d68a11bbbedca1460a560378b (diff) | |
download | numpy-cbce4e6565e7a7fbe065502b264622ce7d64740a.tar.gz |
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").
Diffstat (limited to 'numpy/numarray/_capi.c')
-rw-r--r-- | numpy/numarray/_capi.c | 18 |
1 files changed, 8 insertions, 10 deletions
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); } |