summaryrefslogtreecommitdiff
path: root/numpy/core/src/arrayobject.c
diff options
context:
space:
mode:
authorcookedm <cookedm@localhost>2006-02-24 05:47:33 +0000
committercookedm <cookedm@localhost>2006-02-24 05:47:33 +0000
commite3a1d502e5d08a755dd1d91eb74341c7617adbdd (patch)
tree2d0d140f593dc4fe1c90caa16c96c9e1e3d20533 /numpy/core/src/arrayobject.c
parentee7b258d90768a6ec1e7a697e3b7b5067eda00e4 (diff)
downloadnumpy-e3a1d502e5d08a755dd1d91eb74341c7617adbdd.tar.gz
Special-case PyArray_FromAny to handle Python scalars.
bool, int, long, float, and complex are tested for early, and converted using PyArray_FromScalar before going to the general code. Before, tests for attributes like __array__ would be done first. This does have the side effect that if you have a subclass of float, say, that defines the array interface then that will not be used. I can't conceive of a use for that though.
Diffstat (limited to 'numpy/core/src/arrayobject.c')
-rw-r--r--numpy/core/src/arrayobject.c59
1 files changed, 35 insertions, 24 deletions
diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c
index ba47ffd19..2e2f6021b 100644
--- a/numpy/core/src/arrayobject.c
+++ b/numpy/core/src/arrayobject.c
@@ -5265,6 +5265,32 @@ _array_small_type(PyArray_Descr *chktype, PyArray_Descr* mintype)
return outtype;
}
+static PyArray_Descr *
+_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);
+ }
+ return NULL;
+}
+
/* op is an object to be converted to an ndarray.
minitype is the minimum type-descriptor needed.
@@ -5299,6 +5325,11 @@ _array_find_type(PyObject *op, PyArray_Descr *minitype, int max)
goto finish;
}
+ chktype = _array_find_python_scalar_type(op);
+ if (chktype) {
+ goto finish;
+ }
+
if ((ip=PyObject_GetAttrString(op, "__array_typestr__"))!=NULL) {
if (PyString_Check(ip)) {
chktype =_array_typedescr_fromstr(PyString_AS_STRING(ip));
@@ -5390,29 +5421,6 @@ _array_find_type(PyObject *op, PyArray_Descr *minitype, int max)
goto finish;
}
- if (PyBool_Check(op)) {
- chktype = PyArray_DescrFromType(PyArray_BOOL);
- goto finish;
- }
- else if (PyInt_Check(op)) {
- chktype = PyArray_DescrFromType(PyArray_LONG);
- goto finish;
- } else if (PyLong_Check(op)) {
- /* if integer can fit into a longlong then return that
- */
- if ((PyLong_AsLongLong(op) == -1) && PyErr_Occurred()) {
- PyErr_Clear();
- goto deflt;
- }
- chktype = PyArray_DescrFromType(PyArray_LONGLONG);
- goto finish;
- } else if (PyFloat_Check(op)) {
- chktype = PyArray_DescrFromType(PyArray_DOUBLE);
- goto finish;
- } else if (PyComplex_Check(op)) {
- chktype = PyArray_DescrFromType(PyArray_CDOUBLE);
- goto finish;
- }
deflt:
chktype = PyArray_DescrFromType(PyArray_OBJECT);
@@ -6286,7 +6294,10 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth,
r = PyArray_FromArray((PyArrayObject *)op, newtype, flags);
else if (PyArray_IsScalar(op, Generic)) {
r = PyArray_FromScalar(op, newtype);
- }
+ } else if (newtype == NULL &&
+ (newtype = _array_find_python_scalar_type(op))) {
+ r = Array_FromScalar(op, newtype);
+ }
else if (((r = PyArray_FromStructInterface(op))!=Py_NotImplemented)|| \
((r = PyArray_FromInterface(op)) != Py_NotImplemented) || \
((r = PyArray_FromArrayAttr(op, newtype, context)) \