Author: mattip Branch: numpypy-repr-fix Changeset: r50980:68dec1e17bd9 Date: 2012-01-02 22:46 +0200 http://bitbucket.org/pypy/pypy/changeset/68dec1e17bd9/
Log: add tests for issue 964 and more, make tests pass 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 @@ -409,7 +409,7 @@ def descr_repr(self, space): res = StringBuilder() res.append("array(") - concrete = self.get_concrete() + concrete = self.get_concrete_or_scalar() dtype = concrete.find_dtype() if not concrete.size: res.append('[]') @@ -421,9 +421,13 @@ res.append(')') else: concrete.to_str(space, 1, res, indent=' ') - if (dtype is not interp_dtype.get_dtype_cache(space).w_float64dtype and - dtype is not interp_dtype.get_dtype_cache(space).w_int64dtype) or \ - not self.size: + if (dtype is interp_dtype.get_dtype_cache(space).w_float64dtype or \ + dtype.kind == interp_dtype.SIGNEDLTR and \ + dtype.itemtype.get_element_size() == rffi.sizeof(lltype.Signed)) \ + and self.size: + # Do not print dtype + pass + else: res.append(", dtype=" + dtype.name) res.append(")") return space.wrap(res.build()) @@ -840,14 +844,17 @@ each line will begin with indent. ''' size = self.size + dtype = self.find_dtype() if size < 1: builder.append('[]') return + elif size == 1: + builder.append(dtype.itemtype.str_format(self.getitem(0))) + return if size > 1000: # Once this goes True it does not go back to False for recursive # calls use_ellipsis = True - dtype = self.find_dtype() ndims = len(self.shape) i = 0 start = True @@ -863,10 +870,9 @@ builder.append('\n' + indent) else: builder.append(indent) - # create_slice requires len(chunks) > 1 in order to reduce - # shape - view = self.create_slice([(i, 0, 0, 1), (0, self.shape[1], 1, self.shape[1])]).get_concrete() - view.to_str(space, comma, builder, indent=indent + ' ', use_ellipsis=use_ellipsis) + view = self.create_slice([(i, 0, 0, 1)]).get_concrete() + view.to_str(space, comma, builder, indent=indent + ' ', + use_ellipsis=use_ellipsis) builder.append('\n' + indent + '..., ') i = self.shape[0] - 3 while i < self.shape[0]: @@ -880,8 +886,9 @@ builder.append(indent) # create_slice requires len(chunks) > 1 in order to reduce # shape - view = self.create_slice([(i, 0, 0, 1), (0, self.shape[1], 1, self.shape[1])]).get_concrete() - view.to_str(space, comma, builder, indent=indent + ' ', use_ellipsis=use_ellipsis) + view = self.create_slice([(i, 0, 0, 1)]).get_concrete() + view.to_str(space, comma, builder, indent=indent + ' ', + use_ellipsis=use_ellipsis) i += 1 elif ndims == 1: spacer = ',' * comma + ' ' @@ -912,8 +919,6 @@ builder.append(dtype.itemtype.str_format(self.getitem(item))) item += self.strides[0] i += 1 - else: - builder.append('[') builder.append(']') @jit.unroll_safe 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 @@ -1347,6 +1347,7 @@ class AppTestRepr(BaseNumpyAppTest): def test_repr(self): from numpypy import array, zeros + intSize = array(5).dtype.itemsize a = array(range(5), float) assert repr(a) == "array([0.0, 1.0, 2.0, 3.0, 4.0])" a = array([], float) @@ -1354,11 +1355,23 @@ a = zeros(1001) assert repr(a) == "array([0.0, 0.0, 0.0, ..., 0.0, 0.0, 0.0])" a = array(range(5), long) - assert repr(a) == "array([0, 1, 2, 3, 4])" + if a.dtype.itemsize == intSize: + assert repr(a) == "array([0, 1, 2, 3, 4])" + else: + assert repr(a) == "array([0, 1, 2, 3, 4], dtype=int64)" + a = array(range(5), 'int32') + if a.dtype.itemsize == intSize: + assert repr(a) == "array([0, 1, 2, 3, 4])" + else: + assert repr(a) == "array([0, 1, 2, 3, 4], dtype=int32)" a = array([], long) assert repr(a) == "array([], dtype=int64)" a = array([True, False, True, False], "?") assert repr(a) == "array([True, False, True, False], dtype=bool)" + a = zeros([]) + assert repr(a) == "array(0.0)" + a = array(0.2) + assert repr(a) == "array(0.2)" def test_repr_multi(self): from numpypy import array, zeros _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit