[pypy-commit] pypy numpy-indexing-by-arrays-bool: A branch to merge what's there in indexing by arrays, the rest left for the

fijal noreply at buildbot.pypy.org
Fri Jan 20 15:17:23 CET 2012


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: numpy-indexing-by-arrays-bool
Changeset: r51522:8cc13fd6cd81
Date: 2012-01-20 16:16 +0200
http://bitbucket.org/pypy/pypy/changeset/8cc13fd6cd81/

Log:	A branch to merge what's there in indexing by arrays, the rest left
	for the future when I figure out.

	Fix translation

diff --git a/pypy/module/micronumpy/interp_iter.py b/pypy/module/micronumpy/interp_iter.py
--- a/pypy/module/micronumpy/interp_iter.py
+++ b/pypy/module/micronumpy/interp_iter.py
@@ -6,10 +6,7 @@
 
 # structures to describe slicing
 
-class BaseChunk(object):
-    pass
-
-class Chunk(BaseChunk):
+class Chunk(object):
     def __init__(self, start, stop, step, lgt):
         self.start = start
         self.stop = stop
@@ -20,14 +17,6 @@
         if self.step != 0:
             shape.append(self.lgt)
 
-class IntArrayChunk(BaseChunk):
-    def __init__(self, arr):
-        self.arr = arr.get_concrete()
-
-class BoolArrayChunk(BaseChunk):
-    def __init__(self, arr):
-        self.arr = arr.get_concrete()
-
 class BaseTransform(object):
     pass
 
diff --git a/pypy/module/micronumpy/interp_ufuncs.py b/pypy/module/micronumpy/interp_ufuncs.py
--- a/pypy/module/micronumpy/interp_ufuncs.py
+++ b/pypy/module/micronumpy/interp_ufuncs.py
@@ -249,15 +249,16 @@
 
 
 class W_Ufunc2(W_Ufunc):
-    _immutable_fields_ = ["comparison_func", "func", "name"]
+    _immutable_fields_ = ["comparison_func", "func", "name", "int_only"]
     argcount = 2
 
     def __init__(self, func, name, promote_to_float=False, promote_bools=False,
-        identity=None, comparison_func=False):
+        identity=None, comparison_func=False, int_only=False):
 
         W_Ufunc.__init__(self, name, promote_to_float, promote_bools, identity)
         self.func = func
         self.comparison_func = comparison_func
+        self.int_only = int_only
 
     def call(self, space, args_w):
         from pypy.module.micronumpy.interp_numarray import (Call2,
@@ -268,6 +269,7 @@
         w_rhs = convert_to_array(space, w_rhs)
         calc_dtype = find_binop_result_dtype(space,
             w_lhs.find_dtype(), w_rhs.find_dtype(),
+            int_only=self.int_only,
             promote_to_float=self.promote_to_float,
             promote_bools=self.promote_bools,
         )
@@ -304,10 +306,12 @@
 
 
 def find_binop_result_dtype(space, dt1, dt2, promote_to_float=False,
-    promote_bools=False):
+    promote_bools=False, int_only=False):
     # dt1.num should be <= dt2.num
     if dt1.num > dt2.num:
         dt1, dt2 = dt2, dt1
+    if int_only and (not dt1.is_int_type() or not dt2.is_int_type()):
+        raise OperationError(space.w_TypeError, space.wrap("Unsupported types"))
     # Some operations promote op(bool, bool) to return int8, rather than bool
     if promote_bools and (dt1.kind == dt2.kind == interp_dtype.BOOLLTR):
         return interp_dtype.get_dtype_cache(space).w_int8dtype
@@ -425,8 +429,10 @@
             ("add", "add", 2, {"identity": 0}),
             ("subtract", "sub", 2),
             ("multiply", "mul", 2, {"identity": 1}),
-            ("bitwise_and", "bitwise_and", 2, {"identity": 1}),
-            ("bitwise_or", "bitwise_or", 2, {"identity": 0}),
+            ("bitwise_and", "bitwise_and", 2, {"identity": 1,
+                                               'int_only': True}),
+            ("bitwise_or", "bitwise_or", 2, {"identity": 0,
+                                             'int_only': True}),
             ("divide", "div", 2, {"promote_bools": True}),
             ("mod", "mod", 2, {"promote_bools": True}),
             ("power", "pow", 2, {"promote_bools": True}),
@@ -477,7 +483,7 @@
         extra_kwargs["identity"] = identity
 
         func = ufunc_dtype_caller(space, ufunc_name, op_name, argcount,
-            comparison_func=extra_kwargs.get("comparison_func", False)
+            comparison_func=extra_kwargs.get("comparison_func", False),
         )
         if argcount == 1:
             ufunc = W_Ufunc1(func, ufunc_name, **extra_kwargs)
diff --git a/pypy/module/micronumpy/test/test_ufuncs.py b/pypy/module/micronumpy/test/test_ufuncs.py
--- a/pypy/module/micronumpy/test/test_ufuncs.py
+++ b/pypy/module/micronumpy/test/test_ufuncs.py
@@ -353,12 +353,13 @@
         assert (add.reduce(a, 1) == [6.0, 22.0, 38.0]).all()
 
     def test_bitwise(self):
-        from _numpypy import bitwise_and, bitwise_or, arange
+        from _numpypy import bitwise_and, bitwise_or, arange, array
         a = arange(6).reshape(2, 3)
         assert (a & 1 == [[0, 1, 0], [1, 0, 1]]).all()
         assert (a & 1 == bitwise_and(a, 1)).all()
         assert (a | 1 == [[1, 1, 3], [3, 5, 5]]).all()
         assert (a | 1 == bitwise_or(a, 1)).all()
+        raises(TypeError, 'array([1.0]) & 1')
 
     def test_comparisons(self):
         import operator
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -171,14 +171,6 @@
     @simple_binary_op
     def min(self, v1, v2):
         return min(v1, v2)
-
-    @simple_binary_op
-    def bitwise_and(self, v1, v2):
-        return v1 & v2
-
-    @simple_binary_op
-    def bitwise_or(self, v1, v2):
-        return v1 | v2
     
 
 class Bool(BaseType, Primitive):
@@ -270,6 +262,14 @@
             assert v == 0
             return 0
 
+    @simple_binary_op
+    def bitwise_and(self, v1, v2):
+        return v1 & v2
+
+    @simple_binary_op
+    def bitwise_or(self, v1, v2):
+        return v1 | v2
+
 class Int8(BaseType, Integer):
     T = rffi.SIGNEDCHAR
     BoxType = interp_boxes.W_Int8Box


More information about the pypy-commit mailing list