1. Romain Guillebert
  2. pypy

Commits

mattip  committed 46c9ad0

StringType passes tests, translates

  • Participants
  • Parent commits 4c5d7dc
  • Branches numpypy-problems

Comments (0)

Files changed (5)

File pypy/module/micronumpy/arrayimpl/voidbox.py

View file
     def __init__(self, size, dtype):
         self.storage = alloc_raw_storage(size)
         self.dtype = dtype
+        self.size = size
 
     def __del__(self):
         free_raw_storage(self.storage)

File pypy/module/micronumpy/interp_boxes.py

View file
         except KeyError:
             raise OperationError(space.w_IndexError,
                                  space.wrap("Field %s does not exist" % item))
-        return space.wrap(dtype.itemtype.read(self.arr, self.ofs, ofs, dtype))
+        read_val = dtype.itemtype.read(self.arr, self.ofs, ofs, dtype)
+        if isinstance (read_val, W_StringBox):
+            # StringType returns a str
+            return space.wrap(dtype.itemtype.to_str(read_val))
+        return read_val
 
     @unwrap_spec(item=str)
     def descr_setitem(self, space, item, w_value):
             arr.storage[i] = arg[i]
         return W_StringBox(arr, 0, arr.dtype)
 
-
 class W_UnicodeBox(W_CharacterBox):
     def descr__new__unicode_box(space, w_subtype, w_arg):
         from pypy.module.micronumpy.interp_dtype import new_unicode_dtype

File pypy/module/micronumpy/interp_dtype.py

View file
     def fill(self, storage, box, start, stop):
         self.itemtype.fill(storage, self.get_size(), box, start, stop, 0)
 
+    def get_name(self):
+        if self.char == 'S':
+            return '|S' + str(self.get_size())
+        return self.name
+
     def descr_str(self, space):
-        return space.wrap(self.name)
+        return space.wrap(self.get_name())
 
     def descr_repr(self, space):
-        return space.wrap("dtype('%s')" % self.name)
+        return space.wrap("dtype('%s')" % self.get_name())
 
     def descr_get_itemsize(self, space):
         return space.wrap(self.itemtype.get_element_size())

File pypy/module/micronumpy/test/test_numarray.py

View file
         assert d.fields['x'] == (dtype(str), 0)
         assert d.fields['y'] == (dtype('int32'), 1)
         a = array([('a', 2), ('c', 1)], dtype=d)
+        assert a[1]['y'] == 1
         assert a[0]['x'] == 'a'
-        assert a[1]['y'] == 1
 
     def test_stringarray(self):
         from _numpypy import array
-        a = array(['abc'])
-        assert str(a) == "['abc']"
-        assert a.dtype == '|S3'
+        a = array(['abc'],'S3')
+        assert repr(a) == "array(['abc'])"
+        assert str(a.dtype) == '|S3'
        
 class AppTestPyPy(BaseNumpyAppTest):
     def setup_class(cls):

File pypy/module/micronumpy/types.py

View file
 from pypy.rlib.rstruct.runpack import runpack
 from pypy.tool.sourcetools import func_with_new_name
 from pypy.rlib import jit
+from pypy.rlib.rstring import StringBuilder
 
 
 degToRad = math.pi / 180.0
     T = lltype.Char
 
     def coerce(self, space, dtype, w_item):
-        if space.isinstance_w(w_item, space.w_str):
-            return space.unwrap(w_item)
-        print 'w_item',type(w_item)
-        xxx
+        from pypy.module.micronumpy.interp_dtype import new_string_dtype
+        arg = space.str_w(space.str(w_item))
+        arr = interp_boxes.VoidBoxStorage(len(arg), new_string_dtype(space, len(arg)))
+        for i in range(len(arg)):
+            arr.storage[i] = arg[i]
+        return interp_boxes.W_StringBox(arr,  0, None)
 
     def store(self, arr, i, offset, box):
-        assert isinstance(box, str)
-        for k in range(min(self.size-i, len(box)-offset)):
-            arr.storage[k + i] = box[k + offset]
+        assert isinstance(box, interp_boxes.W_StringBox)
+        for k in range(min(self.size-i, box.arr.size-offset)):
+            arr.storage[k + i] = box.arr.storage[k + offset]
 
     def read(self, arr, i, offset, dtype=None):
-        return arr.storage[i+offset]
+        if dtype is None:
+            dtype = arr.dtype
+        return interp_boxes.W_StringBox(arr, i + offset, dtype)
+        #print 'read',arr, arr.dtype
+        #xxx
+        #builder = StringBuilder()
+        #i = 0
+        #while i < self.size:
+        #    assert isinstance(arr.storage[i], str)
+        #    builder.append(arr.storage[i])
+        #    i += 1
+        #return builder.build()
+    def to_str(self, item):    
+        builder = StringBuilder()
+        assert isinstance(item, interp_boxes.W_StringBox)
+        i = 0
+        while i < self.size:
+            assert isinstance(item.arr.storage[i], str)
+            builder.append(item.arr.storage[i])
+            i += 1
+        return builder.build()
+
+    def str_format(self, item):
+        builder = StringBuilder()
+        builder.append("'")
+        builder.append(self.to_str(item))
+        builder.append("'")
+        return builder.build()
 
 class VoidType(BaseType, BaseStringType):
     T = lltype.Char