[pypy-commit] pypy voidtype_strformat: fix multirow record array access
mattip
noreply at buildbot.pypy.org
Thu Nov 21 23:51:56 CET 2013
Author: Matti Picus <matti.picus at gmail.com>
Branch: voidtype_strformat
Changeset: r68268:5aaafb1df4d8
Date: 2013-11-21 23:52 +0200
http://bitbucket.org/pypy/pypy/changeset/5aaafb1df4d8/
Log: fix multirow record array access
diff --git a/pypy/module/micronumpy/iter.py b/pypy/module/micronumpy/iter.py
--- a/pypy/module/micronumpy/iter.py
+++ b/pypy/module/micronumpy/iter.py
@@ -61,10 +61,24 @@
def apply(self, space, orig_arr):
arr = orig_arr.implementation
ofs, subdtype = arr.dtype.fields[self.name]
- # strides backstrides are identical, ofs only changes start
- return W_NDimArray.new_slice(space, arr.start + ofs, arr.get_strides(),
- arr.get_backstrides(),
- arr.shape, arr, orig_arr, subdtype)
+ # ofs only changes start
+ # create a view of the original array by extending
+ # the shape, strides, backstrides of the array
+ from pypy.module.micronumpy.support import calc_strides
+ strides, backstrides = calc_strides(subdtype.shape,
+ subdtype.subdtype, arr.order)
+ final_shape = arr.shape + subdtype.shape
+ final_strides = arr.get_strides()[:]
+ final_strides += strides
+ final_backstrides = arr.get_backstrides()[:]
+ final_backstrides += backstrides
+ final_dtype = subdtype
+ print self.name,'strides',arr.get_strides(),strides
+ if subdtype.subdtype:
+ final_dtype = subdtype.subdtype
+ return W_NDimArray.new_slice(space, arr.start + ofs, final_strides,
+ final_backstrides,
+ final_shape, arr, orig_arr, final_dtype)
class Chunks(BaseChunk):
def __init__(self, l):
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
@@ -3069,7 +3069,9 @@
]
h = np.array(buf, dtype=descr)
assert len(h) == 2
- skip('broken') # XXX
+ assert h['x'].shape == (2, 2)
+ assert h['y'].strides == (41, 16, 8)
+ assert h['z'].shape == (2,)
for v in (h, h[0], h['x']):
repr(v) # check for crash in repr
assert (h['x'] == np.array([buf[0][0],
@@ -3107,9 +3109,10 @@
[[7, 8, 9], [10, 11, 12]]])],
dtype=dt)
s = str(a)
- assert s.endswith("[('aaaa', 1.0, 8.0, [[[1, 2, 3], [4, 5, 6]], " \
+ assert s.endswith("[('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
c = numpy.array([[(1, 2, 'a'), (3, 4, 'b')], [(5, 6, 'c'), (7, 8, 'd')]],
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
@@ -1789,24 +1789,16 @@
dtype.subdtype)
return W_NDimArray(implementation)
- def str_format(self, val):
- # only called with the results of readarray()
- from pypy.module.micronumpy.base import W_NDimArray
- assert isinstance(val, W_NDimArray)
- i = val.create_iter()
- first = True
- dtype = val.get_dtype()
- s = StringBuilder()
- s.append('[')
- while not i.done():
- if first:
- first = False
- else:
- s.append(', ')
- s.append(dtype.itemtype.str_format(i.getitem()))
- i.next()
- s.append(']')
- return s.build()
+ def read(self, arr, i, offset, dtype=None):
+ if dtype is None:
+ dtype = arr.dtype
+ return interp_boxes.W_VoidBox(arr, i + offset, dtype)
+
+ @jit.unroll_safe
+ def str_format(self, box):
+ assert isinstance(box, interp_boxes.W_VoidBox)
+ arr = self.readarray(box.arr, box.ofs, 0, box.dtype)
+ return arr.dump_data(prefix='', suffix='')
class RecordType(FlexibleType):
T = lltype.Char
@@ -1867,10 +1859,7 @@
first = False
else:
pieces.append(", ")
- if isinstance(tp, VoidType):
- val = tp.readarray(box.arr, box.ofs, ofs, subdtype)
- else:
- val = tp.read(box.arr, box.ofs, ofs, subdtype)
+ val = tp.read(box.arr, box.ofs, ofs, subdtype)
pieces.append(tp.str_format(val))
pieces.append(")")
return "".join(pieces)
More information about the pypy-commit
mailing list