Author: Maciej Fijalkowski <[email protected]>
Branch: numpy-back-to-applevel
Changeset: r51743:6a5df7612571
Date: 2012-01-24 21:45 +0200
http://bitbucket.org/pypy/pypy/changeset/6a5df7612571/
Log: inversion and don't swallow exceptions too much
diff --git a/lib_pypy/numpypy/core/arrayprint.py
b/lib_pypy/numpypy/core/arrayprint.py
--- a/lib_pypy/numpypy/core/arrayprint.py
+++ b/lib_pypy/numpypy/core/arrayprint.py
@@ -542,12 +542,12 @@
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
diff --git a/pypy/module/micronumpy/__init__.py
b/pypy/module/micronumpy/__init__.py
--- a/pypy/module/micronumpy/__init__.py
+++ b/pypy/module/micronumpy/__init__.py
@@ -94,6 +94,7 @@
("tan", "tan"),
('bitwise_and', 'bitwise_and'),
('bitwise_or', 'bitwise_or'),
+ ('bitwise_not', 'invert'),
('isnan', 'isnan'),
('isinf', 'isinf'),
]:
diff --git a/pypy/module/micronumpy/interp_dtype.py
b/pypy/module/micronumpy/interp_dtype.py
--- a/pypy/module/micronumpy/interp_dtype.py
+++ b/pypy/module/micronumpy/interp_dtype.py
@@ -90,7 +90,8 @@
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
diff --git a/pypy/module/micronumpy/interp_numarray.py
b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -100,6 +100,7 @@
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):
@@ -1277,6 +1278,7 @@
__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),
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
@@ -30,12 +30,14 @@
_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)
@@ -248,9 +250,10 @@
_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
@@ -284,10 +287,10 @@
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,
@@ -466,6 +469,7 @@
'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}),
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
@@ -377,6 +377,12 @@
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
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
@@ -189,7 +189,7 @@
@simple_binary_op
def min(self, v1, v2):
return min(v1, v2)
-
+
class Bool(BaseType, Primitive):
T = lltype.Bool
@@ -232,6 +232,20 @@
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
@@ -288,6 +302,10 @@
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
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit