Author: Alex Gaynor <[email protected]>
Branch: numpy-dtype-alt
Changeset: r46361:6995b2446ffc
Date: 2011-08-07 19:15 -0700
http://bitbucket.org/pypy/pypy/changeset/6995b2446ffc/

Log:    fix the str and repr of arrays

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
@@ -1,10 +1,11 @@
 from pypy.interpreter.baseobjspace import Wrappable
 from pypy.interpreter.gateway import interp2app
 from pypy.interpreter.typedef import TypeDef, interp_attrproperty
+from pypy.objspace.std.floatobject import float2string
+from pypy.rlib.rfloat import DTSF_STR_PRECISION
 from pypy.rlib.unroll import unrolling_iterable
 from pypy.rpython.lltypesystem import lltype, llmemory, rffi
 
-
 SIGNEDLTR = "i"
 
 class W_Dtype(Wrappable):
@@ -62,6 +63,9 @@
     def setitem_w(self, space, storage, i, w_item):
         self.setitem(storage, i, self.unwrap(space, w_item))
 
+    def str_format(self, item):
+        return str(item)
+
 def make_array_ptr(T):
     return lltype.Ptr(lltype.Array(T, hints={"nolength": True}))
 
@@ -96,6 +100,9 @@
     applevel_types = ["int"]
     TP = make_array_ptr(rffi.LONG)
 
+    def unwrap(self, space, w_item):
+        return space.int_w(space.int(w_item))
+
 class W_Int64Dtype(LowLevelDtype, W_Dtype):
     num = 9
     applevel_types = ["long"]
@@ -109,6 +116,9 @@
     def unwrap(self, space, w_item):
         return space.float_w(space.float(w_item))
 
+    def str_format(self, item):
+        return float2string(item, 'g', DTSF_STR_PRECISION)
+
 
 ALL_DTYPES = [
     W_BoolDtype, W_Int8Dtype, W_Int32Dtype, W_LongDtype, W_Int64Dtype, 
W_Float64Dtype
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
@@ -6,7 +6,6 @@
 from pypy.interpreter.typedef import TypeDef, GetSetProperty
 from pypy.module.micronumpy import interp_ufuncs, interp_dtype
 from pypy.module.micronumpy.interp_support import Signature
-from pypy.objspace.std.floatobject import float2string as float2string_orig
 from pypy.rlib import jit
 from pypy.rlib.rfloat import DTSF_STR_PRECISION
 from pypy.rpython.lltypesystem import lltype
@@ -29,9 +28,6 @@
 def minimum(v1, v2):
     return min(v1, v2)
 
-def float2string(x):
-    return float2string_orig(x, 'g', DTSF_STR_PRECISION)
-
 class BaseArray(Wrappable):
     def __init__(self):
         self.invalidates = []
@@ -199,19 +195,20 @@
             return self.descr_mul(space, w_other)
 
     def _getnums(self, comma):
+        dtype = self.find_dtype()
         if self.find_size() > 1000:
             nums = [
-                float2string(self.eval(index))
+                dtype.str_format(self.eval(index))
                 for index in range(3)
             ]
             nums.append("..." + "," * comma)
             nums.extend([
-                float2string(self.eval(index))
+                dtype.str_format(self.eval(index))
                 for index in range(self.find_size() - 3, self.find_size())
             ])
         else:
             nums = [
-                float2string(self.eval(index))
+                dtype.str_format(self.eval(index))
                 for index in range(self.find_size())
             ]
         return nums
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
@@ -56,6 +56,10 @@
         assert repr(a) == "array([0.0, 1.0, 2.0, 3.0, 4.0])"
         a = zeros(1001)
         assert repr(a) == "array([0.0, 0.0, 0.0, ..., 0.0, 0.0, 0.0])"
+        a = array(range(5), 'l')
+        assert repr(a) == "array([0, 1, 2, 3, 4])"
+        a = array([True, False, True, False], "?")
+        assert repr(a) == "array([True, False, True, False])"
 
     def test_repr_slice(self):
         from numpy import array, zeros
@@ -73,6 +77,10 @@
         assert str((2*a)[:]) == "[0.0 2.0 4.0 6.0 8.0]"
         a = zeros(1001)
         assert str(a) == "[0.0 0.0 0.0 ..., 0.0 0.0 0.0]"
+        a = array(range(5), 'l')
+        assert str(a) == "[0 1 2 3 4]"
+        a = array([True, False, True, False], "?")
+        assert str(a) == "[True False True False]"
 
     def test_str_slice(self):
         from numpy import array, zeros
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to