[pypy-commit] pypy numpy-dtype: add bool, int64, and uint64 types
justinpeel
noreply at buildbot.pypy.org
Wed Aug 3 04:03:04 CEST 2011
Author: Justin Peel <notmuchtotell at gmail.com>
Branch: numpy-dtype
Changeset: r46223:738b3abe41b1
Date: 2011-08-02 19:43 -0600
http://bitbucket.org/pypy/pypy/changeset/738b3abe41b1/
Log: add bool, int64, and uint64 types
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,12 +64,19 @@
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 unwrap_bool(space, val):
+ return space.is_true(val)
+
+def cast_bool(val):
+ return rffi.cast(lltype.Bool, val)
+
def cast_int8(val):
return rffi.cast(rffi.SIGNEDCHAR, val)
@@ -82,24 +89,36 @@
def cast_uint16(val):
return rffi.cast(rffi.USHORT, val)
-def cast_int(val):
+def cast_long(val):
return rffi.cast(rffi.LONG, val)
+def cast_ulong(val):
+ return rffi.cast(rffi.ULONG, val)
+
+def cast_int64(val):
+ return rffi.cast(rffi.LONGLONG, val)
+
+def cast_uint64(val):
+ return rffi.cast(rffi.ULONGLONG, val)
+
def cast_float(val):
return rffi.cast(lltype.Float, val)
+Bool_dtype = Dtype(cast_bool, unwrap_bool, Bool_num, BOOLLTR)
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)
+Long_dtype = Dtype(cast_long, unwrap_int, Long_num, SIGNEDLTR)
+ULong_dtype = Dtype(cast_ulong, unwrap_int, ULong_num, UNSIGNEDLTR)
+Int64_dtype = Dtype(cast_int64, unwrap_int, Int64_num, SIGNEDLTR)
+UInt64_dtype = Dtype(cast_uint64, unwrap_int, UInt64_num, UNSIGNEDLTR)
Float64_dtype = Dtype(cast_float, unwrap_float, Float64_num,
FLOATINGLTR)
-_dtype_list = (None, # bool
+_dtype_list = (Bool_dtype, # bool
Int8_dtype,
UInt8_dtype,
Int16_dtype,
@@ -108,8 +127,8 @@
None,
Long_dtype,
ULong_dtype,
- None,
- None,
+ Int64_dtype,
+ UInt64_dtype,
None,
Float64_dtype,
None,
@@ -141,6 +160,14 @@
raise OperationError(space.w_TypeError,
space.wrap("data type not understood"))
+def find_base_dtype(dtype1, dtype2):
+ num1 = dtype1.num
+ num2 = dtype2.num
+ # this is much more complex
+ if num1 < num2:
+ return dtype2
+ return dtype
+
def descr_new_dtype(space, w_type, w_string_or_type):
return space.wrap(get_dtype(space, w_type, w_string_or_type))
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
@@ -2,7 +2,7 @@
from pypy.interpreter.error import OperationError, operationerrfmt
from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.interpreter.typedef import TypeDef, GetSetProperty
-from pypy.module.micronumpy.interp_dtype import Dtype, Float64_num, Int32_num, Float64_dtype, get_dtype, find_scalar_dtype
+from pypy.module.micronumpy.interp_dtype import Dtype, Float64_num, Int32_num, Float64_dtype, get_dtype, find_scalar_dtype, find_base_dtype
from pypy.module.micronumpy.interp_support import Signature
from pypy.module.micronumpy import interp_ufuncs
from pypy.objspace.std.floatobject import float2string as float2string_orig
@@ -21,8 +21,8 @@
lltype.Array(rffi.UINT, hints={'nolength': True}), # uint32
lltype.Array(rffi.LONG, hints={'nolength': True}), # long
lltype.Array(rffi.ULONG, hints={'nolength': True}), # ulong
- None, # longlong
- None, # ulonglong
+ lltype.Array(rffi.LONGLONG, hints={'nolength': True}), # longlong
+ lltype.Array(rffi.ULONGLONG, hints={'nolength': True}), # ulonglong
lltype.Array(lltype.SingleFloat, hints={'nolength': True}), # float32
lltype.Array(lltype.Float, hints={'nolength': True}), # float64
None, # float128
@@ -424,8 +424,8 @@
dtype2 = self.right.find_dtype()
# this is more complicated than this.
# for instance int32 + uint32 = int64
- if dtype.num != dtype.num and dtype.num < dtype2.num:
- dtype = dtype2
+ if dtype.num != dtype.num:
+ dtype = find_base_dtype(dtype, dtype2)
self.dtype = dtype
def _del_sources(self):
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
@@ -8,15 +8,24 @@
d = dtype('l')
assert d.num == 7
assert d.kind == 'i'
+
+ def test_bool_array(self):
+ from numpy import array
+ a = array([0, 1, 2, 2.5], '?')
+ assert a[0] == False
+ for i in xrange(1, 4):
+ assert a[i] == True
def test_overflow(self):
from numpy import array
# only one 32-bit system for now.. will change to 'i' when we can
+ assert array([3], '?')[0] == True
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')")
+ raises(OverflowError, "array([9223372036854775808], 'q')")
def test_int_array(self):
from numpy import array
More information about the pypy-commit
mailing list