[pypy-commit] pypy numpypy-problems: StringType passes tests, translates

mattip noreply at buildbot.pypy.org
Sun Sep 30 10:58:58 CEST 2012


Author: mattip <matti.picus at gmail.com>
Branch: numpypy-problems
Changeset: r57686:46c9ad0d437f
Date: 2012-09-30 10:54 +0200
http://bitbucket.org/pypy/pypy/changeset/46c9ad0d437f/

Log:	StringType passes tests, translates

diff --git a/pypy/module/micronumpy/arrayimpl/voidbox.py b/pypy/module/micronumpy/arrayimpl/voidbox.py
--- a/pypy/module/micronumpy/arrayimpl/voidbox.py
+++ b/pypy/module/micronumpy/arrayimpl/voidbox.py
@@ -6,6 +6,7 @@
     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)
diff --git a/pypy/module/micronumpy/interp_boxes.py b/pypy/module/micronumpy/interp_boxes.py
--- a/pypy/module/micronumpy/interp_boxes.py
+++ b/pypy/module/micronumpy/interp_boxes.py
@@ -228,7 +228,11 @@
         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):
@@ -253,7 +257,6 @@
             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
diff --git a/pypy/module/micronumpy/interp_dtype.py b/pypy/module/micronumpy/interp_dtype.py
--- a/pypy/module/micronumpy/interp_dtype.py
+++ b/pypy/module/micronumpy/interp_dtype.py
@@ -64,11 +64,16 @@
     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())
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
@@ -2241,14 +2241,14 @@
         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):
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
@@ -15,6 +15,7 @@
 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
@@ -908,18 +909,47 @@
     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


More information about the pypy-commit mailing list