[pypy-commit] pypy default: test, fix for str(box('abc')) => 'abc' rather than "'abc'"
mattip
noreply at buildbot.pypy.org
Tue May 19 23:08:18 CEST 2015
Author: mattip <matti.picus at gmail.com>
Branch:
Changeset: r77416:6f94cd71078c
Date: 2015-05-20 00:08 +0300
http://bitbucket.org/pypy/pypy/changeset/6f94cd71078c/
Log: test, fix for str(box('abc')) => 'abc' rather than "'abc'"
diff --git a/pypy/module/micronumpy/boxes.py b/pypy/module/micronumpy/boxes.py
--- a/pypy/module/micronumpy/boxes.py
+++ b/pypy/module/micronumpy/boxes.py
@@ -193,7 +193,7 @@
"'%T' object is not iterable", self)
def descr_str(self, space):
- return space.wrap(self.get_dtype(space).itemtype.str_format(self))
+ return space.wrap(self.get_dtype(space).itemtype.str_format(self, add_quotes=False))
def descr_format(self, space, w_spec):
return space.format(self.item(space), w_spec)
diff --git a/pypy/module/micronumpy/ndarray.py b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -277,7 +277,7 @@
if self.is_scalar() and dtype.is_str():
s.append(dtype.itemtype.to_str(i.getitem(state)))
else:
- s.append(dtype.itemtype.str_format(i.getitem(state)))
+ s.append(dtype.itemtype.str_format(i.getitem(state), add_quotes=True))
state = i.next(state)
if not self.is_scalar():
s.append(']')
diff --git a/pypy/module/micronumpy/test/test_ndarray.py b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -3468,6 +3468,9 @@
assert str(array('abc')) == 'abc'
assert str(array(1.5)) == '1.5'
assert str(array(1.5).real) == '1.5'
+ arr = array(['abc', 'abc'])
+ for a in arr.flat:
+ assert str(a) == 'abc'
def test_ndarray_buffer_strides(self):
from numpy import ndarray, array
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
@@ -388,7 +388,7 @@
def to_builtin_type(self, space, w_item):
return space.wrap(self.unbox(w_item))
- def str_format(self, box):
+ def str_format(self, box, add_quotes=True):
return "True" if self.unbox(box) else "False"
@staticmethod
@@ -454,7 +454,7 @@
def _coerce(self, space, w_item):
return self._base_coerce(space, w_item)
- def str_format(self, box):
+ def str_format(self, box, add_quotes=True):
return str(self.for_computation(self.unbox(box)))
@staticmethod
@@ -727,7 +727,7 @@
return self.box(rfloat.NAN)
return self.box(space.float_w(space.call_function(space.w_float, w_item)))
- def str_format(self, box):
+ def str_format(self, box, add_quotes=True):
return float2string(self.for_computation(self.unbox(box)), "g",
rfloat.DTSF_STR_PRECISION)
@@ -1132,7 +1132,7 @@
w_obj.__init__(w_tmpobj.real, w_tmpobj.imag)
return w_obj
- def str_format(self, box):
+ def str_format(self, box, add_quotes=True):
real, imag = self.for_computation(self.unbox(box))
imag_str = str_format(imag)
if not rfloat.isfinite(imag):
@@ -1862,7 +1862,7 @@
w_obj = self.space.newcomplex(real, imag)
return self.BoxType(w_obj)
- def str_format(self, box):
+ def str_format(self, box, add_quotes=True):
return self.space.str_w(self.space.repr(self.unbox(box)))
def runpack_str(self, space, s):
@@ -2122,11 +2122,13 @@
dtype = arr.dtype
return boxes.W_StringBox(arr, i + offset, dtype)
- def str_format(self, item):
+ def str_format(self, item, add_quotes=True):
builder = StringBuilder()
- builder.append("'")
+ if add_quotes:
+ builder.append("'")
builder.append(self.to_str(item))
- builder.append("'")
+ if add_quotes:
+ builder.append("'")
return builder.build()
# XXX move the rest of this to base class when UnicodeType is supported
@@ -2209,7 +2211,7 @@
def read(self, arr, i, offset, dtype=None):
raise oefmt(self.space.w_NotImplementedError, "unicode type not completed")
- def str_format(self, item):
+ def str_format(self, item, add_quotes=True):
raise oefmt(self.space.w_NotImplementedError, "unicode type not completed")
def to_builtin_type(self, space, box):
@@ -2314,7 +2316,7 @@
return boxes.W_VoidBox(arr, i + offset, dtype)
@jit.unroll_safe
- def str_format(self, box):
+ def str_format(self, box, add_quotes=True):
assert isinstance(box, boxes.W_VoidBox)
arr = self.readarray(box.arr, box.ofs, 0, box.dtype)
return arr.dump_data(prefix='', suffix='')
@@ -2425,7 +2427,7 @@
return space.newtuple(items)
@jit.unroll_safe
- def str_format(self, box):
+ def str_format(self, box, add_quotes=True):
assert isinstance(box, boxes.W_VoidBox)
pieces = ["("]
first = True
@@ -2437,7 +2439,7 @@
else:
pieces.append(", ")
val = tp.read(box.arr, box.ofs, ofs, subdtype)
- pieces.append(tp.str_format(val))
+ pieces.append(tp.str_format(val, add_quotes=add_quotes))
pieces.append(")")
return "".join(pieces)
More information about the pypy-commit
mailing list