[pypy-commit] pypy numpy-data-buffer: move numpy.fromstring to applevel, add support for dtypes and count.

timo_jbo noreply at buildbot.pypy.org
Mon Oct 3 07:51:48 CEST 2011


Author: Timo Paulssen <timonator at perpetuum-immobile.de>
Branch: numpy-data-buffer
Changeset: r47780:f4a7400e9c6e
Date: 2011-10-03 07:22 +0200
http://bitbucket.org/pypy/pypy/changeset/f4a7400e9c6e/

Log:	move numpy.fromstring to applevel, add support for dtypes and count.

diff --git a/lib_pypy/numpy/__init__.py b/lib_pypy/numpy/__init__.py
--- a/lib_pypy/numpy/__init__.py
+++ b/lib_pypy/numpy/__init__.py
@@ -6,7 +6,6 @@
         zeros,
         empty,
         ones,
-        fromstring,
 
         abs,
         absolute,
@@ -87,3 +86,24 @@
 
     return array(result)
 
+def fromstring(s, dtype=float, count=-1, sep=''):
+    from _numpy import dtype as dt
+    if sep:
+        raise NotImplementedError("Cannot use fromstring with a separator yet")
+
+    _dtype = dt(dtype)
+    if count > 0:
+        length = count * _dtype.itemsize
+        if length > len(s):
+            raise ValueError("length of string (%d) not enough for %d %s" % (len(s), count, _dtype))
+        s = s[:length]
+    else:
+        length = len(s)
+        if len(s) % _dtype.itemsize != 0:
+            raise ValueError("length of string (%d) not evenly dividable by size of dtype (%d)" % (len(s), _dtype.itemsize))
+
+    arr = empty(length / _dtype.itemsize, dtype=_dtype)
+    print len(arr.data), len(s), length, _dtype.itemsize
+    arr.data[:length] = s
+
+    return arr
diff --git a/lib_pypy/pypy_test/test_numpy.py b/lib_pypy/pypy_test/test_numpy.py
--- a/lib_pypy/pypy_test/test_numpy.py
+++ b/lib_pypy/pypy_test/test_numpy.py
@@ -74,3 +74,25 @@
         raises(ValueError, "bincount(c, w)")
         raises(ValueError, "bincount([])")
 
+    def test_fromstring(self):
+        from numpy import fromstring
+        import struct
+
+        data = struct.pack('dddd', 0, 1, 2, 3)
+        a = fromstring(data)
+        assert len(a) == 4
+        for i in range(4):
+            assert a[i] == i
+        raises(ValueError, fromstring, "abc")
+
+        data = struct.pack("iiii", 0, 1, 2, 3)
+        assert len(a) == 4
+        a = fromstring(data, dtype="i")
+        for i in range(4):
+            assert a[i] == i
+
+        data = struct.pack("iiii", 0, 1, 2, 3) + "hello world"
+        a = fromstring(data, dtype="i", count=4)
+        assert len(a) == 4
+        for i in range(4):
+            assert a[i] == i
diff --git a/pypy/module/_numpy/__init__.py b/pypy/module/_numpy/__init__.py
--- a/pypy/module/_numpy/__init__.py
+++ b/pypy/module/_numpy/__init__.py
@@ -12,7 +12,6 @@
         'zeros': 'interp_numarray.zeros',
         'empty': 'interp_numarray.zeros',
         'ones': 'interp_numarray.ones',
-        'fromstring': 'interp_support.fromstring',
     }
 
     # ufuncs
diff --git a/pypy/module/_numpy/interp_support.py b/pypy/module/_numpy/interp_support.py
deleted file mode 100644
--- a/pypy/module/_numpy/interp_support.py
+++ /dev/null
@@ -1,34 +0,0 @@
-from pypy.interpreter.error import OperationError
-from pypy.interpreter.gateway import unwrap_spec
-from pypy.module._numpy.interp_dtype import W_Float64Dtype
-from pypy.rlib.rstruct.runpack import runpack
-from pypy.rpython.lltypesystem import lltype, rffi
-
-
-FLOAT_SIZE = rffi.sizeof(lltype.Float)
-
- at unwrap_spec(s=str)
-def fromstring(space, s):
-    from pypy.module._numpy.interp_numarray import SingleDimArray
-    length = len(s)
-
-    if length % FLOAT_SIZE == 0:
-        number = length/FLOAT_SIZE
-    else:
-        raise OperationError(space.w_ValueError, space.wrap(
-            "string length %d not divisable by %d" % (length, FLOAT_SIZE)))
-
-    dtype = space.fromcache(W_Float64Dtype)
-    a = SingleDimArray(number, dtype=dtype)
-
-    start = 0
-    end = FLOAT_SIZE
-    i = 0
-    while i < number:
-        part = s[start:end]
-        a.dtype.setitem(a.storage, i, dtype.box(runpack('d', part)))
-        i += 1
-        start += FLOAT_SIZE
-        end += FLOAT_SIZE
-
-    return space.wrap(a)
diff --git a/pypy/module/_numpy/test/test_numarray.py b/pypy/module/_numpy/test/test_numarray.py
--- a/pypy/module/_numpy/test/test_numarray.py
+++ b/pypy/module/_numpy/test/test_numarray.py
@@ -587,15 +587,3 @@
                 assert c[i] == func(b[i], 3)
 
 
-class AppTestSupport(object):
-    def setup_class(cls):
-        import struct
-        cls.space = gettestobjspace(usemodules=('_numpy',))
-        cls.w_data = cls.space.wrap(struct.pack('dddd', 1, 2, 3, 4))
-
-    def test_fromstring(self):
-        from _numpy import fromstring
-        a = fromstring(self.data)
-        for i in range(4):
-            assert a[i] == i + 1
-        raises(ValueError, fromstring, "abc")


More information about the pypy-commit mailing list