[pypy-commit] pypy default: test/fix conversion of np.str to numeric types

bdkearns noreply at buildbot.pypy.org
Wed Dec 18 01:42:58 CET 2013


Author: Brian Kearns <bdkearns at gmail.com>
Branch: 
Changeset: r68451:6b8b4188b7ee
Date: 2013-12-17 19:28 -0500
http://bitbucket.org/pypy/pypy/changeset/6b8b4188b7ee/

Log:	test/fix conversion of np.str to numeric types

diff --git a/pypy/module/micronumpy/arrayimpl/concrete.py b/pypy/module/micronumpy/arrayimpl/concrete.py
--- a/pypy/module/micronumpy/arrayimpl/concrete.py
+++ b/pypy/module/micronumpy/arrayimpl/concrete.py
@@ -47,7 +47,7 @@
     def setslice(self, space, arr):
         impl = arr.implementation
         if impl.is_scalar():
-            self.fill(impl.get_scalar_value())
+            self.fill(space, impl.get_scalar_value())
             return
         shape = shape_agreement(space, self.get_shape(), arr)
         if impl.storage == self.storage:
@@ -100,7 +100,7 @@
         tmp = self.get_real(orig_array)
         tmp.setslice(space, convert_to_array(space, w_value))
 
-    def get_imag(self, orig_array):
+    def get_imag(self, space, orig_array):
         strides = self.get_strides()
         backstrides = self.get_backstrides()
         if self.dtype.is_complex_type():
@@ -110,11 +110,11 @@
         impl = NonWritableArray(self.get_shape(), self.dtype, self.order, strides,
                              backstrides)
         if not self.dtype.is_flexible_type():
-            impl.fill(self.dtype.box(0))
+            impl.fill(space, self.dtype.box(0))
         return impl
 
     def set_imag(self, space, orig_array, w_value):
-        tmp = self.get_imag(orig_array)
+        tmp = self.get_imag(space, orig_array)
         tmp.setslice(space, convert_to_array(space, w_value))
 
     # -------------------- applevel get/setitem -----------------------
@@ -357,7 +357,7 @@
                                          self.get_backstrides(),
                                          self.get_shape())
 
-    def fill(self, box):
+    def fill(self, space, box):
         self.dtype.itemtype.fill(self.storage, self.dtype.get_size(),
                                  box, 0, self.size, 0)
 
@@ -435,8 +435,8 @@
     def base(self):
         return self.orig_arr
 
-    def fill(self, box):
-        loop.fill(self, box.convert_to(self.dtype))
+    def fill(self, space, box):
+        loop.fill(self, box.convert_to(space, self.dtype))
 
     def create_iter(self, shape=None, backward_broadcast=False, require_index=False):
         if shape is not None and \
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
@@ -54,8 +54,7 @@
         return self.value
 
     def set_scalar_value(self, w_val):
-        assert isinstance(w_val, W_GenericBox)
-        self.value = w_val.convert_to(self.dtype)
+        self.value = w_val
 
     def copy(self, space):
         scalar = Scalar(self.dtype)
@@ -96,12 +95,12 @@
                     ','.join([str(x) for x in w_arr.get_shape()],))))
         if self.dtype.is_complex_type():
             self.value = self.dtype.itemtype.composite(
-                               w_arr.get_scalar_value().convert_to(dtype),
+                               w_arr.get_scalar_value().convert_to(space, dtype),
                                self.value.convert_imag_to(dtype))
         else:
             self.value = w_arr.get_scalar_value()
 
-    def get_imag(self, orig_array):
+    def get_imag(self, space, orig_array):
         if self.dtype.is_complex_type():
             scalar = Scalar(self.dtype.float_type)
             scalar.value = self.value.convert_imag_to(scalar.dtype)
