Author: Justin Peel <[email protected]>
Branch: numpy-dtype
Changeset: r46217:bb78cfeb8c6d
Date: 2011-08-02 13:22 -0600
http://bitbucket.org/pypy/pypy/changeset/bb78cfeb8c6d/
Log: Added ScalarWrapper. Added some more 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
@@ -2,6 +2,7 @@
from pypy.interpreter.error import OperationError
from pypy.interpreter.gateway import interp2app
from pypy.interpreter.typedef import TypeDef
+from pypy.rlib.rarithmetic import r_int, r_uint, LONG_BIT, LONGLONG_BIT
_letters_to_nums = [-1]*128
@@ -23,9 +24,19 @@
# typenums
Bool_num = 0
+Int8_num = 1
+UInt8_num = 2
+Int16_num = 3
+UInt16_num = 4
Int32_num = 5
UInt32_num = 6
+Long_num = 7
+ULong_num = 8
+Int64_num = 9
+UInt64_num = 10
+Float32_num = 11
Float64_num = 12
+Float128_num = 13
# dtype 'kinds'. Used to determine which operations can be performed on array
BOOLLTR = 'b'
@@ -39,12 +50,12 @@
# 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, typenum, kind):
+ def __init__(self, convfunc, wrapfunc, num, kind):
# doesn't handle align and copy parameters yet
# only deals with simple strings e.g., 'uint32', and type objects
- self.convfunc = convfunc
- self.wrapfunc = wrapfunc
- self.typenum = typenum
+ self.conv = convfunc
+ self.wrap = wrapfunc
+ self.num = num
self.kind = kind
def conv_float(space, val):
@@ -53,19 +64,34 @@
def wrap_float(space, val):
return space.float_w(val)
-def conv_int(space, val):
- return space.int(val)
+def conv_long(space, val):
+ return r_int(val)
def wrap_int(space, val):
return space.int_w(val)
+def conv_ulong(space, val):
+ return r_uint(val)
+
Float64_dtype = Dtype(conv_float, wrap_float, Float64_num,
FLOATINGLTR)
-Int32_dtype = Dtype(conv_int, wrap_int, Int32_num, SIGNEDLTR)
+#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)
_dtype_list = [None] * 14
_dtype_list[Float64_num] = Float64_dtype
-_dtype_list[Int32_num] = Int32_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
+
+def find_scalar_dtype(space, scalar):
+ if space.is_true(space.isinstance(scalar, space.w_int)):
+ return Long_dtype
+ if space.is_true(space.isinstance(scalar, space.w_float)):
+ return Float64_dtype
def get_dtype(space, w_type, w_string_or_type):
if space.is_true(space.isinstance(w_string_or_type,
space.gettypeobject(Dtype.typedef))):
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,13 +2,13 @@
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
+from pypy.module.micronumpy.interp_dtype import Dtype, Float64_num, Int32_num,
Float64_dtype, get_dtype, find_scalar_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
from pypy.rlib import jit
from pypy.rlib.rfloat import DTSF_STR_PRECISION
-from pypy.rpython.lltypesystem import lltype
+from pypy.rpython.lltypesystem import lltype, rffi
from pypy.tool.sourcetools import func_with_new_name
import math
@@ -17,8 +17,8 @@
None, # uint8
None, # int16
None, # uint16
- lltype.Array(lltype.Signed, hints={'nolength': True}), #int32
- lltype.Array(lltype.Unsigned, hints={'nolength': True}), # uint32
+ lltype.Array(rffi.INT, hints={'nolength': True}), #int32
+ lltype.Array(rffi.UINT, hints={'nolength': True}), # uint32
None, # long
None, # ulong
None, # longlong
@@ -83,7 +83,7 @@
def _binop_right_impl(w_ufunc):
def impl(self, space, w_other):
- w_other = FloatWrapper(space.float_w(w_other))
+ w_other = wrap_scalar(space, w_other, self.dtype)
return w_ufunc(space, w_other, self)
return func_with_new_name(impl, "binop_right_%s_impl" %
w_ufunc.__name__)
@@ -309,24 +309,35 @@
return new_numarray(space, w_obj, Float64_dtype)
else:
# If it's a scalar
- return FloatWrapper(space.float_w(w_obj))
+ return wrap_scalar(space, w_obj)
-class FloatWrapper(BaseArray):
+def wrap_scalar(space, scalar, dtype=None):
+ if dtype is None:
+ dtype = find_scalar_dtype(space, scalar)
+ return ScalarWrapper(dtype.wrap(space, scalar), dtype)
+
+class ScalarWrapper(BaseArray):
"""
Intermediate class representing a float literal.
"""
- _immutable_fields_ = ["float_value"]
+ _immutable_fields_ = ["value", "dtype"]
signature = Signature()
- def __init__(self, float_value):
+ def __init__(self, value, dtype):
BaseArray.__init__(self)
- self.float_value = float_value
+ self.value = value
+ self.dtype = dtype
def find_size(self):
raise ValueError
def eval(self, i):
- return self.float_value
+ return self.value
+
+# this is really only to simplify the tests. Maybe it should be moved?
+class FloatWrapper(ScalarWrapper):
+ def __init__(self, value):
+ ScalarWrapper.__init__(self, value, Float64_dtype)
class VirtualArray(BaseArray):
"""
@@ -505,7 +516,7 @@
BaseArray.__init__(self)
self.size = size
self.dtype = dtype
- TP = TPs[dtype.typenum]
+ TP = TPs[dtype.num]
self.storage = lltype.malloc(TP, size, zero=True,
flavor='raw', track_allocation=False,
add_memory_pressure=True)
@@ -547,10 +558,10 @@
dtype = get_dtype(space, Dtype, dtype)
arr = SingleDimArray(len(l), dtype)
i = 0
- convfunc = dtype.convfunc
- wrapfunc = dtype.wrapfunc
+ conv = dtype.conv
+ wrap = dtype.wrap
for w_elem in l:
- arr.storage[i] = wrapfunc(space, convfunc(space, w_elem))
+ arr.storage[i] = wrap(space, conv(space, w_elem))
i += 1
return arr
diff --git a/pypy/module/micronumpy/test/test_base.py
b/pypy/module/micronumpy/test/test_base.py
--- a/pypy/module/micronumpy/test/test_base.py
+++ b/pypy/module/micronumpy/test/test_base.py
@@ -1,5 +1,5 @@
from pypy.conftest import gettestobjspace
-from pypy.module.micronumpy.interp_numarray import SingleDimArray, FloatWrapper
+from pypy.module.micronumpy.interp_numarray import SingleDimArray,
FloatWrapper, ScalarWrapper
from pypy.module.micronumpy.interp_dtype import Float64_dtype
class BaseNumpyAppTest(object):
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
@@ -5,4 +5,4 @@
class AppTestDtype(BaseNumpyAppTest):
def test_dtype(self):
from numpy import dtype
- d = dtype('i')
+ d = dtype('l')
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit