Author: Brian Kearns <[email protected]>
Branch:
Changeset: r69234:3e6219275db7
Date: 2014-02-21 00:17 -0500
http://bitbucket.org/pypy/pypy/changeset/3e6219275db7/
Log: fix some numpy str/repr cases
diff --git a/pypy/module/micronumpy/arrayimpl/scalar.py
b/pypy/module/micronumpy/arrayimpl/scalar.py
--- a/pypy/module/micronumpy/arrayimpl/scalar.py
+++ b/pypy/module/micronumpy/arrayimpl/scalar.py
@@ -106,10 +106,7 @@
scalar.value = self.value.convert_imag_to(scalar.dtype)
return scalar
scalar = Scalar(self.dtype)
- if self.dtype.is_flexible_type():
- scalar.value = self.value
- else:
- scalar.value = scalar.dtype.itemtype.box(0)
+ scalar.value = scalar.dtype.coerce(space, None)
return scalar
def set_imag(self, space, orig_array, w_val):
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
@@ -262,24 +262,30 @@
def descr_str(self, space):
cache = get_appbridge_cache(space)
if cache.w_array_str is None:
- return space.wrap(self.dump_data())
+ return space.wrap(self.dump_data(prefix='', separator='',
suffix=''))
return space.call_function(cache.w_array_str, self)
- def dump_data(self, prefix='array(', suffix=')'):
+ def dump_data(self, prefix='array(', separator=',', suffix=')'):
i = self.create_iter()
first = True
dtype = self.get_dtype()
s = StringBuilder()
s.append(prefix)
- s.append('[')
+ if not self.is_scalar():
+ s.append('[')
while not i.done():
if first:
first = False
else:
- s.append(', ')
- s.append(dtype.itemtype.str_format(i.getitem()))
+ s.append(separator)
+ s.append(' ')
+ if self.is_scalar() and dtype.is_str_type():
+ s.append(dtype.itemtype.to_str(i.getitem()))
+ else:
+ s.append(dtype.itemtype.str_format(i.getitem()))
i.next()
- s.append(']')
+ if not self.is_scalar():
+ s.append(']')
s.append(suffix)
return s.build()
diff --git a/pypy/module/micronumpy/test/test_complex.py
b/pypy/module/micronumpy/test/test_complex.py
--- a/pypy/module/micronumpy/test/test_complex.py
+++ b/pypy/module/micronumpy/test/test_complex.py
@@ -477,6 +477,7 @@
assert c[i] == max(a[i], b[i])
def test_basic(self):
+ import sys
from numpypy import (dtype, add, array, dtype,
subtract as sub, multiply, divide, negative, absolute as abs,
floor_divide, real, imag, sign)
@@ -507,9 +508,8 @@
assert str(exc.value) == \
"could not broadcast input array from shape (2) into shape ()"
a = array('abc')
- assert str(a.real) == str(a)
- # numpy imag for flexible types returns self
- assert str(a.imag) == str(a)
+ assert str(a.real) == 'abc'
+ assert str(a.imag) == ''
for t in 'complex64', 'complex128', 'clongdouble':
complex_ = dtype(t).type
O = complex(0, 0)
@@ -578,10 +578,14 @@
assert repr(abs(complex(float('nan'), float('nan')))) == 'nan'
# numpy actually raises an AttributeError,
# but numpypy raises a TypeError
- exc = raises((TypeError, AttributeError), 'c2.real = 10.')
- assert str(exc.value) == "readonly attribute"
- exc = raises((TypeError, AttributeError), 'c2.imag = 10.')
- assert str(exc.value) == "readonly attribute"
+ if '__pypy__' in sys.builtin_module_names:
+ exct, excm = TypeError, 'readonly attribute'
+ else:
+ exct, excm = AttributeError, 'is not writable'
+ exc = raises(exct, 'c2.real = 10.')
+ assert excm in exc.value[0]
+ exc = raises(exct, 'c2.imag = 10.')
+ assert excm in exc.value[0]
assert(real(c2) == 3.0)
assert(imag(c2) == 4.0)
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
@@ -3115,6 +3115,12 @@
assert array(2.2-1.1j, dtype='<c16').tostring() == \
'\x9a\x99\x99\x99\x99\x99\x01@\x9a\x99\x99\x99\x99\x99\xf1\xbf'
+ def test_str(self):
+ from numpypy import array
+ assert str(array([1, 2, 3])) == '[1 2 3]'
+ assert str(array(['abc'], 'S3')) == "['abc']"
+ assert str(array('abc')) == 'abc'
+
class AppTestRepr(BaseNumpyAppTest):
def setup_class(cls):
@@ -3127,12 +3133,10 @@
cache.w_array_str = None
cache.w_array_repr = None
- def test_repr_str(self):
+ def test_repr(self):
from numpypy import array
assert repr(array([1, 2, 3])) == 'array([1, 2, 3])'
- assert str(array([1, 2, 3])) == 'array([1, 2, 3])'
assert repr(array(['abc'], 'S3')) == "array(['abc'])"
- assert str(array(['abc'], 'S3')) == "array(['abc'])"
def teardown_class(cls):
if option.runappdirect:
@@ -3469,8 +3473,8 @@
assert str(a) == "[('aaaa', 1.0, 8.0, [[[1, 2, 3], [4, 5, 6]], " \
"[[7, 8, 9], [10, 11,
12]]])]"
else:
- assert str(a) == "array([('aaaa', 1.0, 8.0, [1, 2, 3, 4, 5, 6, " \
- "7, 8, 9, 10, 11,
12])])"
+ assert str(a) == "[('aaaa', 1.0, 8.0, [1, 2, 3, 4, 5, 6, " \
+ "7, 8, 9, 10, 11, 12])]"
def test_issue_1589(self):
import numpypy as numpy
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit