Commits

Timo Paulssen committed f4a7400

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

  • Participants
  • Parent commits 7c67ce4
  • Branches numpy-data-buffer

Comments (0)

Files changed (5)

lib_pypy/numpy/__init__.py

         zeros,
         empty,
         ones,
-        fromstring,
 
         abs,
         absolute,
 
     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

lib_pypy/pypy_test/test_numpy.py

         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

pypy/module/_numpy/__init__.py

         'zeros': 'interp_numarray.zeros',
         'empty': 'interp_numarray.zeros',
         'ones': 'interp_numarray.ones',
-        'fromstring': 'interp_support.fromstring',
     }
 
     # ufuncs

pypy/module/_numpy/interp_support.py

-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)
-
-@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)

pypy/module/_numpy/test/test_numarray.py

                 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")