Author: mattip <[email protected]>
Branch: ndarray-subtype
Changeset: r65202:02610cc6e8f6
Date: 2013-07-05 17:58 +0300
http://bitbucket.org/pypy/pypy/changeset/02610cc6e8f6/
Log: fill out tests, try to be consistent about w_ naming
diff --git a/pypy/module/micronumpy/base.py b/pypy/module/micronumpy/base.py
--- a/pypy/module/micronumpy/base.py
+++ b/pypy/module/micronumpy/base.py
@@ -31,7 +31,7 @@
self.implementation = implementation
@staticmethod
- def from_shape(space, shape, dtype, order='C', subtype=None, is_new=False):
+ def from_shape(space, shape, dtype, order='C', w_subtype=None):
from pypy.module.micronumpy.arrayimpl import concrete, scalar
if not shape:
@@ -40,33 +40,17 @@
strides, backstrides = calc_strides(shape, dtype.base, order)
impl = concrete.ConcreteArray(shape, dtype.base, order, strides,
backstrides)
- if subtype:
- if space.isinstance_w(subtype, space.w_type):
- #got type, either from __new__ or from view casting
- ret = space.allocate_instance(W_NDimArray, subtype)
- W_NDimArray.__init__(ret, impl)
- if is_new:
- space.call_function(space.getattr(ret,
- space.wrap('__array_finalize__')),
- space.w_None)
- else:
- # view casting, call finalize
- space.call_function(space.getattr(ret,
- space.wrap('__array_finalize__')),
- subtype)
- else:
- #got instance
- ret = space.allocate_instance(W_NDimArray, space.type(subtype))
- W_NDimArray.__init__(ret, impl)
- space.call_function(space.getattr(ret,
- space.wrap('__array_finalize__')),
- subtype)
+ if w_subtype:
+ ret = space.allocate_instance(W_NDimArray, space.type(w_subtype))
+ W_NDimArray.__init__(ret, impl)
+ space.call_function(space.getattr(ret,
+ space.wrap('__array_finalize__')), w_subtype)
else:
ret = W_NDimArray(impl)
return ret
@staticmethod
- def from_shape_and_storage(space, shape, storage, dtype, order='C',
owning=False, subtype=None):
+ def from_shape_and_storage(space, shape, storage, dtype, order='C',
owning=False, w_subtype=None):
from pypy.module.micronumpy.arrayimpl import concrete
assert shape
strides, backstrides = calc_strides(shape, dtype, order)
@@ -77,21 +61,11 @@
else:
impl = concrete.ConcreteArrayNotOwning(shape, dtype, order,
strides,
backstrides, storage)
- if subtype:
- if space.isinstance_w(subtype, space.w_type):
- #got type, probably from descr_XXX
- ret = space.allocate_instance(W_NDimArray, subtype)
- W_NDimArray.__init__(ret, impl)
- space.call_function(space.getattr(ret,
- space.wrap('__array_finalize__')),
- space.w_None)
- else:
- #got instance
- ret = space.allocate_instance(W_NDimArray, space.type(subtype))
- W_NDimArray.__init__(ret, impl)
- space.call_function(space.getattr(ret,
- space.wrap('__array_finalize__')),
- subtype)
+ if w_subtype:
+ ret = space.allocate_instance(W_NDimArray, space.type(w_subtype))
+ W_NDimArray.__init__(ret, impl)
+ space.call_function(space.getattr(ret,
+ space.wrap('__array_finalize__')), w_subtype)
return ret
return W_NDimArray(impl)
diff --git a/pypy/module/micronumpy/interp_arrayops.py
b/pypy/module/micronumpy/interp_arrayops.py
--- a/pypy/module/micronumpy/interp_arrayops.py
+++ b/pypy/module/micronumpy/interp_arrayops.py
@@ -88,7 +88,7 @@
y.get_dtype())
shape = shape_agreement(space, arr.get_shape(), x)
shape = shape_agreement(space, shape, y)
- out = W_NDimArray.from_shape(space, shape, dtype, subtype=arr)
+ out = W_NDimArray.from_shape(space, shape, dtype, w_subtype=arr)
return loop.where(out, shape, arr, x, y, dtype)
def dot(space, w_obj1, w_obj2):
@@ -148,25 +148,25 @@
def repeat(space, w_arr, repeats, w_axis):
arr = convert_to_array(space, w_arr)
if space.is_none(w_axis):
- arr = arr.descr_flatten(space)
- orig_size = arr.get_shape()[0]
- shape = [arr.get_shape()[0] * repeats]
- res = W_NDimArray.from_shape(space, shape, arr.get_dtype(),
subtype=arr)
+ w_arr = arr.descr_flatten(space)
+ orig_size = w_arr.get_shape()[0]
+ shape = [w_arr.get_shape()[0] * repeats]
+ w_res = W_NDimArray.from_shape(space, shape, arr.get_dtype(),
w_subtype=w_arr)
for i in range(repeats):
Chunks([Chunk(i, shape[0] - repeats + i, repeats,
- orig_size)]).apply(space, res).implementation.setslice(space,
arr)
+ orig_size)]).apply(space,
w_res).implementation.setslice(space, w_arr)
else:
axis = space.int_w(w_axis)
- shape = arr.get_shape()[:]
+ shape = w_arr.get_shape()[:]
chunks = [Chunk(0, i, 1, i) for i in shape]
orig_size = shape[axis]
shape[axis] *= repeats
- res = W_NDimArray.from_shape(space, shape, arr.get_dtype(),
subtype=arr)
+ w_res = W_NDimArray.from_shape(space, shape, arr.get_dtype(),
subtype=w_arr)
for i in range(repeats):
chunks[axis] = Chunk(i, shape[axis] - repeats + i, repeats,
orig_size)
- Chunks(chunks).apply(space, res).implementation.setslice(space,
arr)
- return res
+ Chunks(chunks).apply(space, w_res).implementation.setslice(space,
w_arr)
+ return w_res
def count_nonzero(space, w_obj):
return space.wrap(loop.count_all_true(convert_to_array(space, w_obj)))
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
@@ -85,8 +85,8 @@
res_shape = [size] + self.get_shape()[1:]
else:
res_shape = [size]
- res = W_NDimArray.from_shape(space, res_shape, self.get_dtype(),
subtype=self)
- return loop.getitem_filter(res, self, arr)
+ w_res = W_NDimArray.from_shape(space, res_shape, self.get_dtype(),
w_subtype=self)
+ return loop.getitem_filter(w_res, self, arr)
def setitem_filter(self, space, idx, val):
if len(idx.get_shape()) > 1 and idx.get_shape() != self.get_shape():
@@ -481,9 +481,9 @@
loop.byteswap(self.implementation, self.implementation)
return self
else:
- res = W_NDimArray.from_shape(space, self.get_shape(),
self.get_dtype(), subtype=self)
- loop.byteswap(self.implementation, res.implementation)
- return res
+ w_res = W_NDimArray.from_shape(space, self.get_shape(),
self.get_dtype(), w_subtype=self)
+ loop.byteswap(self.implementation, w_res.implementation)
+ return w_res
@unwrap_spec(mode=str)
def descr_choose(self, space, w_choices, w_out=None, mode='raise'):
@@ -775,9 +775,9 @@
return W_NDimArray.new_scalar(space, dtype, space.wrap(0))
# Do the dims match?
out_shape, other_critical_dim = match_dot_shapes(space, self, other)
- result = W_NDimArray.from_shape(space, out_shape, dtype, subtype=self)
+ w_res = W_NDimArray.from_shape(space, out_shape, dtype, w_subtype=self)
# This is the place to add fpypy and blas
- return loop.multidim_dot(space, self, other, result, dtype,
+ return loop.multidim_dot(space, self, other, w_res, dtype,
other_critical_dim)
@unwrap_spec(w_axis = WrappedDefault(None))
@@ -921,8 +921,8 @@
return W_NDimArray.new_scalar(space, dtype)
if space.is_w(w_subtype, space.gettypefor(W_NDimArray)):
return W_NDimArray.from_shape(space, shape, dtype, order)
- ret = W_NDimArray.from_shape(space, shape, dtype, order, w_subtype,
is_new=True)
- return ret
+ raise OperationError(space.w_TypeError, space.wrap(
+ "__new__ is not meant to be called except with a ndarray"))
@unwrap_spec(addr=int)
def descr__from_shape_and_storage(space, w_cls, w_shape, addr, w_dtype,
w_subclass=None):
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
@@ -208,7 +208,7 @@
)
dtype = out.get_dtype()
else:
- out = W_NDimArray.from_shape(space, shape, dtype, subtype=obj)
+ out = W_NDimArray.from_shape(space, shape, dtype,
w_subtype=obj)
return loop.do_axis_reduce(shape, self.func, obj, dtype, axis, out,
self.identity, cumultative, temp)
if cumultative:
@@ -217,7 +217,7 @@
raise OperationError(space.w_ValueError, space.wrap(
"out of incompatible size"))
else:
- out = W_NDimArray.from_shape(space, [obj.get_size()], dtype,
subtype=obj)
+ out = W_NDimArray.from_shape(space, [obj.get_size()], dtype,
w_subtype=obj)
loop.compute_reduce_cumultative(obj, out, dtype, self.func,
self.identity)
return out
diff --git a/pypy/module/micronumpy/loop.py b/pypy/module/micronumpy/loop.py
--- a/pypy/module/micronumpy/loop.py
+++ b/pypy/module/micronumpy/loop.py
@@ -20,8 +20,9 @@
'left_iter', 'right_iter', 'out_iter'])
def call2(space, shape, func, calc_dtype, res_dtype, w_lhs, w_rhs, out):
+ # handle array_priority
if out is None:
- out = W_NDimArray.from_shape(space, shape, res_dtype)
+ out = W_NDimArray.from_shape(space, shape, res_dtype, w_subtype=w_lhs)
left_iter = w_lhs.create_iter(shape)
right_iter = w_rhs.create_iter(shape)
out_iter = out.create_iter(shape)
@@ -50,7 +51,7 @@
def call1(space, shape, func, calc_dtype, res_dtype, w_obj, out):
if out is None:
- out = W_NDimArray.from_shape(space, shape, res_dtype, subtype=w_obj)
+ out = W_NDimArray.from_shape(space, shape, res_dtype, w_subtype=w_obj)
obj_iter = w_obj.create_iter(shape)
out_iter = out.create_iter(shape)
shapelen = len(shape)
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
@@ -10,16 +10,18 @@
class NoNew(ndarray):
def __new__(cls, subtype):
raise ValueError('should not call __new__')
- def __array_finalize(self, obj):
+ def __array_finalize__(self, obj):
+
self.called_finalize = True
return NoNew ''')
cls.w_SubType = cls.space.appexec([], '''():
- from numpypy import ndarray
+ from numpypy import ndarray, asarray
class SubType(ndarray):
- def __new__(cls):
- cls.called_new = True
- return cls
- def __array_finalize(self, obj):
+ def __new__(obj, input_array):
+ obj = asarray(input_array).view(obj)
+ obj.called_new = True
+ return obj
+ def __array_finalize__(self, obj):
self.called_finalize = True
return SubType ''')
@@ -98,13 +100,25 @@
def test_sub_call2(self):
# c + a vs. a + c, what about array priority?
- assert False
+ from numpypy import array
+ a = array(range(12)).view(self.NoNew)
+ b = self.SubType(range(12))
+ c = b + a
+ assert isinstance(c, self.SubType)
+ c = a + b
+ assert isinstance(c, self.NoNew)
def test_sub_call1(self):
- assert False
+ from numpypy import array, sqrt
+ a = array(range(12)).view(self.NoNew)
+ b = sqrt(a)
+ assert b.called_finalize == True
def test_sub_astype(self):
- assert False
+ from numpypy import array
+ a = array(range(12)).view(self.NoNew)
+ b = a.astype(float)
+ assert b.called_finalize == True
def test_sub_reshape(self):
from numpypy import array
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit