[pypy-commit] pypy numpy-dtype: added int8, uint8, int16, uint16 dtypes. Added back in some explicit casting.

justinpeel noreply at buildbot.pypy.org
Wed Aug 3 03:04:59 CEST 2011


Author: Justin Peel <notmuchtotell at gmail.com>
Branch: numpy-dtype
Changeset: r46222:e1c6862ff132
Date: 2011-08-02 19:05 -0600
http://bitbucket.org/pypy/pypy/changeset/e1c6862ff132/

Log:	added int8, uint8, int16, uint16 dtypes. Added back in some explicit
	casting.

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
@@ -64,35 +64,56 @@
 
     def descr_kind(self, space):
         return space.wrap(self.kind)
+def unwrap_float(space, val):
+    return space.float_w(space.float(val))
+
+def unwrap_int(space, val):
+    return space.int_w(space.int(val))
+
+def cast_int8(val):
+    return rffi.cast(rffi.SIGNEDCHAR, val)
+
+def cast_uint8(val):
+    return rffi.cast(rffi.UCHAR, val)
+
+def cast_int16(val):
+    return rffi.cast(rffi.SHORT, val)
+
+def cast_uint16(val):
+    return rffi.cast(rffi.USHORT, val)
+
+def cast_int(val):
+    return rffi.cast(rffi.LONG, val)
 
 def cast_float(val):
     return rffi.cast(lltype.Float, val)
 
-def unwrap_float(space, val):
-    return space.float_w(space.float(val))
-
-def cast_long(val):
-    return rffi.cast(rffi.INT, val)
-
-def unwrap_int(space, val):
-    return space.int_w(space.int(val))
-
-def cast_ulong(val):
-    return rffi.cast(rffi.UINT, val)
-
+Int8_dtype = Dtype(cast_int8, unwrap_int, Int8_num, SIGNEDLTR)
+UInt8_dtype = Dtype(cast_uint8, unwrap_int, UInt8_num, SIGNEDLTR)
+Int16_dtype = Dtype(cast_int16, unwrap_int, Int16_num, SIGNEDLTR)
+UInt16_dtype = Dtype(cast_uint16, unwrap_int, UInt16_num, SIGNEDLTR)
+#Int32_dtype = Dtype(cast_int32, unwrap_int, Int32_num, SIGNEDLTR)
+#UInt32_dtype = Dtype(cast_uint32, unwrap_int, UIn32_num, UNSIGNEDLTR)
+Long_dtype = Dtype(cast_int, unwrap_int, Long_num, SIGNEDLTR)
+ULong_dtype = Dtype(cast_int, unwrap_int, Long_num, UNSIGNEDLTR)
 Float64_dtype = Dtype(cast_float, unwrap_float, Float64_num,
                         FLOATINGLTR)
-#Int32_dtype = Dtype(cast_int32, unwrap_int, Int32_num, SIGNEDLTR)
-#UInt32_dtype = Dtype(cast_uint32, unwrap_int, UIn32_num, UNSIGNEDLTR)
-Long_dtype = Dtype(cast_long, unwrap_int, Long_num, SIGNEDLTR)
-ULong_dtype = Dtype(cast_ulong, unwrap_int, Long_num, UNSIGNEDLTR)
 
-_dtype_list = [None] * 14
-_dtype_list[Float64_num] = Float64_dtype
-#_dtype_list[Int32_num] = Int32_dtype
-#_dtype_list[UInt32_num] = UInt32_dtype
-_dtype_list[Long_num] = Long_dtype
-_dtype_list[ULong_num] = ULong_dtype
+_dtype_list = (None, # bool
+               Int8_dtype,
+               UInt8_dtype,
+               Int16_dtype,
+               UInt16_dtype,
+               None,
+               None,
+               Long_dtype,
+               ULong_dtype,
+               None,
+               None,
+               None,
+               Float64_dtype,
+               None,
+)
 
 def find_scalar_dtype(space, scalar):
     if space.is_true(space.isinstance(scalar, space.w_int)):
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
@@ -13,17 +13,17 @@
 import math
 
 TPs = (lltype.Array(lltype.Bool, hints={'nolength': True}), # bool
-       None, # int8
-       None, # uint8
-       None, # int16
-       None, # uint16
+       lltype.Array(rffi.SIGNEDCHAR, hints={'nolength': True}), # int8
+       lltype.Array(rffi.UCHAR, hints={'nolength': True}), # uint8
+       lltype.Array(rffi.SHORT, hints={'nolength': True}), # int16
+       lltype.Array(rffi.USHORT, hints={'nolength': True}), # uint16
        lltype.Array(rffi.INT, hints={'nolength': True}), #int32
        lltype.Array(rffi.UINT, hints={'nolength': True}), # uint32
        lltype.Array(rffi.LONG, hints={'nolength': True}), # long
-       None, # ulong
+       lltype.Array(rffi.ULONG, hints={'nolength': True}), # ulong
        None, # longlong
        None, # ulonglong
-       None, # float32
+       lltype.Array(lltype.SingleFloat, hints={'nolength': True}), # float32
        lltype.Array(lltype.Float, hints={'nolength': True}), # float64
        None, # float128
 )
@@ -566,10 +566,9 @@
     arr = SingleDimArray(len(l), dtype)
     i = 0
     unwrap = dtype.unwrap
-    # the types seem to be casting on their own so I've omitted the cast for now
-    #cast = dtype.cast
+    cast = dtype.cast
     for w_elem in l:
-        arr.storage[i] = unwrap(space, w_elem)
+        arr.storage[i] = cast(unwrap(space, w_elem))
         i += 1
     return arr
 
diff --git a/pypy/module/micronumpy/test/test_dtypes.py b/pypy/module/micronumpy/test/test_dtypes.py
--- a/pypy/module/micronumpy/test/test_dtypes.py
+++ b/pypy/module/micronumpy/test/test_dtypes.py
@@ -9,9 +9,13 @@
         assert d.num == 7
         assert d.kind == 'i'
 
-    def test_too_large_int(self):
+    def test_overflow(self):
         from numpy import array
         # only one 32-bit system for now.. will change to 'i' when we can
+        assert array([128], 'b')[0] == -128
+        assert array([256], 'B')[0] == 0
+        assert array([32768], 'h')[0] == -32768
+        assert array([65536], 'H')[0] == 0
         raises(OverflowError, "array([2147483648], 'l')")
 
     def test_int_array(self):
diff --git a/pypy/module/micronumpy/test/test_ufuncs.py b/pypy/module/micronumpy/test/test_ufuncs.py
--- a/pypy/module/micronumpy/test/test_ufuncs.py
+++ b/pypy/module/micronumpy/test/test_ufuncs.py
@@ -76,10 +76,9 @@
 
     def test_add_types(self):
         from numpy import array, add
-        a = array([-5.0, -0.0, 1.0], 'l')
+        a = array([-5.2, -0.0, 1.0], 'd')
         b = array([3, -2, 3], 'l')
         c = add(a, b)
-        print c
         assert c.dtype is a.dtype
         for i in range(3):
             assert c[i] == a[i] + b[i]


More information about the pypy-commit mailing list