Author: mattip <[email protected]>
Branch: object-dtype2
Changeset: r76762:48af64138cf8
Date: 2015-04-09 10:51 +0300
http://bitbucket.org/pypy/pypy/changeset/48af64138cf8/
Log: add tests that match -A behaviour, hack till they pass (cleanup
needed)
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
@@ -614,6 +614,8 @@
self.w_obj = w_obj
def convert_to(self, space, dtype):
+ if dtype.is_bool():
+ return W_BoolBox(space.bool_w(self.w_obj))
return self # XXX
def descr__getattr__(self, space, w_key):
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
@@ -202,11 +202,14 @@
return self
elif isinstance(w_idx, W_NDimArray) and w_idx.get_dtype().is_bool() \
and w_idx.ndims() > 0:
- return self.getitem_filter(space, w_idx)
+ w_res = self.getitem_filter(space, w_idx)
try:
- return self.implementation.descr_getitem(space, self, w_idx)
+ w_res = self.implementation.descr_getitem(space, self, w_idx)
except ArrayArgumentException:
- return self.getitem_array_int(space, w_idx)
+ w_res = self.getitem_array_int(space, w_idx)
+ if w_res.is_scalar() and w_res.get_dtype(space).is_object():
+ return w_res.get_dtype(space).itemtype.unbox(w_res)
+ return w_res
def getitem(self, space, index_list):
return self.implementation.getitem_index(space, index_list)
@@ -889,7 +892,10 @@
"The truth value of an array with more than one element "
"is ambiguous. Use a.any() or a.all()"))
iter, state = self.create_iter()
- return space.wrap(space.is_true(iter.getitem(state)))
+ w_val = iter.getitem(state)
+ if self.get_dtype().is_object():
+ w_val = self.get_dtype().itemtype.unbox(w_val)
+ return space.wrap(space.is_true(w_val))
def _binop_impl(ufunc_name):
def impl(self, space, w_other, w_out=None):
diff --git a/pypy/module/micronumpy/test/test_object_arrays.py
b/pypy/module/micronumpy/test/test_object_arrays.py
--- a/pypy/module/micronumpy/test/test_object_arrays.py
+++ b/pypy/module/micronumpy/test/test_object_arrays.py
@@ -33,16 +33,26 @@
a = np.array(["foo"], dtype=object)
b = a and complex(1, -1)
assert b == complex(1, -1)
- b = complex(1, -1) and a
+ b = np.array(complex(1, -1)) and a
assert (b == a).all()
def test_logical_ufunc(self):
import numpy as np
a = np.array(["foo"], dtype=object)
b = np.array([1], dtype=object)
- raises(TypeError, np.logical_and, a, 1)
- raises(TypeError, np.logical_and, b, complex(1, -1))
+ d = np.array([complex(1, 10)], dtype=object)
+ c = np.logical_and(a, 1)
+ assert c.dtype == np.dtype('object')
+ assert c == 1
+ c = np.logical_and(b, complex(1, -1))
+ assert c.dtype == np.dtype('object')
+ assert c == complex(1, -1)
+ c = np.logical_and(d, b)
+ assert c == 1
c = b & 1
+ assert c.dtype == np.dtype('object')
+ assert (c == 1).all()
+ c = np.array(1) & b
assert (c == b).all()
def test_reduce(self):
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
@@ -149,9 +149,8 @@
return self.box(array[0])
def unbox(self, box):
- if not isinstance(box, self.BoxType):
- # i.e. box is an ObjectBox
- raise oefmt(self.space.w_AttributeError, '')
+ if isinstance(box, ObjectType.BoxType):
+ return box.w_obj
return box.value
def coerce(self, space, dtype, w_item):
@@ -1701,12 +1700,15 @@
@specialize.argtype(1, 2)
def box_complex(self, real, imag):
+ if isinstance(real, rffi.r_singlefloat):
+ real = rffi.cast(rffi.DOUBLE, real)
+ if isinstance(imag, rffi.r_singlefloat):
+ imag = rffi.cast(rffi.DOUBLE, imag)
w_obj = self.space.newcomplex(real, imag)
return self.BoxType(w_obj)
def str_format(self, box):
- return 'Object as string'
- #return space.str_w(space.repr(self.unbox(box)))
+ return self.space.str_w(self.space.repr(self.unbox(box)))
def to_builtin_type(self, space, box):
assert isinstance(box, self.BoxType)
@@ -1716,7 +1718,7 @@
def for_computation(v):
return v
- @raw_binary_op
+ @simple_binary_op
def eq(self, v1, v2):
return self.space.eq_w(v1, v2)
@@ -1735,28 +1737,32 @@
def arctan2(self, v1, v2):
raise oefmt(self.space.w_AttributeError, 'arctan2')
- @raw_unary_op
+ @simple_unary_op
def bool(self,v):
- return not self.space.is_w(v, self.space.w_None) and \
- not self.space.eq_w(v, self.space.wrap(0)) and \
- not self.space.len_w(v) == 0
+ return self._bool(v)
def _bool(self, v):
- return self.space.bool_w(v)
+ if self.space.is_true(v):
+ return True
+ return False
- @raw_binary_op
+ @simple_binary_op
def logical_and(self, v1, v2):
- return self._bool(v1) and self._bool(v2)
+ if self._bool(v1):
+ return v2
+ return v1
- @raw_binary_op
+ @simple_binary_op
def logical_or(self, v1, v2):
- return self._bool(v1) or self._bool(v2)
+ if self._bool(v1):
+ return v1
+ return v2
- @raw_unary_op
+ @simple_unary_op
def logical_not(self, v):
return not self._bool(v)
- @raw_binary_op
+ @simple_binary_op
def logical_xor(self, v1, v2):
a = self._bool(v1)
b = self._bool(v2)
@@ -1808,7 +1814,7 @@
def func(self, w_v):
w_impl = self.space.lookup(w_v, op)
if w_impl is None:
- raise oefmt(self.space.w_AttributeError, op)
+ raise oefmt(self.space.w_AttributeError, 'unknown op "%s" on
object' % op)
return self.space.get_and_call_function(w_impl, w_v)
func.__name__ = 'object_' + op
setattr(cls, op, func)
diff --git a/pypy/module/micronumpy/ufuncs.py b/pypy/module/micronumpy/ufuncs.py
--- a/pypy/module/micronumpy/ufuncs.py
+++ b/pypy/module/micronumpy/ufuncs.py
@@ -378,6 +378,8 @@
w_val = self.func(calc_dtype,
w_obj.get_scalar_value().convert_to(space,
calc_dtype))
if out is None:
+ if res_dtype.is_object():
+ w_val = w_obj.get_scalar_value()
return w_val
w_val = res_dtype.coerce(space, w_val)
if out.is_scalar():
@@ -481,7 +483,7 @@
else:
out = w_out
calc_dtype = out.get_dtype()
- if self.comparison_func:
+ if self.comparison_func and not calc_dtype.is_object():
res_dtype = get_dtype_cache(space).w_booldtype
else:
res_dtype = calc_dtype
@@ -495,6 +497,8 @@
out.set_scalar_value(arr)
else:
out.fill(space, arr)
+ elif calc_dtype.is_object():
+ out = arr.get_scalar_value()
else:
out = arr
return out
@@ -1130,6 +1134,8 @@
def impl(res_dtype, lvalue, rvalue):
res = get_op(res_dtype)(lvalue, rvalue)
if comparison_func:
+ if res_dtype.is_object():
+ return res
return dtype_cache.w_booldtype.box(res)
return res
return func_with_new_name(impl, ufunc_name)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit