[pypy-commit] pypy default: .any() and .all() are available on all numpy types, not only bools

antocuni noreply at buildbot.pypy.org
Fri Mar 22 13:51:59 CET 2013


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: 
Changeset: r62657:938e96a52c5e
Date: 2013-03-22 13:45 +0100
http://bitbucket.org/pypy/pypy/changeset/938e96a52c5e/

Log:	.any() and .all() are available on all numpy types, not only bools

diff --git a/pypy/module/micronumpy/interp_boxes.py b/pypy/module/micronumpy/interp_boxes.py
--- a/pypy/module/micronumpy/interp_boxes.py
+++ b/pypy/module/micronumpy/interp_boxes.py
@@ -172,15 +172,17 @@
     def item(self, space):
         return self.get_dtype(space).itemtype.to_builtin_type(space, self)
 
+    def descr_any(self, space):
+        value = space.is_true(self)
+        return space.wrap(W_BoolBox(value))
+
+    def descr_all(self, space):
+        value = space.is_true(self)
+        return space.wrap(W_BoolBox(value))
+
 class W_BoolBox(W_GenericBox, PrimitiveBox):
     descr__new__, _get_dtype = new_dtype_getter("bool")
 
-    def descr_any(self, space):
-        return self
-
-    def descr_all(self, space):
-        return self
-
 class W_NumberBox(W_GenericBox):
     _attrs_ = ()
 
@@ -423,14 +425,14 @@
     __hash__ = interp2app(W_GenericBox.descr_hash),
 
     tolist = interp2app(W_GenericBox.item),
+    any = interp2app(W_GenericBox.descr_any),
+    all = interp2app(W_GenericBox.descr_all),
 )
 
 W_BoolBox.typedef = TypeDef("bool_", W_GenericBox.typedef,
     __module__ = "numpypy",
     __new__ = interp2app(W_BoolBox.descr__new__.im_func),
     __index__ = interp2app(descr_index),
-    any = interp2app(W_BoolBox.descr_any),
-    all = interp2app(W_BoolBox.descr_all),
 )
 
 W_NumberBox.typedef = TypeDef("number", W_GenericBox.typedef,
diff --git a/pypy/module/micronumpy/test/test_dtypes.py b/pypy/module/micronumpy/test/test_dtypes.py
--- a/pypy/module/micronumpy/test/test_dtypes.py
+++ b/pypy/module/micronumpy/test/test_dtypes.py
@@ -335,15 +335,6 @@
         assert X(True) is numpy.True_
         assert numpy.bool_("False") is numpy.True_
 
-    def test_bool_any_all(self):
-        import numpypy as numpy
-        x = numpy.bool_(True)
-        assert x.any()
-        assert x.all()
-        x = numpy.bool_(False)
-        assert not x.any()
-        assert not x.all()
-
     def test_int8(self):
         import numpypy as numpy
 
@@ -690,6 +681,32 @@
         from numpypy import dtype
         assert dtype('i4').alignment == 4
 
+    def test_any_all(self):
+        import numpypy as numpy
+        x = numpy.bool_(True)
+        assert x.any()
+        assert x.all()
+        x = numpy.bool_(False)
+        assert not x.any()
+        assert not x.all()
+        #
+        x = numpy.float64(0)
+        assert not x.any()
+        assert not x.all()
+        assert isinstance(x.any(), numpy.bool_)
+
+    def test_ravel(self):
+        from numpypy import float64, int8, array
+        x = float64(42.5).ravel()
+        assert x.dtype == float64
+        assert (x == array([42.5])).all()
+        #
+        x = int8(42).ravel()
+        assert x.dtype == int8
+        assert (x == array(42)).all()
+        
+
+
 class AppTestStrUnicodeDtypes(BaseNumpyAppTest):
     def test_str_unicode(self):
         from numpypy import str_, unicode_, character, flexible, generic


More information about the pypy-commit mailing list