[pypy-commit] pypy numpy-refactor: tostring

fijal noreply at buildbot.pypy.org
Fri Sep 7 18:33:36 CEST 2012


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: numpy-refactor
Changeset: r57224:7cdf73adf84e
Date: 2012-09-07 18:20 +0200
http://bitbucket.org/pypy/pypy/changeset/7cdf73adf84e/

Log:	tostring

diff --git a/pypy/module/micronumpy/arrayimpl/scalar.py b/pypy/module/micronumpy/arrayimpl/scalar.py
--- a/pypy/module/micronumpy/arrayimpl/scalar.py
+++ b/pypy/module/micronumpy/arrayimpl/scalar.py
@@ -18,7 +18,7 @@
         raise Exception("Don't call setitem on scalar iterators")
 
     def done(self):
-        return False
+        raise Exception("should not call done on scalars")
 
     def reset(self):
         pass
diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -58,6 +58,9 @@
     def descr_fill(self, space, w_value):
         self.fill(self.get_dtype().coerce(space, w_value))
 
+    def descr_tostring(self, space):
+        return space.wrap(loop.tostring(space, self))
+
     def getitem_filter(self, space, arr):
         if arr.get_size() > self.get_size():
             raise OperationError(space.w_IndexError,
@@ -508,7 +511,9 @@
     size = GetSetProperty(W_NDimArray.descr_get_size),
     itemsize = GetSetProperty(W_NDimArray.descr_get_itemsize),
     nbytes = GetSetProperty(W_NDimArray.descr_get_nbytes),
+
     fill = interp2app(W_NDimArray.descr_fill),
+    tostring = interp2app(W_NDimArray.descr_tostring),
 
     mean = interp2app(W_NDimArray.descr_mean),
     sum = interp2app(W_NDimArray.descr_sum),
diff --git a/pypy/module/micronumpy/loop.py b/pypy/module/micronumpy/loop.py
--- a/pypy/module/micronumpy/loop.py
+++ b/pypy/module/micronumpy/loop.py
@@ -5,6 +5,8 @@
 
 from pypy.rlib.objectmodel import specialize
 from pypy.module.micronumpy.base import W_NDimArray
+from pypy.rlib.rstring import StringBuilder
+from pypy.rpython.lltypesystem import lltype, rffi
 
 def call2(shape, func, name, calc_dtype, res_dtype, w_lhs, w_rhs, out):
     if out is None:
@@ -217,3 +219,17 @@
         ai.setitem(val)
         ai.next()
         i += 1
+
+def tostring(space, arr):
+    builder = StringBuilder()
+    iter = arr.create_iter()
+    res_str = W_NDimArray.from_shape([1], arr.get_dtype(), order='C')
+    itemsize = arr.get_dtype().itemtype.get_element_size()
+    res_str_casted = rffi.cast(rffi.CArrayPtr(lltype.Char),
+                               res_str.implementation.get_storage_as_int(space))
+    while not iter.done():
+        res_str.implementation.setitem(0, iter.getitem())
+        for i in range(itemsize):
+            builder.append(res_str_casted[i])
+        iter.next()
+    return builder.build()


More information about the pypy-commit mailing list