Author: Justin Peel <[email protected]>
Branch: numpy-dtype
Changeset: r46221:4145ce2b3d99
Date: 2011-08-02 18:17 -0600
http://bitbucket.org/pypy/pypy/changeset/4145ce2b3d99/
Log: got one type of int working. binary operations appear to work with
mixed 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
@@ -3,6 +3,7 @@
from pypy.interpreter.gateway import interp2app
from pypy.interpreter.typedef import TypeDef, GetSetProperty
from pypy.rlib.rarithmetic import r_int, r_uint, LONG_BIT, LONGLONG_BIT
+from pypy.rpython.lltypesystem import lltype, rffi
_letters_to_nums = [-1]*128
@@ -50,11 +51,11 @@
# fields, names, f?, metadata. I'll just implement the base minimum for
# now. This will include type, kind, typeobj?, byteorder, type_num, elsize,
#
- def __init__(self, convfunc, wrapfunc, num, kind):
+ def __init__(self, castfunc, unwrapfunc, num, kind):
# doesn't handle align and copy parameters yet
# only deals with simple strings e.g., 'uint32', and type objects
- self.conv = convfunc
- self.wrap = wrapfunc
+ self.cast = castfunc
+ self.unwrap = unwrapfunc
self.num = num
self.kind = kind
@@ -64,27 +65,27 @@
def descr_kind(self, space):
return space.wrap(self.kind)
-def conv_float(space, val):
- return space.float(val)
+def cast_float(val):
+ return rffi.cast(lltype.Float, val)
-def wrap_float(space, val):
- return space.float_w(val)
+def unwrap_float(space, val):
+ return space.float_w(space.float(val))
-def conv_long(space, val):
- return r_int(val)
+def cast_long(val):
+ return rffi.cast(rffi.INT, val)
-def wrap_int(space, val):
- return space.int_w(val)
+def unwrap_int(space, val):
+ return space.int_w(space.int(val))
-def conv_ulong(space, val):
- return r_uint(val)
+def cast_ulong(val):
+ return rffi.cast(rffi.UINT, val)
-Float64_dtype = Dtype(conv_float, wrap_float, Float64_num,
+Float64_dtype = Dtype(cast_float, unwrap_float, Float64_num,
FLOATINGLTR)
-#Int32_dtype = Dtype(conv_int32, wrap_int, Int32_num, SIGNEDLTR)
-#UInt32_dtype = Dtype(conv_uint32, wrap_int, UIn32_num, UNSIGNEDLTR)
-Long_dtype = Dtype(conv_long, wrap_int, Long_num, SIGNEDLTR)
-ULong_dtype = Dtype(conv_long, wrap_int, Long_num, UNSIGNEDLTR)
+#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
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
@@ -19,7 +19,7 @@
None, # uint16
lltype.Array(rffi.INT, hints={'nolength': True}), #int32
lltype.Array(rffi.UINT, hints={'nolength': True}), # uint32
- None, # long
+ lltype.Array(rffi.LONG, hints={'nolength': True}), # long
None, # ulong
None, # longlong
None, # ulonglong
@@ -314,7 +314,7 @@
def wrap_scalar(space, scalar, dtype=None):
if dtype is None:
dtype = find_scalar_dtype(space, scalar)
- return ScalarWrapper(dtype.wrap(space, scalar), dtype)
+ return ScalarWrapper(dtype.unwrap(space, scalar), dtype)
class ScalarWrapper(BaseArray):
"""
@@ -422,12 +422,11 @@
self.right = right
dtype = self.left.find_dtype()
dtype2 = self.right.find_dtype()
- if dtype.num >= dtype2.num:
- self.dtype = dtype
- elif dtype.num < dtype2.num:
- self.dtype = dtype2
- else:
- self.dtype = dtype
+ # this is more complicated than this.
+ # for instance int32 + uint32 = int64
+ if dtype.num != dtype.num and dtype.num < dtype2.num:
+ dtype = dtype2
+ self.dtype = dtype
def _del_sources(self):
self.left = None
@@ -566,15 +565,17 @@
dtype = get_dtype(space, Dtype, dtype)
arr = SingleDimArray(len(l), dtype)
i = 0
- conv = dtype.conv
- wrap = dtype.wrap
+ unwrap = dtype.unwrap
+ # the types seem to be casting on their own so I've omitted the cast for
now
+ #cast = dtype.cast
for w_elem in l:
- arr.storage[i] = wrap(space, conv(space, w_elem))
+ arr.storage[i] = unwrap(space, w_elem)
i += 1
return arr
def descr_new_numarray(space, w_type, __args__):
# this isn't such a great check. We should improve it including exceptions.
+ # Also needs to be able to handle keywords
iterable = __args__.arguments_w[0]
if len(__args__.arguments_w) == 2:
dtype = __args__.arguments_w[1]
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,3 +8,15 @@
d = dtype('l')
assert d.num == 7
assert d.kind == 'i'
+
+ def test_too_large_int(self):
+ from numpy import array
+ # only one 32-bit system for now.. will change to 'i' when we can
+ raises(OverflowError, "array([2147483648], 'l')")
+
+ def test_int_array(self):
+ from numpy import array
+ a = array([1.5, 2.5, 3.5], 'l')
+ assert a[0] == 1
+ assert a[1] == 2
+ assert a[2] == 3
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
@@ -74,6 +74,16 @@
for i in range(3):
assert c[i] == a[i] + b[i]
+ def test_add_types(self):
+ from numpy import array, add
+ a = array([-5.0, -0.0, 1.0], 'l')
+ 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]
+
def test_divide(self):
from numpy import array, divide
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit