From 98389353b020db90e90e00da4bdf19e18f942cba Mon Sep 17 00:00:00 2001 From: David Cournapeau Date: Thu, 30 Apr 2009 08:37:19 +0000 Subject: Put sequence protocol in separate file. --- numpy/core/src/arrayobject.c | 139 +------------------------------------------ 1 file changed, 1 insertion(+), 138 deletions(-) (limited to 'numpy/core/src/arrayobject.c') diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c index be4932551..c141314ac 100644 --- a/numpy/core/src/arrayobject.c +++ b/numpy/core/src/arrayobject.c @@ -35,6 +35,7 @@ maintainer email: oliphant.travis@ieee.org #include "arrayiterators.h" #include "arraymapping.h" #include "arraygetset.h" +#include "arraysequence.h" #ifndef Py_UNICODE_WIDE #include "ucsnarrow.h" #endif @@ -2217,144 +2218,6 @@ static PyBufferProcs array_as_buffer = { /****************** End of Buffer Protocol *******************************/ - -/************************************************************************* - **************** Implement Sequence Protocol ************************** - *************************************************************************/ - -/* Some of this is repeated in the array_as_mapping protocol. But - we fill it in here so that PySequence_XXXX calls work as expected -*/ - - -static PyObject * -array_slice(PyArrayObject *self, Py_ssize_t ilow, - Py_ssize_t ihigh) -{ - PyArrayObject *r; - Py_ssize_t l; - char *data; - - if (self->nd == 0) { - PyErr_SetString(PyExc_ValueError, "cannot slice a 0-d array"); - return NULL; - } - - l=self->dimensions[0]; - if (ilow < 0) { - ilow = 0; - } - else if (ilow > l) { - ilow = l; - } - if (ihigh < ilow) { - ihigh = ilow; - } - else if (ihigh > l) { - ihigh = l; - } - - if (ihigh != ilow) { - data = index2ptr(self, ilow); - if (data == NULL) { - return NULL; - } - } - else { - data = self->data; - } - - self->dimensions[0] = ihigh-ilow; - Py_INCREF(self->descr); - r = (PyArrayObject *) \ - PyArray_NewFromDescr(self->ob_type, self->descr, - self->nd, self->dimensions, - self->strides, data, - self->flags, (PyObject *)self); - self->dimensions[0] = l; - if (r == NULL) { - return NULL; - } - r->base = (PyObject *)self; - Py_INCREF(self); - PyArray_UpdateFlags(r, UPDATE_ALL); - return (PyObject *)r; -} - - -static int -array_ass_slice(PyArrayObject *self, Py_ssize_t ilow, - Py_ssize_t ihigh, PyObject *v) { - int ret; - PyArrayObject *tmp; - - if (v == NULL) { - PyErr_SetString(PyExc_ValueError, - "cannot delete array elements"); - return -1; - } - if (!PyArray_ISWRITEABLE(self)) { - PyErr_SetString(PyExc_RuntimeError, - "array is not writeable"); - return -1; - } - if ((tmp = (PyArrayObject *)array_slice(self, ilow, ihigh)) == NULL) { - return -1; - } - ret = PyArray_CopyObject(tmp, v); - Py_DECREF(tmp); - - return ret; -} - -static int -array_contains(PyArrayObject *self, PyObject *el) -{ - /* equivalent to (self == el).any() */ - - PyObject *res; - int ret; - - res = PyArray_EnsureAnyArray(PyObject_RichCompare((PyObject *)self, - el, Py_EQ)); - if (res == NULL) { - return -1; - } - ret = array_any_nonzero((PyArrayObject *)res); - Py_DECREF(res); - return ret; -} - -static PySequenceMethods array_as_sequence = { -#if PY_VERSION_HEX >= 0x02050000 - (lenfunc)array_length, /*sq_length*/ - (binaryfunc)NULL, /*sq_concat is handled by nb_add*/ - (ssizeargfunc)NULL, - (ssizeargfunc)array_item_nice, - (ssizessizeargfunc)array_slice, - (ssizeobjargproc)array_ass_item, /*sq_ass_item*/ - (ssizessizeobjargproc)array_ass_slice, /*sq_ass_slice*/ - (objobjproc) array_contains, /*sq_contains */ - (binaryfunc) NULL, /*sg_inplace_concat */ - (ssizeargfunc)NULL, -#else - (inquiry)array_length, /*sq_length*/ - (binaryfunc)NULL, /*sq_concat is handled by nb_add*/ - (intargfunc)NULL, /*sq_repeat is handled nb_multiply*/ - (intargfunc)array_item_nice, /*sq_item*/ - (intintargfunc)array_slice, /*sq_slice*/ - (intobjargproc)array_ass_item, /*sq_ass_item*/ - (intintobjargproc)array_ass_slice, /*sq_ass_slice*/ - (objobjproc) array_contains, /*sq_contains */ - (binaryfunc) NULL, /*sg_inplace_concat */ - (intargfunc) NULL /*sg_inplace_repeat */ -#endif -}; - - -/****************** End of Sequence Protocol ****************************/ - - static int dump_data(char **string, int *n, int *max_n, char *data, int nd, intp *dimensions, intp *strides, PyArrayObject* self) -- cgit v1.2.1