Author: mattip <[email protected]>
Branch: numpy-fixes
Changeset: r77150:17474e40f4dc
Date: 2015-05-04 23:00 +0300
http://bitbucket.org/pypy/pypy/changeset/17474e40f4dc/

Log:    add __array_priority__ which should determine the return type for
        ufuncs

diff --git a/pypy/module/micronumpy/boxes.py b/pypy/module/micronumpy/boxes.py
--- a/pypy/module/micronumpy/boxes.py
+++ b/pypy/module/micronumpy/boxes.py
@@ -197,6 +197,9 @@
     def descr_hash(self, space):
         return space.hash(self.item(space))
 
+    def descr___array_priority__(self, space):
+        return space.wrap(0.0)
+
     def descr_index(self, space):
         return space.index(self.item(space))
 
@@ -680,6 +683,8 @@
 
     __hash__ = interp2app(W_GenericBox.descr_hash),
 
+    __array_priority__ = GetSetProperty(W_GenericBox.descr___array_priority__),
+
     tolist = interp2app(W_GenericBox.item),
     item = interp2app(W_GenericBox.descr_item),
     transpose = interp2app(W_GenericBox.descr_transpose),
diff --git a/pypy/module/micronumpy/compile.py 
b/pypy/module/micronumpy/compile.py
--- a/pypy/module/micronumpy/compile.py
+++ b/pypy/module/micronumpy/compile.py
@@ -203,6 +203,9 @@
         assert isinstance(w_obj, BoolObject)
         return bool(w_obj.intval)
 
+    def lt(self, w_lhs, w_rhs):
+        return BoolObject(self.int_w(w_lhs) < self.int_w(w_rhs))
+
     def is_w(self, w_obj, w_what):
         return w_obj is w_what
 
diff --git a/pypy/module/micronumpy/ndarray.py 
b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -569,6 +569,11 @@
     def fdel___pypy_data__(self, space):
         self.w_pypy_data = None
 
+    __array_priority__ = 0.0
+
+    def descr___array_priority__(self, space):
+        return space.wrap(self.__array_priority__)
+
     def descr_argsort(self, space, w_axis=None, w_kind=None, w_order=None):
         # happily ignore the kind
         # create a contiguous copy of the array
@@ -934,7 +939,8 @@
             try:
                 return ufunc(self, space, w_other, w_out)
             except OperationError, e:
-                if e.match(space, space.w_ValueError):
+                if e.match(space, space.w_ValueError) and \
+                   'operands could not be broadcast together' in 
str(e.get_w_value(space)):
                     return space.w_False
                 raise e
 
@@ -1506,6 +1512,7 @@
     __array_finalize__ = interp2app(W_NDimArray.descr___array_finalize__),
     __array_prepare__ = interp2app(W_NDimArray.descr___array_prepare__),
     __array_wrap__ = interp2app(W_NDimArray.descr___array_wrap__),
+    __array_priority__ = GetSetProperty(W_NDimArray.descr___array_priority__),
     __array__         = interp2app(W_NDimArray.descr___array__),
 )
 
diff --git a/pypy/module/micronumpy/test/test_subtype.py 
b/pypy/module/micronumpy/test/test_subtype.py
--- a/pypy/module/micronumpy/test/test_subtype.py
+++ b/pypy/module/micronumpy/test/test_subtype.py
@@ -72,7 +72,7 @@
 
     def test_subtype_view(self):
         from numpy import ndarray, array
-        class matrix(ndarray, object):
+        class matrix(ndarray):
             def __new__(subtype, data, dtype=None, copy=True):
                 if isinstance(data, matrix):
                     return data
@@ -80,6 +80,7 @@
         a = array(range(5))
         b = matrix(a)
         assert isinstance(b, matrix)
+        assert b.__array_priority__ == 0.0
         assert (b == a).all()
         a = array(5)[()]
         for s in [matrix, ndarray]:
@@ -96,6 +97,7 @@
         import numpy as np
         arr = np.array([1,2,3])
         ret = np.ndarray.__new__(np.ndarray, arr.shape, arr.dtype, buffer=arr)
+        assert ret.__array_priority__ == 0.0
         assert (arr == ret).all()
 
     def test_finalize(self):
@@ -281,7 +283,11 @@
 
     def test_array_of_subtype(self):
         import numpy as N
-        # numpy's matrix class caused an infinite loop
+        # this part of numpy's matrix class causes an infinite loop
+        # on cpython
+        import sys
+        if '__pypy__' not in sys.builtin_module_names:
+            skip('does not pass on cpython')
         class matrix(N.ndarray):
             def __new__(subtype, data, dtype=None, copy=True):
                 print('matrix __new__')
@@ -331,7 +337,7 @@
                 return ret
 
             def __array_finalize__(self, obj):
-                print('matrix __array_finalize__')
+                print('matrix __array_finalize__',obj)
                 self._getitem = False
                 if (isinstance(obj, matrix) and obj._getitem): return
                 ndim = self.ndim
@@ -354,7 +360,7 @@
                 return
 
             def __getitem__(self, index):
-                print('matrix __getitem__')
+                print('matrix __getitem__',index)
                 self._getitem = True
 
                 try:
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to