Commits

Maciej Fijalkowski  committed 6a5df76

inversion and don't swallow exceptions too much

  • Participants
  • Parent commits 838a815
  • Branches numpy-back-to-applevel

Comments (0)

Files changed (7)

File lib_pypy/numpypy/core/arrayprint.py

         self.exp_format = False
         self.large_exponent = False
         self.max_str_len = 0
-        try:
-            self.fillFormat(data)
-        except (TypeError, NotImplementedError):
+        #try:
+        self.fillFormat(data)
+        #except (TypeError, NotImplementedError):
             # if reduce(data) fails, this instance will not be called, just
             # instantiated in formatdict.
-            pass
+            #pass
 
     def fillFormat(self, data):
         import numeric as _nc

File pypy/module/micronumpy/__init__.py

         ("tan", "tan"),
         ('bitwise_and', 'bitwise_and'),
         ('bitwise_or', 'bitwise_or'),
+        ('bitwise_not', 'invert'),
         ('isnan', 'isnan'),
         ('isinf', 'isinf'),
     ]:

File pypy/module/micronumpy/interp_dtype.py

         return space.newtuple([])
 
     def is_int_type(self):
-        return self.kind == SIGNEDLTR or self.kind == UNSIGNEDLTR
+        return (self.kind == SIGNEDLTR or self.kind == UNSIGNEDLTR or
+                self.kind == BOOLLTR)
 
     def is_bool_type(self):
         return self.kind == BOOLLTR

File pypy/module/micronumpy/interp_numarray.py

     descr_pos = _unaryop_impl("positive")
     descr_neg = _unaryop_impl("negative")
     descr_abs = _unaryop_impl("absolute")
+    descr_invert = _unaryop_impl("invert")
 
     def _binop_impl(ufunc_name):
         def impl(self, space, w_other):
 
     __and__ = interp2app(BaseArray.descr_and),
     __or__ = interp2app(BaseArray.descr_or),
+    __invert__ = interp2app(BaseArray.descr_invert),
 
     __repr__ = interp2app(BaseArray.descr_repr),
     __str__ = interp2app(BaseArray.descr_str),

File pypy/module/micronumpy/interp_ufuncs.py

     _attrs_ = ["name", "promote_to_float", "promote_bools", "identity"]
     _immutable_fields_ = ["promote_to_float", "promote_bools", "name"]
 
-    def __init__(self, name, promote_to_float, promote_bools, identity):
+    def __init__(self, name, promote_to_float, promote_bools, identity,
+                 int_only):
         self.name = name
         self.promote_to_float = promote_to_float
         self.promote_bools = promote_bools
 
         self.identity = identity
+        self.int_only = int_only
 
     def descr_repr(self, space):
         return space.wrap("<ufunc '%s'>" % self.name)
     _immutable_fields_ = ["func", "name"]
 
     def __init__(self, func, name, promote_to_float=False, promote_bools=False,
-        identity=None, bool_result=False):
+        identity=None, bool_result=False, int_only=False):
 
-        W_Ufunc.__init__(self, name, promote_to_float, promote_bools, identity)
+        W_Ufunc.__init__(self, name, promote_to_float, promote_bools, identity,
+                         int_only)
         self.func = func
         self.bool_result = bool_result
 
     def __init__(self, func, name, promote_to_float=False, promote_bools=False,
         identity=None, comparison_func=False, int_only=False):
 
-        W_Ufunc.__init__(self, name, promote_to_float, promote_bools, identity)
+        W_Ufunc.__init__(self, name, promote_to_float, promote_bools, identity,
+                         int_only)
         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,
                                                'int_only': True}),
             ("bitwise_or", "bitwise_or", 2, {"identity": 0,
                                              'int_only': True}),
+            ("invert", "invert", 1, {"int_only": True}),
             ("divide", "div", 2, {"promote_bools": True}),
             ("true_divide", "div", 2, {"promote_to_float": True}),
             ("mod", "mod", 2, {"promote_bools": True}),

File pypy/module/micronumpy/test/test_ufuncs.py

         assert (a | 1 == bitwise_or(a, 1)).all()
         raises(TypeError, 'array([1.0]) & 1')
 
+    def test_unary_bitops(self):
+        from _numpypy import bitwise_not, array
+        a = array([1, 2, 3, 4])
+        assert (~a == [-2, -3, -4, -5]).all()
+        assert (bitwise_not(a) == ~a).all()
+
     def test_comparisons(self):
         import operator
         from _numpypy import equal, not_equal, less, less_equal, greater, greater_equal

File pypy/module/micronumpy/types.py

     @simple_binary_op
     def min(self, v1, v2):
         return min(v1, v2)
-    
+
 
 class Bool(BaseType, Primitive):
     T = lltype.Bool
     def default_fromstring(self, space):
         return self.box(False)
 
+    # XXX check rpythonization
+
+    @simple_binary_op
+    def bitwise_and(self, v1, v2):
+        return v1 & v2
+
+    @simple_binary_op
+    def bitwise_or(self, v1, v2):
+        return v1 | v2
+
+    @simple_unary_op
+    def invert(self, v):
+        return ~v
+
 class Integer(Primitive):
     _mixin_ = True
 
     def bitwise_or(self, v1, v2):
         return v1 | v2
 
+    @simple_unary_op
+    def invert(self, v):
+        return ~v
+
 class Int8(BaseType, Integer):
     T = rffi.SIGNEDCHAR
     BoxType = interp_boxes.W_Int8Box