Author: Jeff Terrace <[email protected]>
Branch: numpy-full-fromstring
Changeset: r50525:d5854741cdef
Date: 2011-12-14 19:53 -0500
http://bitbucket.org/pypy/pypy/changeset/d5854741cdef/

Log:    Fix most review comments, two have responses in the notes sections
        of REVIEW

diff --git a/pypy/module/micronumpy/REVIEW b/pypy/module/micronumpy/REVIEW
--- a/pypy/module/micronumpy/REVIEW
+++ b/pypy/module/micronumpy/REVIEW
@@ -1,14 +1,25 @@
 Review items
 ============
 
+Fixed
+-----
 * Rather than `True if len(sep_stripped) == 0 else False` just use:
   `len(sep_stripped) == 0`.
 * Rather than name the variable `A`, name it `items` or somsething like that.
 * Rather than using `ptr`, use `idx`, since it's not really a pointer.
+* Same comment about comparing array equality as before.
+* Rather than name the attribute `char`, name it `format_code`.
+* `default_fromstring` can do `self.box(-1.0)`, instead of the coerce thing.
 * Rather than doing a string format to raise an error (L67), use
   `operationerrfmt`.
-* Same comment about comparing array equality as before.
+
+Notes
+-----
+* Tests for both bool and long dtypes with this.
+  (jterrace: fromstring with bool segfaults on my numpy 1.5.1.
+  Supposedly it has been fixed in later versions, but I can't
+  seem to install it to check to make sure my tests are correct.
+  There are already some tests for int64. Is that what you meant
+  by long types?)
 * No need for the `self.char == "?"` default.
-* Rather than name the attribute `char`, name it `format_code`.
-* `default_fromstring` can do `self.box(-1.0)`, instead of the coerce thing.
-* Tests for both bool and long dtypes with this.
+  (jterrace: Does not translate without it)
\ No newline at end of file
diff --git a/pypy/module/micronumpy/interp_support.py 
b/pypy/module/micronumpy/interp_support.py
--- a/pypy/module/micronumpy/interp_support.py
+++ b/pypy/module/micronumpy/interp_support.py
@@ -1,4 +1,4 @@
-from pypy.interpreter.error import OperationError
+from pypy.interpreter.error import OperationError, operationerrfmt
 from pypy.interpreter.gateway import unwrap_spec
 from pypy.rpython.lltypesystem import lltype, rffi
 from pypy.module.micronumpy import interp_dtype
@@ -11,17 +11,17 @@
     from pypy.module.micronumpy.interp_numarray import W_NDimArray
 
     sep_stripped = strip_spaces(sep)
-    skip_bad_vals = True if len(sep_stripped) == 0 else False
+    skip_bad_vals = len(sep_stripped) == 0
 
-    A = []
+    items = []
     num_items = 0
-    ptr = 0
+    idx = 0
     
-    while (num_items < count or count == -1) and ptr < len(s):
-        nextptr = s.find(sep, ptr)
-        if nextptr < 0:
-            nextptr = length
-        piece = strip_spaces(s[ptr:nextptr])
+    while (num_items < count or count == -1) and idx < len(s):
+        nextidx = s.find(sep, idx)
+        if nextidx < 0:
+            nextidx = length
+        piece = strip_spaces(s[idx:nextidx])
         if len(piece) > 0 or not skip_bad_vals:
             if len(piece) == 0 and not skip_bad_vals:
                 val = dtype.itemtype.default_fromstring(space)
@@ -42,17 +42,17 @@
                                 raise
                     if not gotit:
                         val = dtype.itemtype.default_fromstring(space)
-                    nextptr = length
-            A.append(val)
+                    nextidx = length
+            items.append(val)
             num_items += 1
-        ptr = nextptr + 1
+        idx = nextidx + 1
     
     if count > num_items:
         raise OperationError(space.w_ValueError, space.wrap(
             "string is smaller than requested size"))
 
     a = W_NDimArray(num_items, [num_items], dtype=dtype)
-    for i, val in enumerate(A):
+    for i, val in enumerate(items):
         a.dtype.setitem(a.storage, i, val)
     
     return space.wrap(a)
@@ -64,8 +64,9 @@
     if count == -1:
         count = length / itemsize
     if length % itemsize != 0:
