[pypy-commit] pypy numpy-record-dtypes: start implementing string boxes

fijal noreply at buildbot.pypy.org
Sat Feb 25 03:12:10 CET 2012


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: numpy-record-dtypes
Changeset: r52895:36e4ff545206
Date: 2012-02-24 18:11 -0800
http://bitbucket.org/pypy/pypy/changeset/36e4ff545206/

Log:	start implementing string boxes

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
@@ -197,9 +197,6 @@
 
 
 class W_FlexibleBox(W_GenericBox):
-    pass
-
-class W_VoidBox(W_FlexibleBox):
     def __init__(self, arr, ofs):
         self.arr = arr # we have to keep array alive
         self.ofs = ofs
@@ -207,6 +204,7 @@
     def get_dtype(self, space):
         return self.arr.dtype
 
+class W_VoidBox(W_FlexibleBox):
     @unwrap_spec(item=str)
     def descr_getitem(self, space, item):
         try:
@@ -230,10 +228,26 @@
     pass
 
 class W_StringBox(W_CharacterBox):
-    pass
+    def descr__new__(space, w_subtype, w_arg):
+        from pypy.module.micronumpy.interp_numarray import W_NDimArray
+        from pypy.module.micronumpy.interp_dtype import new_string_dtype
+
+        arg = space.str_w(space.str(w_arg))
+        arr = W_NDimArray([1], new_string_dtype(space, len(arg)))
+        for i in range(len(arg)):
+            arr.storage[i] = arg[i]
+        return W_StringBox(arr, 0)
 
 class W_UnicodeBox(W_CharacterBox):
-    pass
+    def descr__new__(space, w_subtype, w_arg):
+        from pypy.module.micronumpy.interp_numarray import W_NDimArray
+        from pypy.module.micronumpy.interp_dtype import new_unicode_dtype
+
+        arg = space.unicode_w(space.unicode(w_arg))
+        arr = W_NDimArray([1], new_unicode_dtype(space, len(arg)))
+        for i in range(len(arg)):
+            arr.setitem(i, arg[i])
+        return W_UnicodeBox(arr, 0)
 
 W_GenericBox.typedef = TypeDef("generic",
     __module__ = "numpypy",
@@ -394,9 +408,11 @@
 
 W_StringBox.typedef = TypeDef("string_", (str_typedef, W_CharacterBox.typedef),
     __module__ = "numpypy",
+    __new__ = interp2app(W_StringBox.descr__new__.im_func),
 )
 
 W_UnicodeBox.typedef = TypeDef("unicode_", (unicode_typedef, W_CharacterBox.typedef),
     __module__ = "numpypy",
+    __new__ = interp2app(W_UnicodeBox.descr__new__.im_func),
 )
                                           
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
@@ -247,6 +247,27 @@
     byteorder_prefix = '>'
     nonnative_byteorder_prefix = '<'
 
+def new_string_dtype(space, size):
+    return W_Dtype(
+        types.StringType(size),
+        num=18,
+        kind=STRINGLTR,
+        name='string',
+        char='S' + str(size),
+        w_box_type = space.gettypefor(interp_boxes.W_StringBox),
+    )
+
+def new_unicode_dtype(space, size):
+    return W_Dtype(
+        types.UnicodeType(size),
+        num=19,
+        kind=UNICODELTR,
+        name='unicode',
+        char='U' + str(size),
+        w_box_type = space.gettypefor(interp_boxes.W_UnicodeBox),
+    )
+
+
 class DtypeCache(object):
     def __init__(self, space):
         self.w_booldtype = W_Dtype(
@@ -379,7 +400,7 @@
             w_box_type = space.gettypefor(interp_boxes.W_ULongLongBox),
         )
         self.w_stringdtype = W_Dtype(
-            types.StringType(0),
+            types.StringType(1),
             num=18,
             kind=STRINGLTR,
             name='string',
@@ -388,7 +409,7 @@
             alternate_constructors=[space.w_str],
         )
         self.w_unicodedtype = W_Dtype(
-            types.UnicodeType(0),
+            types.UnicodeType(1),
             num=19,
             kind=UNICODELTR,
             name='unicode',
diff --git a/pypy/module/micronumpy/test/test_dtypes.py b/pypy/module/micronumpy/test/test_dtypes.py
--- a/pypy/module/micronumpy/test/test_dtypes.py
+++ b/pypy/module/micronumpy/test/test_dtypes.py
@@ -521,6 +521,14 @@
         assert d.name == "unicode256"
         assert d.num == 19
 
+    def test_string_boxes(self):
+        from _numpypy import str_
+        assert str_(3) == '3'
+
+    def test_unicode_boxes(self):
+        from _numpypy import str_
+        assert str_(3) == '3'
+
 class AppTestRecordDtypes(BaseNumpyAppTest):
     def test_create(self):
         from _numpypy import dtype, void


More information about the pypy-commit mailing list