@@ -125,7 +124,7 @@
                     ','.join([str(x) for x in w_arr.get_shape()],))))
         self.value = self.dtype.itemtype.composite(
                             self.value.convert_real_to(dtype),
-                            w_arr.get_scalar_value().convert_to(dtype),
+                            w_arr.get_scalar_value().convert_to(space, dtype),
                             )
 
     def descr_getitem(self, space, _, w_idx):
@@ -180,7 +179,7 @@
             w_res.implementation.setitem(0, index_type.itemtype.box(0)) 
         return space.newtuple([w_res])
 
-    def fill(self, w_value):
+    def fill(self, space, w_value):
         self.value = w_value
 
     def get_storage_as_int(self, space):
diff --git a/pypy/module/micronumpy/interp_arrayops.py b/pypy/module/micronumpy/interp_arrayops.py
--- a/pypy/module/micronumpy/interp_arrayops.py
+++ b/pypy/module/micronumpy/interp_arrayops.py
@@ -89,7 +89,7 @@
     shape = shape_agreement(space, arr.get_shape(), x)
     shape = shape_agreement(space, shape, y)
     out = W_NDimArray.from_shape(space, shape, dtype)
-    return loop.where(out, shape, arr, x, y, dtype)
+    return loop.where(space, out, shape, arr, x, y, dtype)
 
 def dot(space, w_obj1, w_obj2, w_out=None):
     w_arr = convert_to_array(space, w_obj1)
diff --git a/pypy/module/micronumpy/interp_boxes.py b/pypy/module/micronumpy/interp_boxes.py
--- a/pypy/module/micronumpy/interp_boxes.py
+++ b/pypy/module/micronumpy/interp_boxes.py
@@ -66,7 +66,7 @@
     def __init__(self, value):
         self.value = value
 
-    def convert_to(self, dtype):
+    def convert_to(self, space, dtype):
         return dtype.box(self.value)
 
     def __repr__(self):
@@ -91,7 +91,7 @@
         self.real = real
         self.imag = imag
 
-    def convert_to(self, dtype):
+    def convert_to(self, space, dtype):
         return dtype.box_complex(self.real, self.imag)
 
     def convert_real_to(self, dtype):
@@ -149,17 +149,17 @@
         return space.index(self.item(space))
 
     def descr_int(self, space):
-        box = self.convert_to(W_LongBox._get_dtype(space))
+        box = self.convert_to(space, W_LongBox._get_dtype(space))
         assert isinstance(box, W_LongBox)
         return space.wrap(box.value)
 
     def descr_long(self, space):
-        box = self.convert_to(W_Int64Box._get_dtype(space))
+        box = self.convert_to(space, W_Int64Box._get_dtype(space))
         assert isinstance(box, W_Int64Box)
         return space.wrap(box.value)
 
     def descr_float(self, space):
-        box = self.convert_to(W_Float64Box._get_dtype(space))
+        box = self.convert_to(space, W_Float64Box._get_dtype(space))
         assert isinstance(box, W_Float64Box)
         return space.wrap(box.value)
 
@@ -265,14 +265,14 @@
         if not space.is_none(w_out):
             raise OperationError(space.w_NotImplementedError, space.wrap(
                 "out not supported"))
-        v = self.convert_to(self.get_dtype(space))
+        v = self.convert_to(space, self.get_dtype(space))
         return self.get_dtype(space).itemtype.round(v, decimals)
 
     def descr_astype(self, space, w_dtype):
         from pypy.module.micronumpy.interp_dtype import W_Dtype
         dtype = space.interp_w(W_Dtype,
             space.call_function(space.gettypefor(W_Dtype), w_dtype))
-        return self.convert_to(dtype)
+        return self.convert_to(space, dtype)
 
     def descr_view(self, space, w_dtype):
         from pypy.module.micronumpy.interp_dtype import W_Dtype
@@ -311,7 +311,7 @@
         return space.wrap(0)
 
     def descr_copy(self, space):
-        return self.convert_to(self.get_dtype(space))
+        return self.convert_to(space, self.get_dtype(space))
 
     w_flags = None
     def descr_get_flags(self, space):