-        raise OperationError(space.w_ValueError, space.wrap(
-            "string length %d not divisable by item size %d" % (length, 
itemsize)))
+        raise operationerrfmt(space.w_ValueError,
+                              "string length %d not divisable by item size %d",
+                              length, itemsize)
     if count * itemsize > length:
         raise OperationError(space.w_ValueError, space.wrap(
             "string is smaller than requested 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
@@ -1228,27 +1228,31 @@
         assert g[1] == 2
         assert g[2] == 3
         h = fromstring("1, , 2, 3", dtype=uint8, sep=",")
-        assert h.tolist() == [1,0,2,3]
+        assert (h == [1,0,2,3]).all()
         i = fromstring("1    2 3", dtype=uint8, sep=" ")
-        assert i.tolist() == [1,2,3]
+        assert (i == [1,2,3]).all()
         j = fromstring("1\t\t\t\t2\t3", dtype=uint8, sep="\t")
-        assert j.tolist() == [1,2,3]
+        assert (j == [1,2,3]).all()
         k = fromstring("1,x,2,3", dtype=uint8, sep=",")
-        assert k.tolist() == [1,0]
+        assert (k == [1,0]).all()
         l = fromstring("1,x,2,3", dtype='float32', sep=",")
-        assert l.tolist() == [1.0,-1.0]
+        assert (l == [1.0,-1.0]).all()
         m = fromstring("1,,2,3", sep=",")
-        assert m.tolist() == [1.0,-1.0,2.0,3.0]
+        assert (m == [1.0,-1.0,2.0,3.0]).all()
         n = fromstring("3.4 2.0 3.8 2.2", dtype=int32, sep=" ")
-        assert n.tolist() == [3]
+        assert (n == [3]).all()
         o = fromstring("1.0 2f.0f 3.8 2.2", dtype=float32, sep=" ")
         assert len(o) == 2
         assert o[0] == 1.0
         assert o[1] == 2.0
         p = fromstring("1.0,,2.0,3.0", sep=",")
-        assert p.tolist() == [1.0, -1.0, 2.0, 3.0]
+        assert (p == [1.0, -1.0, 2.0, 3.0]).all()
         q = fromstring("1.0,,2.0,3.0", sep=" ")
-        assert q.tolist() == [1.0]
+        assert (q == [1.0]).all()
+        r = fromstring("\x01\x00\x02", dtype='bool')
+        assert (r == [True, False, True]).all()
+        s = fromstring("1,2,3,,5", dtype="bool", sep=",")
+        assert (s == [True, True, True, False, True]).all()
         
     def test_fromstring_types(self):
         from numpypy import fromstring
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
@@ -56,7 +56,7 @@
 
 class Primitive(object):
     _mixin_ = True
-    char = "?"
+    format_code = '?'
     
     def get_element_size(self):
         return rffi.sizeof(self.T)
@@ -109,9 +109,7 @@
             )
 
     def runpack_str(self, s):
-        if self.char == "?":
-            raise NotImplementedError
-        return self.box(runpack(self.char, s))
+        return self.box(runpack(self.format_code, s))
 
     @simple_binary_op
     def add(self, v1, v2):
@@ -175,6 +173,7 @@
 class Bool(BaseType, Primitive):
     T = lltype.Bool
     BoxType = interp_boxes.W_BoolBox
+    format_code = '?'
 
     True = BoxType(True)
     False = BoxType(False)
@@ -203,6 +202,9 @@
 
     def for_computation(self, v):
         return int(v)
+    
+    def default_fromstring(self, space):
+        return self.box(False)
 
 class Integer(Primitive):
     _mixin_ = True
@@ -218,7 +220,7 @@
         return widen(v)
     
     def default_fromstring(self, space):
-        return self._coerce(space, space.wrap(0))
+        return self.box(0)
 
     @simple_binary_op
     def div(self, v1, v2):
@@ -255,32 +257,32 @@
 class Int8(BaseType, Integer):
     T = rffi.SIGNEDCHAR
     BoxType = interp_boxes.W_Int8Box
-    char = "b"
+    format_code = "b"
 
 class UInt8(BaseType, Integer):
     T = rffi.UCHAR
     BoxType = interp_boxes.W_UInt8Box
-    char = "B"
+    format_code = "B"
 
 class Int16(BaseType, Integer):
     T = rffi.SHORT
     BoxType = interp_boxes.W_Int16Box
-    char = "h"
+    format_code = "h"
 
 class UInt16(BaseType, Integer):
     T = rffi.USHORT
     BoxType = interp_boxes.W_UInt16Box
-    char = "H"
+    format_code = "H"
 
 class Int32(BaseType, Integer):
     T = rffi.INT
     BoxType = interp_boxes.W_Int32Box
-    char = "i"
+    format_code = "i"
 
 class UInt32(BaseType, Integer):
     T = rffi.UINT
     BoxType = interp_boxes.W_UInt32Box
-    char = "I"
+    format_code = "I"
 
 class Long(BaseType, Integer):
     T = rffi.LONG
@@ -293,12 +295,12 @@
 class Int64(BaseType, Integer):
     T = rffi.LONGLONG
     BoxType = interp_boxes.W_Int64Box
-    char = "q"
+    format_code = "q"
 
 class UInt64(BaseType, Integer):
     T = rffi.ULONGLONG
     BoxType = interp_boxes.W_UInt64Box
-    char = "Q"
+    format_code = "Q"
 
     def _coerce(self, space, w_item):
         try:
@@ -327,7 +329,7 @@
         return float(v)
 
     def default_fromstring(self, space):
-        return self._coerce(space, space.wrap(-1.0))
+        return self.box(-1.0)
 
     @simple_binary_op
     def div(self, v1, v2):
@@ -428,9 +430,9 @@
 class Float32(BaseType, Float):
     T = rffi.FLOAT
     BoxType = interp_boxes.W_Float32Box
-    char = "f"
+    format_code = "f"
 
 class Float64(BaseType, Float):
     T = rffi.DOUBLE
     BoxType = interp_boxes.W_Float64Box
-    char = "d"
\ No newline at end of file
+    format_code = "d"
\ No newline at end of file
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to