Author: Armin Rigo <ar...@tunes.org> Branch: Changeset: r50397:6fb87770b5d2 Date: 2011-12-11 21:57 +0100 http://bitbucket.org/pypy/pypy/changeset/6fb87770b5d2/
Log: merge heads diff --git a/pypy/module/micronumpy/interp_boxes.py b/pypy/module/micronumpy/interp_boxes.py --- a/pypy/module/micronumpy/interp_boxes.py +++ b/pypy/module/micronumpy/interp_boxes.py @@ -91,6 +91,9 @@ descr_neg = _unaryop_impl("negative") descr_abs = _unaryop_impl("absolute") + def descr_tolist(self, space): + return self.get_dtype(space).itemtype.to_builtin_type(space, self) + class W_BoolBox(W_GenericBox, PrimitiveBox): descr__new__, get_dtype = new_dtype_getter("bool") @@ -179,6 +182,8 @@ __neg__ = interp2app(W_GenericBox.descr_neg), __abs__ = interp2app(W_GenericBox.descr_abs), + + tolist = interp2app(W_GenericBox.descr_tolist), ) W_BoolBox.typedef = TypeDef("bool_", W_GenericBox.typedef, 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 @@ -876,6 +876,17 @@ arr.setshape(space, new_shape) return arr + def descr_tolist(self, space): + if len(self.shape) == 0: + assert isinstance(self, Scalar) + return self.value.descr_tolist(space) + w_result = space.newlist([]) + for i in range(self.shape[0]): + space.call_method(w_result, "append", + space.call_method(self.descr_getitem(space, space.wrap(i)), "tolist") + ) + return w_result + def descr_mean(self, space): return space.div(self.descr_sum(space), space.wrap(self.find_size())) @@ -1485,6 +1496,7 @@ copy = interp2app(BaseArray.descr_copy), reshape = interp2app(BaseArray.descr_reshape), + tolist = interp2app(BaseArray.descr_tolist), ) diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py --- a/pypy/module/micronumpy/test/test_numarray.py +++ b/pypy/module/micronumpy/test/test_numarray.py @@ -879,6 +879,45 @@ b[0] = 3 assert b.__debug_repr__() == 'Call2(add, forced=Array)' + def test_tolist_scalar(self): + from numpypy import int32, bool_ + x = int32(23) + assert x.tolist() == 23 + assert type(x.tolist()) is int + y = bool_(True) + assert y.tolist() is True + + def test_tolist_zerodim(self): + from numpypy import array + x = array(3) + assert x.tolist() == 3 + assert type(x.tolist()) is int + + def test_tolist_singledim(self): + from numpypy import array + a = array(range(5)) + assert a.tolist() == [0, 1, 2, 3, 4] + assert type(a.tolist()[0]) is int + b = array([0.2, 0.4, 0.6]) + assert b.tolist() == [0.2, 0.4, 0.6] + + def test_tolist_multidim(self): + from numpypy import array + a = array([[1, 2], [3, 4]]) + assert a.tolist() == [[1, 2], [3, 4]] + + def test_tolist_view(self): + from numpypy import array + a = array([[1,2],[3,4]]) + assert (a + a).tolist() == [[2, 4], [6, 8]] + + def test_tolist_slice(self): + from numpypy import array + a = array([[17.1, 27.2], [40.3, 50.3]]) + assert a[:,0].tolist() == [17.1, 40.3] + assert a[0].tolist() == [17.1, 27.2] + + class AppTestMultiDim(BaseNumpyAppTest): def test_init(self): import numpypy 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 @@ -78,6 +78,9 @@ w_obj.__init__(self._coerce(space, w_item).value) return w_obj + def to_builtin_type(self, space, box): + return space.wrap(self.for_computation(self.unbox(box))) + def _coerce(self, space, w_item): raise NotImplementedError @@ -180,6 +183,9 @@ def _coerce(self, space, w_item): return self.box(space.is_true(w_item)) + def to_builtin_type(self, space, w_item): + return space.wrap(self.unbox(w_item)) + def str_format(self, box): value = self.unbox(box) return "True" if value else "False" _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit