diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-12-08 03:23:10 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-12-08 03:23:10 +0000 |
commit | db487c204c9df0472642d14d80632382463ecbcc (patch) | |
tree | e066024d8d2388ff525556dc0f11478174294f75 /numpy/core/src/arrayobject.c | |
parent | e3a1550ff1417599f68207f60ea622404d281178 (diff) | |
download | numpy-db487c204c9df0472642d14d80632382463ecbcc.tar.gz |
Add feature so that user-defined types can be recognized when instances of scalars of those types are present in the nested sequence.
Diffstat (limited to 'numpy/core/src/arrayobject.c')
-rw-r--r-- | numpy/core/src/arrayobject.c | 62 |
1 files changed, 44 insertions, 18 deletions
diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c index aef04b4c7..eaed75f14 100644 --- a/numpy/core/src/arrayobject.c +++ b/numpy/core/src/arrayobject.c @@ -6918,27 +6918,53 @@ _array_find_python_scalar_type(PyObject *op) { if (PyFloat_Check(op)) { return PyArray_DescrFromType(PyArray_DOUBLE); - } else if (PyComplex_Check(op)) { - return PyArray_DescrFromType(PyArray_CDOUBLE); - } else if (PyInt_Check(op)) { - /* bools are a subclass of int */ - if (PyBool_Check(op)) { - return PyArray_DescrFromType(PyArray_BOOL); - } else { - return PyArray_DescrFromType(PyArray_LONG); - } - } else if (PyLong_Check(op)) { - /* if integer can fit into a longlong then return that - */ - if ((PyLong_AsLongLong(op) == -1) && PyErr_Occurred()) { - PyErr_Clear(); - return PyArray_DescrFromType(PyArray_OBJECT); - } - return PyArray_DescrFromType(PyArray_LONGLONG); + } + else if (PyComplex_Check(op)) { + return PyArray_DescrFromType(PyArray_CDOUBLE); + } + else if (PyInt_Check(op)) { + /* bools are a subclass of int */ + if (PyBool_Check(op)) { + return PyArray_DescrFromType(PyArray_BOOL); + } else { + return PyArray_DescrFromType(PyArray_LONG); + } + } + else if (PyLong_Check(op)) { + /* if integer can fit into a longlong then return that + */ + if ((PyLong_AsLongLong(op) == -1) && PyErr_Occurred()) { + PyErr_Clear(); + return PyArray_DescrFromType(PyArray_OBJECT); + } + return PyArray_DescrFromType(PyArray_LONGLONG); } return NULL; } +static PyArray_Descr * +_use_default_type(PyObject *op) +{ + int typenum, l; + PyObject *type; + + typenum = -1; + l = 0; + type = (PyObject *)op->ob_type; + while (l < PyArray_NUMUSERTYPES) { + if (type == (PyObject *)(userdescrs[l]->typeobj)) { + typenum = l + PyArray_USERDEF; + break; + } + l++; + } + if (typenum == -1) { + typenum = PyArray_OBJECT; + } + return PyArray_DescrFromType(typenum); +} + + /* op is an object to be converted to an ndarray. minitype is the minimum type-descriptor needed. @@ -7086,7 +7112,7 @@ _array_find_type(PyObject *op, PyArray_Descr *minitype, int max) deflt: - chktype = PyArray_DescrFromType(PyArray_OBJECT); + chktype = _use_default_type(op); finish: |