@@ -472,14 +472,13 @@
         dtype.itemtype.store(self.arr, self.ofs, ofs,
                              dtype.coerce(space, w_value))
 
-    def convert_to(self, dtype):
+    def convert_to(self, space, dtype):
         # if we reach here, the record fields are guarenteed to match.
         return self
 
 class W_CharacterBox(W_FlexibleBox):
-    def convert_to(self, dtype):
-        # XXX assert dtype is str type
-        return self
+    def convert_to(self, space, dtype):
+        return dtype.coerce(space, space.wrap(self.raw_str()))
 
 class W_StringBox(W_CharacterBox):
     def descr__new__string_box(space, w_subtype, w_arg):
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
@@ -94,7 +94,7 @@
         return space.wrap(self.get_size() * self.get_dtype().get_size())
 
     def descr_fill(self, space, w_value):
-        self.fill(self.get_dtype().coerce(space, w_value))
+        self.fill(space, self.get_dtype().coerce(space, w_value))
 
     def descr_tostring(self, space, w_order=None):
         order = order_converter(space, w_order, NPY_CORDER)
@@ -288,8 +288,8 @@
     def set_scalar_value(self, w_val):
         self.implementation.set_scalar_value(w_val)
 
-    def fill(self, box):
-        self.implementation.fill(box)
+    def fill(self, space, box):
+        self.implementation.fill(space, box)
 
     def descr_get_size(self, space):
         return space.wrap(self.get_size())
@@ -314,7 +314,7 @@
                          self.implementation.get_real(self))
 
     def descr_get_imag(self, space):
-        ret = self.implementation.get_imag(self)
+        ret = self.implementation.get_imag(space, self)
         return wrap_impl(space, space.type(self), self, ret)
 
     def descr_set_real(self, space, w_value):
@@ -1427,7 +1427,7 @@
         return W_NDimArray.new_scalar(space, dtype, space.wrap(0))
     w_arr = W_NDimArray.from_shape(space, shape, dtype=dtype, order=order)
     one = dtype.box(1)
-    w_arr.fill(one)
+    w_arr.fill(space, one)
     return space.wrap(w_arr)
 
 def _reconstruct(space, w_subtype, w_shape, w_dtype):
diff --git a/pypy/module/micronumpy/interp_ufuncs.py b/pypy/module/micronumpy/interp_ufuncs.py
--- a/pypy/module/micronumpy/interp_ufuncs.py
+++ b/pypy/module/micronumpy/interp_ufuncs.py
@@ -226,7 +226,7 @@
                 dtype = out.get_dtype()
             else:
                 out = W_NDimArray.from_shape(space, shape, dtype, w_instance=obj)
-            return loop.do_axis_reduce(shape, self.func, obj, dtype, axis, out,
+            return loop.do_axis_reduce(space, shape, self.func, obj, dtype, axis, out,
                                        self.identity, cumulative, temp)
         if cumulative:
             if out:
@@ -235,7 +235,7 @@
                         "out of incompatible size"))
             else:
                 out = W_NDimArray.from_shape(space, [obj.get_size()], dtype, w_instance=obj)
-            loop.compute_reduce_cumulative(obj, out, dtype, self.func,
+            loop.compute_reduce_cumulative(space, obj, out, dtype, self.func,
                                             self.identity)
             return out
         if out:
@@ -244,7 +244,7 @@
                               "for reduction operation %s has too many"
                               " dimensions",self.name)
             dtype = out.get_dtype()
-        res = loop.compute_reduce(obj, dtype, self.func, self.done_func,
+        res = loop.compute_reduce(space, obj, dtype, self.func, self.done_func,
                                   self.identity)
         if out:
             out.set_scalar_value(res)
@@ -303,13 +303,13 @@
                     res_dtype = interp_dtype.get_dtype_cache(space).w_float64dtype
         if w_obj.is_scalar():
             w_val = self.func(calc_dtype,
-                              w_obj.get_scalar_value().convert_to(calc_dtype))
+                              w_obj.get_scalar_value().convert_to(space, calc_dtype))
             if out is None:
                 return w_val
             if out.is_scalar():
                 out.set_scalar_value(w_val)
             else:
