diff options
-rw-r--r-- | numpy/core/src/scalarmathmodule.c.src | 44 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 13 |
2 files changed, 52 insertions, 5 deletions
diff --git a/numpy/core/src/scalarmathmodule.c.src b/numpy/core/src/scalarmathmodule.c.src index 99182d83f..f3ee6724e 100644 --- a/numpy/core/src/scalarmathmodule.c.src +++ b/numpy/core/src/scalarmathmodule.c.src @@ -973,11 +973,35 @@ NONZERO_NAME(@name@_,)(PyObject *a) } /**end repeat**/ + +static void +emit_complexwarning() +{ + static PyObject *cls = NULL; + if (cls == NULL) { + PyObject *mod; + mod = PyImport_ImportModule("numpy.core"); + assert(mod != NULL); + cls = PyObject_GetAttrString(mod, "ComplexWarning"); + assert(cls != NULL); + Py_DECREF(mod); + } +#if PY_VERSION_HEX >= 0x02050000 + PyErr_WarnEx(cls, + "Casting complex values to real discards the imaginary " + "part", 0); +#else + PyErr_Warn(cls, + "Casting complex values to real discards the imaginary " + "part"); +#endif +} + /**begin repeat * * #name=byte,ubyte,short,ushort,int,uint,long,ulong,longlong,ulonglong,float,double,longdouble,cfloat,cdouble,clongdouble# * #Name=Byte,UByte,Short,UShort,Int,UInt,Long,ULong,LongLong,ULongLong,Float,Double,LongDouble,CFloat,CDouble,CLongDouble# - * #cmplx=,,,,,,,,,,,,,.real,.real,.real# + * #cmplx=0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1# * #sign=(signed,unsigned)*5,,,,,,# * #unsigntyp=0,1,0,1,0,1,0,1,0,1,1*6# * #ctype=long*8,PY_LONG_LONG*2,double*6# @@ -987,12 +1011,19 @@ NONZERO_NAME(@name@_,)(PyObject *a) static PyObject * @name@_int(PyObject *obj) { - @sign@ @ctype@ x= PyArrayScalar_VAL(obj, @Name@)@cmplx@; +#if @cmplx@ + @sign@ @ctype@ x= PyArrayScalar_VAL(obj, @Name@).real; +#else + @sign@ @ctype@ x= PyArrayScalar_VAL(obj, @Name@); +#endif #if @realtyp@ double ix; modf(x, &ix); x = ix; #endif +#if @cmplx@ + emit_complexwarning(); +#endif /* * For unsigned type, the (@ctype@) cast just does what is implicitely done by @@ -1013,14 +1044,19 @@ static PyObject * * * #name=(byte,ubyte,short,ushort,int,uint,long,ulong,longlong,ulonglong,float,double,longdouble,cfloat,cdouble,clongdouble)*2# * #Name=(Byte,UByte,Short,UShort,Int,UInt,Long,ULong,LongLong,ULongLong,Float,Double,LongDouble,CFloat,CDouble,CLongDouble)*2# - * #cmplx=(,,,,,,,,,,,,,.real,.real,.real)*2# + * #cmplx=(0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1)*2# * #which=long*16,float*16# * #func=(PyLong_FromLongLong, PyLong_FromUnsignedLongLong)*5,PyLong_FromDouble*6,PyFloat_FromDouble*16# */ static PyObject * @name@_@which@(PyObject *obj) { - return @func@((PyArrayScalar_VAL(obj, @Name@))@cmplx@); +#if @cmplx@ + emit_complexwarning(); + return @func@((PyArrayScalar_VAL(obj, @Name@)).real); +#else + return @func@((PyArrayScalar_VAL(obj, @Name@))); +#endif } /**end repeat**/ diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index d9c43b496..9375ab7e4 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -5,8 +5,9 @@ import gc import copy from os import path from numpy.testing import * -from numpy.testing.utils import _assert_valid_refcount +from numpy.testing.utils import _assert_valid_refcount, WarningManager from numpy.compat import asbytes, asunicode, asbytes_nested +import warnings import tempfile import numpy as np @@ -1400,5 +1401,15 @@ class TestRegression(TestCase): f.close() + def test_complex_scalar_warning(self): + for tp in [np.csingle, np.cdouble, np.clongdouble]: + x = tp(1+2j) + assert_warns(np.ComplexWarning, float, x) + ctx = WarningManager() + ctx.__enter__() + warnings.simplefilter('ignore') + assert_equal(float(x), float(x.real)) + ctx.__exit__() + if __name__ == "__main__": run_module_suite() |