Author: Matti Picus <[email protected]>
Branch: pypy-pyarray
Changeset: r66924:87b7bca797bc
Date: 2013-09-12 21:07 +0300
http://bitbucket.org/pypy/pypy/changeset/87b7bca797bc/
Log: test, fix __array__ method
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
@@ -336,7 +336,7 @@
def descr_nonzero(self, space):
index_type = interp_dtype.get_dtype_cache(space).w_int64dtype
return self.implementation.nonzero(space, index_type)
-
+
def descr_tolist(self, space):
if len(self.get_shape()) == 0:
return self.get_scalar_value().item(space)
@@ -424,6 +424,9 @@
"non-int arg not supported"))
def descr___array__(self, space, w_dtype=None):
+ if not space.is_none(w_dtype):
+ raise OperationError(space.w_NotImplementedError, space.wrap(
+ "__array__(dtype) not implemented"))
# stub implementation of __array__()
return self
@@ -1123,12 +1126,14 @@
if not isinstance(w_object, W_NDimArray):
w___array__ = space.lookup(w_object, "__array__")
if w___array__ is not None:
+ if space.is_none(w_dtype):
+ w_dtype = space.w_None
w_array = space.get_and_call_function(w___array__, w_object,
w_dtype)
if isinstance(w_array, W_NDimArray):
# feed w_array back into array() for other properties
return array(space, w_array, w_dtype, False, w_order, subok,
ndmin)
else:
- raise operationerrfmt(space.w_ValueError,
+ raise operationerrfmt(space.w_ValueError,
"object __array__ method not producing an array")
# scalars and strings w/o __array__ method
@@ -1136,10 +1141,10 @@
if not issequence_w(space, w_object) or isstr:
if space.is_none(w_dtype) or isstr:
w_dtype = interp_ufuncs.find_dtype_for_scalar(space, w_object)
- dtype = space.interp_w(interp_dtype.W_Dtype,
+ dtype = space.interp_w(interp_dtype.W_Dtype,
space.call_function(space.gettypefor(interp_dtype.W_Dtype),
w_dtype))
return W_NDimArray.new_scalar(space, dtype, w_object)
-
+
if space.is_none(w_order):
order = 'C'
else:
@@ -1165,7 +1170,7 @@
w_ret.implementation = w_ret.implementation.set_shape(space,
w_ret, shape)
return w_ret
-
+
# not an array or incorrect dtype
shape, elems_w = find_shape_and_elems(space, w_object, dtype)
if dtype is None or (
diff --git a/pypy/module/micronumpy/test/test_base.py
b/pypy/module/micronumpy/test/test_base.py
--- a/pypy/module/micronumpy/test/test_base.py
+++ b/pypy/module/micronumpy/test/test_base.py
@@ -11,10 +11,13 @@
@classmethod
def setup_class(cls):
+ isNumpy = False
if option.runappdirect:
if '__pypy__' not in sys.builtin_module_names:
import numpy
sys.modules['numpypy'] = numpy
+ isNumpy = True
+ cls.w_isNumpy = cls.space.wrap(isNumpy)
cls.w_non_native_prefix = cls.space.wrap(nonnative_byteorder_prefix)
cls.w_native_prefix = cls.space.wrap(byteorder_prefix)
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
@@ -221,3 +221,26 @@
b = a.reshape(3, 4)
assert b.called_finalize == True
+ def test___array__(self):
+ from numpypy import ndarray, array, dtype
+ class D(ndarray):
+ def __new__(subtype, shape, dtype):
+ self = ndarray.__new__(subtype, shape, dtype)
+ self.id = 'subtype'
+ return self
+ class C(object):
+ def __init__(self, val, dtype):
+ self.val = val
+ self.dtype = dtype
+ def __array__(self, dtype=None):
+ retVal = D(self.val, dtype)
+ return retVal
+
+ a = C([2, 2], int)
+ b = array(a)
+ assert b.shape == (2, 2)
+ if not self.isNumpy:
+ assert b.id == 'subtype'
+ assert isinstance(b, D)
+ c = array(a, float)
+ assert c.dtype is dtype(float)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit