diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-01-29 08:40:39 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-01-29 08:40:39 +0000 |
commit | 2438f98798fa6cf0443433d879365cd2121b6b5e (patch) | |
tree | 3e7305a14290e7df9aea8cb666df01b83463a0ef | |
parent | c0d68f47a04c22d1e9de64b8d09d43fb1b72fc63 (diff) | |
download | numpy-2438f98798fa6cf0443433d879365cd2121b6b5e.tar.gz |
Convert to arrays in piecewise. Improve error-handling of object-array conversions.
-rw-r--r-- | numpy/core/src/arraytypes.inc.src | 46 | ||||
-rw-r--r-- | numpy/lib/function_base.py | 3 |
2 files changed, 22 insertions, 27 deletions
diff --git a/numpy/core/src/arraytypes.inc.src b/numpy/core/src/arraytypes.inc.src index 96d2386e8..7f04facd3 100644 --- a/numpy/core/src/arraytypes.inc.src +++ b/numpy/core/src/arraytypes.inc.src @@ -1,34 +1,28 @@ /* -*- c -*- */ -static ulong -MyPyLong_AsUnsignedLong(PyObject *vv) +/**begin repeat +#name=UnsignedLong,UnsignedLongLong,LongLong# +#type=ulong,ulonglong,longlong# + */ +static @type@ +MyPyLong_As@name@(PyObject *vv) { - if ((vv != NULL) && PyInt_Check(vv)) { - long val = PyInt_AsLong(vv); - if (val < 0) { - PyErr_SetString(PyExc_OverflowError, - "can't convert negative value to unsigned long"); - return (ulong) -1; - } - return val; - } - return PyLong_AsUnsignedLong(vv); -} + @type@ ret; -static ulonglong -MyPyLong_AsUnsignedLongLong(PyObject *vv) -{ - if ((vv != NULL) && PyInt_Check(vv)) { - longlong val = PyInt_AsLong(vv); - if (val < 0) { - PyErr_SetString(PyExc_OverflowError, - "can't convert negative value to unsigned long"); - return (ulonglong) -1; - } - return val; + if (!PyLong_Check(vv)) { + PyObject *mylong; + mylong = PyNumber_Long(vv); + if (mylong == NULL) return (@type@) -1; + vv = mylong; } - return PyLong_AsUnsignedLongLong(vv); + else Py_INCREF(vv); + + ret = PyLong_As@name@(vv); + Py_DECREF(vv); + return ret; } +/**end repeat**/ + static double _getNAN(void) { @@ -67,7 +61,7 @@ MyPyFloat_AsDouble(PyObject *obj) #TYP=BOOL,BYTE,UBYTE,SHORT,USHORT,INT,LONG,UINT,ULONG,LONGLONG,ULONGLONG,FLOAT,DOUBLE# #func1=PyBool_FromLong, PyInt_FromLong*6, PyLong_FromUnsignedLong*2, PyLong_FromLongLong, PyLong_FromUnsignedLongLong, PyFloat_FromDouble*2# -#func2=PyObject_IsTrue, PyInt_AsLong*6, MyPyLong_AsUnsignedLong*2, PyLong_AsLongLong, MyPyLong_AsUnsignedLongLong, MyPyFloat_AsDouble*2# +#func2=PyObject_IsTrue, PyInt_AsLong*6, MyPyLong_AsUnsignedLong*2, MyPyLong_AsLongLong, MyPyLong_AsUnsignedLongLong, MyPyFloat_AsDouble*2# #typ=Bool, byte, ubyte, short, ushort, int, long, uint, ulong, longlong, ulonglong, float, double# #typ1=long*7, ulong*2, longlong, ulonglong, float, double# #kind=Bool, Byte, UByte, Short, UShort, Int, Long, UInt, ULong, LongLong, ULongLong, Float, Double# diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 7f87818b1..cee17b0aa 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -14,7 +14,7 @@ import types import math import numpy.core.numeric as _nx from numpy.core.numeric import ones, zeros, arange, concatenate, array, \ - asarray, empty, empty_like + asarray, empty, empty_like, asanyarray from numpy.core.numeric import ScalarType, dot, where, newaxis, isscalar from numpy.core.umath import pi, multiply, add, arctan2, maximum, minimum, \ frompyfunc, isnan, absolute, cos, less_equal, sqrt, sin, mod, exp @@ -202,6 +202,7 @@ def piecewise(x, condlist, funclist, *args, **kw): |-- """ + x = asanyarray(x) n2 = len(funclist) if not isinstance(condlist, type([])): condlist = [condlist] |