diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-11-17 20:17:30 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-11-17 20:17:30 +0000 |
commit | ccd63380eb664f67c5fa9fcaad8860138c8c0607 (patch) | |
tree | f9e4b56fbd5afe2883f0d3587d25f16f54acc2d1 /numpy/core/src/arrayobject.c | |
parent | e2756cb71da612edbd7941fc896ef01ebc7424ed (diff) | |
download | numpy-ccd63380eb664f67c5fa9fcaad8860138c8c0607.tar.gz |
Allow data-type objects to be 'repeated' using '*' like ctypes.
Diffstat (limited to 'numpy/core/src/arrayobject.c')
-rw-r--r-- | numpy/core/src/arrayobject.c | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c index cde69108e..4e18f2fa5 100644 --- a/numpy/core/src/arrayobject.c +++ b/numpy/core/src/arrayobject.c @@ -4027,7 +4027,7 @@ 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)NULL, (ssizeargfunc)array_item_nice, (ssizessizeargfunc)array_slice, (ssizeobjargproc)array_ass_item, /*sq_ass_item*/ @@ -10976,7 +10976,6 @@ arraydescr_reduce(PyArray_Descr *self, PyObject *args) } /* returns 1 if this data-type has an object portion - used when setting the state because hasobject is not stored. */ static int @@ -11397,6 +11396,26 @@ descr_length(PyObject *self0) } static PyObject * +descr_repeat(PyObject *self, Py_ssize_t length) +{ + PyObject *tup; + PyArray_Descr *new; + if (length < 0) + return PyErr_Format(PyExc_ValueError, +#if (PY_VERSION_HEX < 0x02050000) + "Array length must be >= 0, not %d", +#else + "Array length must be >= 0, not %zd", +#endif + length); + tup = Py_BuildValue("O" NPY_SSIZE_T_PYFMT, self, length); + if (tup == NULL) return NULL; + PyArray_DescrConverter(tup, &new); + Py_DECREF(tup); + return (PyObject *)new; +} + +static PyObject * descr_subscript(PyArray_Descr *self, PyObject *op) { @@ -11449,6 +11468,12 @@ descr_subscript(PyArray_Descr *self, PyObject *op) return NULL; } +static PySequenceMethods descr_as_sequence = { + descr_length, + (binaryfunc)NULL, + descr_repeat, +}; + static PyMappingMethods descr_as_mapping = { descr_length, /*mp_length*/ (binaryfunc)descr_subscript, /*mp_subscript*/ @@ -11472,7 +11497,7 @@ static PyTypeObject PyArrayDescr_Type = { 0, /* tp_compare */ (reprfunc)arraydescr_repr, /* tp_repr */ 0, /* tp_as_number */ - 0, /* tp_as_sequence */ + &descr_as_sequence, /* tp_as_sequence */ &descr_as_mapping, /* tp_as_mapping */ 0, /* tp_hash */ 0, /* tp_call */ |