Author: Maciej Fijalkowski <[email protected]>
Branch: numpy-refactor
Changeset: r56932:0bfb2e639982
Date: 2012-08-30 14:23 +0200
http://bitbucket.org/pypy/pypy/changeset/0bfb2e639982/
Log: progress with scalars
diff --git a/pypy/module/micronumpy/arrayimpl/__init__.py
b/pypy/module/micronumpy/arrayimpl/__init__.py
--- a/pypy/module/micronumpy/arrayimpl/__init__.py
+++ b/pypy/module/micronumpy/arrayimpl/__init__.py
@@ -6,3 +6,4 @@
return scalar.Scalar(dtype)
else:
return concrete.ConcreteArray(shape, dtype, order)
+
diff --git a/pypy/module/micronumpy/arrayimpl/base.py
b/pypy/module/micronumpy/arrayimpl/base.py
--- a/pypy/module/micronumpy/arrayimpl/base.py
+++ b/pypy/module/micronumpy/arrayimpl/base.py
@@ -8,3 +8,6 @@
def setitem(self, elem):
raise NotImplementedError
+
+ def set_scalar_object(self, value):
+ raise NotImplementedError # works only on scalars
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
@@ -5,7 +5,10 @@
is_scalar = True
def __init__(self, dtype):
- pass
+ self.value = None
def get_shape(self):
return []
+
+ def set_scalar_value(self, value):
+ self.value = value
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
@@ -17,13 +17,17 @@
shape.append(space.int_w(w_item))
return shape
+def scalar_w(space, dtype, w_object):
+ arr = W_NDimArray([], dtype)
+ arr.implementation.set_scalar_value(w_object)
+ return arr
+
class W_NDimArray(Wrappable):
def __init__(self, shape, dtype, buffer=0, offset=0, strides=None,
order='C'):
if strides is not None or offset != 0 or buffer != 0:
raise Exception("unsupported args")
self.implementation = create_implementation(shape, dtype, order)
- self.dtype = dtype
@jit.unroll_safe
def descr_get_shape(self, space):
@@ -37,8 +41,14 @@
self.implementation = self.implementation.set_shape(
_find_shape(space, w_new_shape))
+ def get_dtype(self):
+ return self.implementation.dtype
+
def descr_get_dtype(self, space):
- return self.dtype
+ return self.implementation.dtype
+
+ def descr_get_ndim(self, space):
+ return space.wrap(len(self.get_shape()))
def create_iter(self):
return self.implementation.create_iter()
@@ -71,11 +81,21 @@
dtype = GetSetProperty(W_NDimArray.descr_get_dtype),
shape = GetSetProperty(W_NDimArray.descr_get_shape,
W_NDimArray.descr_set_shape),
+ ndim = GetSetProperty(W_NDimArray.descr_get_ndim),
)
+def decode_w_dtype(space, w_dtype):
+ if w_dtype is None or space.is_w(w_dtype, space.w_None):
+ return None
+ return space.interp_w(interp_dtype.W_Dtype,
+ space.call_function(space.gettypefor(interp_dtype.W_Dtype), w_dtype))
+
@unwrap_spec(ndmin=int, copy=bool, subok=bool)
def array(space, w_object, w_dtype=None, copy=True, w_order=None, subok=False,
ndmin=0):
+ if not space.issequence_w(w_object):
+ dtype = decode_w_dtype(space, w_dtype)
+ return scalar_w(space, dtype, w_object)
if w_order is None or space.is_w(w_order, space.w_None):
order = 'C'
else:
@@ -91,11 +111,7 @@
if copy:
return w_object.copy(space)
return w_object
- if w_dtype is None or space.is_w(w_dtype, space.w_None):
- dtype = None
- else:
- dtype = space.interp_w(interp_dtype.W_Dtype,
- space.call_function(space.gettypefor(interp_dtype.W_Dtype),
w_dtype))
+ dtype = decode_w_dtype(space, w_dtype)
shape, elems_w = find_shape_and_elems(space, w_object, dtype)
if dtype is None:
for w_elem in elems_w:
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
@@ -306,7 +306,7 @@
if space.is_w(w_out, space.w_None) or w_out is None:
out = None
calc_dtype = find_binop_result_dtype(space,
- w_lhs.dtype, w_rhs.dtype,
+ w_lhs.get_dtype(), w_rhs.get_dtype(),
int_only=self.int_only,
promote_to_float=self.promote_to_float,
promote_bools=self.promote_bools,
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit