Commits

Brian Kearns committed 014e37c

fix another view bug

Comments (0)

Files changed (3)

pypy/module/micronumpy/interp_boxes.py

     def get_dtype(self, space):
         return self.arr.dtype
 
+    def raw_str(self):
+        return self.arr.dtype.itemtype.to_str(self)
+
 class W_VoidBox(W_FlexibleBox):
     def descr_getitem(self, space, w_item):
         if space.isinstance_w(w_item, space.w_basestring):
         # XXX assert dtype is str type
         return self
 
-    def raw_str(self):
-        return self.arr.dtype.itemtype.to_str(self)
-
 class W_StringBox(W_CharacterBox):
     def descr__new__string_box(space, w_subtype, w_arg):
         from pypy.module.micronumpy.interp_dtype import new_string_dtype

pypy/module/micronumpy/test/test_numarray.py

         assert a.view('S4') == '\x03'
         a = array('abc1', dtype='c')
         assert a.view('S4') == 'abc1'
+        a = array([(1, 2)], dtype=[('a', int), ('b', int)])[0]
+        assert a.shape == ()
+        assert a.view('S16') == '\x01' + '\x00' * 7 + '\x02'
 
     def test_array_view(self):
         from numpypy import array, dtype
         x = array(['abc', 'defg'], dtype='string')
         assert x.view('S4')[0] == 'abc'
         assert x.view('S4')[1] == 'defg'
+        a = array([(1, 2)], dtype=[('a', int), ('b', int)])
+        assert a.view('S16')[0] == '\x01' + '\x00' * 7 + '\x02'
 
     def test_ndarray_view_empty(self):
         from numpypy import array, dtype

pypy/module/micronumpy/types.py

         BoxType = interp_boxes.W_ComplexLongBox
         ComponentBoxType = interp_boxes.W_FloatLongBox
 
-class BaseStringType(BaseType):
+class FlexibleType(BaseType):
     def get_element_size(self):
         return rffi.sizeof(self.T)
 
+    @jit.unroll_safe
+    def to_str(self, item):
+        builder = StringBuilder()
+        assert isinstance(item, interp_boxes.W_FlexibleBox)
+        i = item.ofs
+        end = i + item.dtype.get_size()
+        while i < end:
+            assert isinstance(item.arr.storage[i], str)
+            if item.arr.storage[i] == '\x00':
+                break
+            builder.append(item.arr.storage[i])
+            i += 1
+        return builder.build()
+
 def str_unary_op(func):
     specialize.argtype(1)(func)
     @functools.wraps(func)
         )
     return dispatcher
 
-class StringType(BaseStringType):
+class StringType(FlexibleType):
     T = lltype.Char
 
     @jit.unroll_safe
             dtype = arr.dtype
         return interp_boxes.W_StringBox(arr, i + offset, dtype)
 
-    @jit.unroll_safe
-    def to_str(self, item):
-        builder = StringBuilder()
-        assert isinstance(item, interp_boxes.W_StringBox)
-        i = item.ofs
-        end = i + item.dtype.get_size()
-        while i < end:
-            assert isinstance(item.arr.storage[i], str)
-            if item.arr.storage[i] == '\x00':
-                break
-            builder.append(item.arr.storage[i])
-            i += 1
-        return builder.build()
-
     def str_format(self, item):
         builder = StringBuilder()
         builder.append("'")
         for i in xrange(start, stop, width):
             self._store(storage, i, offset, box, width)
 
-class UnicodeType(BaseStringType):
+class UnicodeType(FlexibleType):
     T = lltype.UniChar
 
     @jit.unroll_safe
         raise OperationError(space.w_NotImplementedError, space.wrap(
             "coerce (probably from set_item) not implemented for unicode type"))
 
-class VoidType(BaseStringType):
+class VoidType(FlexibleType):
     T = lltype.Char
 
     def _coerce(self, space, arr, ofs, dtype, w_items, shape):
                              dtype.shape, arr, W_NDimArray(arr), dtype.subdtype)
         return W_NDimArray(implementation)
 
-class RecordType(BaseType):
+class RecordType(FlexibleType):
     T = lltype.Char
 
-    def get_element_size(self):
-        return rffi.sizeof(self.T)
-
     def read(self, arr, i, offset, dtype=None):
         if dtype is None:
             dtype = arr.dtype