Commits

Brian Kearns committed 271637e

provide bool() for complex types, test

Comments (0)

Files changed (2)

pypy/module/micronumpy/test/test_dtypes.py

         assert dtype('i4').isnative == True
         assert dtype('>i8').isnative == False
 
-    def test_any_all(self):
+    def test_any_all_nonzero(self):
         import numpypy as numpy
         x = numpy.bool_(True)
         assert x.any()
         assert x.all()
+        assert x.__nonzero__()
+        assert isinstance(x.any(), numpy.bool_)
+        assert isinstance(x.__nonzero__(), bool)
         x = numpy.bool_(False)
         assert not x.any()
         assert not x.all()
+        assert not x.__nonzero__()
         assert isinstance(x.any(), numpy.bool_)
+        assert isinstance(x.__nonzero__(), bool)
         x = numpy.float64(0)
         assert not x.any()
         assert not x.all()
+        assert not x.__nonzero__()
         assert isinstance(x.any(), numpy.float64)
+        assert isinstance(x.__nonzero__(), bool)
+        x = numpy.complex128(0)
+        assert not x.any()
+        assert not x.all()
+        assert not x.__nonzero__()
+        assert isinstance(x.any(), numpy.complex128)
+        assert isinstance(x.__nonzero__(), bool)
+        x = numpy.complex128(0+1j)
+        assert x.any()
+        assert x.all()
+        assert x.__nonzero__()
+        assert isinstance(x.any(), numpy.complex128)
+        assert isinstance(x.__nonzero__(), bool)
 
     def test_ravel(self):
         from numpypy import float64, int8, array
         assert (x == array(42)).all()
 
 
-
 class AppTestStrUnicodeDtypes(BaseNumpyAppTest):
     def test_str_unicode(self):
         skip('numpypy differs from numpy')

pypy/module/micronumpy/types.py

         return v
 
     def to_builtin_type(self, space, box):
-        real,imag = self.for_computation(self.unbox(box))
+        real, imag = self.for_computation(self.unbox(box))
         return space.newcomplex(real, imag)
 
+    def bool(self, v):
+        real, imag = self.for_computation(self.unbox(v))
+        return bool(real) or bool(imag)
+
     def read_bool(self, arr, i, offset):
         v = self.for_computation(self._read(arr.storage, i, offset))
         return bool(v[0]) or bool(v[1])