-                out.fill(res_dtype.coerce(space, w_val))
+                out.fill(space, res_dtype.coerce(space, w_val))
             return out
         shape = shape_agreement(space, w_obj.get_shape(), out,
                                 broadcast_down=False)
@@ -395,14 +395,14 @@
             res_dtype = calc_dtype
         if w_lhs.is_scalar() and w_rhs.is_scalar():
             arr = self.func(calc_dtype,
-                w_lhs.get_scalar_value().convert_to(calc_dtype),
-                w_rhs.get_scalar_value().convert_to(calc_dtype)
+                w_lhs.get_scalar_value().convert_to(space, calc_dtype),
+                w_rhs.get_scalar_value().convert_to(space, calc_dtype)
             )
             if isinstance(out, W_NDimArray):
                 if out.is_scalar():
                     out.set_scalar_value(arr)
                 else:
-                    out.fill(arr)
+                    out.fill(space, arr)
             else:
                 out = arr
             return out
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
@@ -58,10 +58,10 @@
                                      out=out,
                                      left_iter=left_iter, right_iter=right_iter,
                                      out_iter=out_iter)
-        w_left = left_iter.getitem().convert_to(calc_dtype)
-        w_right = right_iter.getitem().convert_to(calc_dtype)
+        w_left = left_iter.getitem().convert_to(space, calc_dtype)
+        w_right = right_iter.getitem().convert_to(space, calc_dtype)
         out_iter.setitem(func(calc_dtype, w_left, w_right).convert_to(
-            res_dtype))
+            space, res_dtype))
         left_iter.next()
         right_iter.next()
         out_iter.next()
@@ -84,8 +84,8 @@
                                      calc_dtype=calc_dtype, res_dtype=res_dtype,
                                      shape=shape, w_obj=w_obj, out=out,
                                      obj_iter=obj_iter, out_iter=out_iter)
-        elem = obj_iter.getitem().convert_to(calc_dtype)
-        out_iter.setitem(func(calc_dtype, elem).convert_to(res_dtype))
+        elem = obj_iter.getitem().convert_to(space, calc_dtype)
+        out_iter.setitem(func(calc_dtype, elem).convert_to(space, res_dtype))
         out_iter.next()
         obj_iter.next()
     return out
@@ -111,7 +111,7 @@
     shapelen = len(shape)
     while not target_iter.done():
         setslice_driver1.jit_merge_point(shapelen=shapelen, dtype=dtype)
-        target_iter.setitem(source_iter.getitem().convert_to(dtype))
+        target_iter.setitem(source_iter.getitem().convert_to(space, dtype))
         target_iter.next()
         source_iter.next()
     return target
@@ -135,20 +135,20 @@
                                         'calc_dtype'],
                               reds = 'auto')
 
-def compute_reduce(obj, calc_dtype, func, done_func, identity):
+def compute_reduce(space, obj, calc_dtype, func, done_func, identity):
     obj_iter = obj.create_iter()
     if identity is None:
-        cur_value = obj_iter.getitem().convert_to(calc_dtype)
+        cur_value = obj_iter.getitem().convert_to(space, calc_dtype)
         obj_iter.next()
     else:
-        cur_value = identity.convert_to(calc_dtype)
+        cur_value = identity.convert_to(space, calc_dtype)
     shapelen = len(obj.get_shape())
     while not obj_iter.done():
         reduce_driver.jit_merge_point(shapelen=shapelen, func=func,
                                       done_func=done_func,
                                       calc_dtype=calc_dtype,
                                       )
-        rval = obj_iter.getitem().convert_to(calc_dtype)
+        rval = obj_iter.getitem().convert_to(space, calc_dtype)
         if done_func is not None and done_func(calc_dtype, rval):
             return rval
         cur_value = func(calc_dtype, cur_value, rval)
@@ -159,22 +159,22 @@
                                   greens = ['shapelen', 'func', 'dtype'],
                                   reds = 'auto')
 
-def compute_reduce_cumulative(obj, out, calc_dtype, func, identity):
+def compute_reduce_cumulative(space, obj, out, calc_dtype, func, identity):
     obj_iter = obj.create_iter()
     out_iter = out.create_iter()
     if identity is None:
-        cur_value = obj_iter.getitem().convert_to(calc_dtype)
+        cur_value = obj_iter.getitem().convert_to(space, calc_dtype)
         out_iter.setitem(cur_value)
         out_iter.next()
         obj_iter.next()
     else:
-        cur_value = identity.convert_to(calc_dtype)
+        cur_value = identity.convert_to(space, calc_dtype)
     shapelen = len(obj.get_shape())
     while not obj_iter.done():
         reduce_cum_driver.jit_merge_point(shapelen=shapelen, func=func,
                                           dtype=calc_dtype,
                                          )
-        rval = obj_iter.getitem().convert_to(calc_dtype)
+        rval = obj_iter.getitem().convert_to(space, calc_dtype)
         cur_value = func(calc_dtype, cur_value, rval)
         out_iter.setitem(cur_value)
         out_iter.next()
@@ -190,7 +190,7 @@
                              greens = ['shapelen', 'dtype', 'arr_dtype'],
                              reds = 'auto')
 
-def where(out, shape, arr, x, y, dtype):
+def where(space, out, shape, arr, x, y, dtype):
     out_iter = out.create_iter(shape)
     arr_iter = arr.create_iter(shape)
     arr_dtype = arr.get_dtype()
@@ -209,9 +209,9 @@
                                         arr_dtype=arr_dtype)
         w_cond = arr_iter.getitem()
         if arr_dtype.itemtype.bool(w_cond):
-            w_val = x_iter.getitem().convert_to(dtype)
+            w_val = x_iter.getitem().convert_to(space, dtype)
         else:
-            w_val = y_iter.getitem().convert_to(dtype)
+            w_val = y_iter.getitem().convert_to(space, dtype)
         out_iter.setitem(w_val)
         out_iter.next()
         arr_iter.next()
@@ -224,7 +224,7 @@
                                             'func', 'dtype'],
                                     reds='auto')
 
-def do_axis_reduce(shape, func, arr, dtype, axis, out, identity, cumulative,
+def do_axis_reduce(space, shape, func, arr, dtype, axis, out, identity, cumulative,
                    temp):
     out_iter = out.create_axis_iter(arr.get_shape(), axis, cumulative)
     if cumulative:
@@ -233,7 +233,7 @@
         temp_iter = out_iter # hack
     arr_iter = arr.create_iter()
     if identity is not None:
-        identity = identity.convert_to(dtype)
+        identity = identity.convert_to(space, dtype)
     shapelen = len(shape)
     while not out_iter.done():
         axis_reduce__driver.jit_merge_point(shapelen=shapelen, func=func,
@@ -241,7 +241,7 @@
         if arr_iter.done():
             w_val = identity
         else:
-            w_val = arr_iter.getitem().convert_to(dtype)
+            w_val = arr_iter.getitem().convert_to(space, dtype)
             if out_iter.first_line:
                 if identity is not None:
                     w_val = func(dtype, identity, w_val)
@@ -316,11 +316,11 @@
     righti = right.create_dot_iter(broadcast_shape, right_skip)
     while not outi.done():
         dot_driver.jit_merge_point(dtype=dtype)
-        lval = lefti.getitem().convert_to(dtype)
-        rval = righti.getitem().convert_to(dtype)
-        outval = outi.getitem().convert_to(dtype)
+        lval = lefti.getitem().convert_to(space, dtype)
+        rval = righti.getitem().convert_to(space, dtype)
+        outval = outi.getitem().convert_to(space, dtype)
         v = dtype.itemtype.mul(lval, rval)
-        value = dtype.itemtype.add(v, outval).convert_to(dtype)
+        value = dtype.itemtype.add(v, outval).convert_to(space, dtype)
         outi.setitem(value)
         outi.next()
         righti.next()
@@ -457,7 +457,7 @@
     arr_iter.next_skip_x(start)
     while length > 0:
         flatiter_setitem_driver1.jit_merge_point(dtype=dtype)
-        arr_iter.setitem(val_iter.getitem().convert_to(dtype))
+        arr_iter.setitem(val_iter.getitem().convert_to(space, dtype))
         # need to repeat i_nput values until all assignments are done
         arr_iter.next_skip_x(step)
         length -= 1
@@ -610,7 +610,7 @@
                     index = 0
                 else:
                     index = len(iterators) - 1
-        out_iter.setitem(iterators[index].getitem().convert_to(dtype))
+        out_iter.setitem(iterators[index].getitem().convert_to(space, dtype))
         for iter in iterators:
             iter.next()
         out_iter.next()
@@ -629,9 +629,9 @@
     out_iter = out.create_iter(shape)
     while not arr_iter.done():
         clip_driver.jit_merge_point(shapelen=shapelen, dtype=dtype)
-        w_v = arr_iter.getitem().convert_to(dtype)
-        w_min = min_iter.getitem().convert_to(dtype)
-        w_max = max_iter.getitem().convert_to(dtype)
+        w_v = arr_iter.getitem().convert_to(space, dtype)
+        w_min = min_iter.getitem().convert_to(space, dtype)
+        w_max = max_iter.getitem().convert_to(space, dtype)
         if dtype.itemtype.lt(w_v, w_min):
             w_v = w_min
         elif dtype.itemtype.gt(w_v, w_max):
@@ -652,7 +652,7 @@
     out_iter = out.create_iter(shape)
     while not arr_iter.done():
         round_driver.jit_merge_point(shapelen=shapelen, dtype=dtype)
-        w_v = dtype.itemtype.round(arr_iter.getitem().convert_to(dtype),
+        w_v = dtype.itemtype.round(arr_iter.getitem().convert_to(space, dtype),
                      decimals)
         out_iter.setitem(w_v)
         arr_iter.next()
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
@@ -1973,6 +1973,12 @@
         else:
             raises(NotImplementedError, array(['1', '2', '3']).astype, float)
 
+        a = array('123')
+        assert a.astype('i8') == 123
+        a = array('abcdefgh')
+        exc = raises(ValueError, a.astype, 'i8')
+        assert exc.value.message.startswith('invalid literal for int()')
+
     def test_base(self):
         from numpypy import array
         assert array(1).base is None
diff --git a/pypy/module/micronumpy/test/test_scalar.py b/pypy/module/micronumpy/test/test_scalar.py
--- a/pypy/module/micronumpy/test/test_scalar.py
+++ b/pypy/module/micronumpy/test/test_scalar.py
@@ -20,6 +20,9 @@
 
     def test_builtin(self):
         import numpy as np
+        assert int(np.str_('12')) == 12
+        exc = raises(ValueError, "int(np.str_('abc'))")
+        assert exc.value.message.startswith('invalid literal for int()')
         assert oct(np.int32(11)) == '013'
         assert oct(np.float32(11.6)) == '013'
         assert oct(np.complex64(11-12j)) == '013'
@@ -77,6 +80,9 @@
         a = np.bool_(True).astype('int32')
         assert type(a) is np.int32
         assert a == 1
+        a = np.str_('123').astype('int32')
+        assert type(a) is np.int32
+        assert a == 123
 
     def test_copy(self):
         import numpy as np


More information about the pypy-commit mailing list