[pypy-commit] pypy numpy-cleanup: progress on cleanups
fijal
noreply at buildbot.pypy.org
Tue Jul 17 19:12:15 CEST 2012
Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: numpy-cleanup
Changeset: r56113:88e42829e970
Date: 2012-07-17 19:11 +0200
http://bitbucket.org/pypy/pypy/changeset/88e42829e970/
Log: progress on cleanups
diff --git a/pypy/module/_numpypy/strides.py b/pypy/module/_numpypy/+strides.py
rename from pypy/module/_numpypy/strides.py
rename to pypy/module/_numpypy/+strides.py
diff --git a/pypy/module/_numpypy/__init__.py b/pypy/module/_numpypy/__init__.py
--- a/pypy/module/_numpypy/__init__.py
+++ b/pypy/module/_numpypy/__init__.py
@@ -19,6 +19,7 @@
interpleveldefs = {
'ndarray': 'interp_numarray.W_NDArray',
'array': 'interp_numarray.descr_array',
+ 'dtype': 'interp_dtype.W_Dtype',
}
appleveldefs = {}
diff --git a/pypy/module/_numpypy/interp_numarray.py b/pypy/module/_numpypy/interp_numarray.py
--- a/pypy/module/_numpypy/interp_numarray.py
+++ b/pypy/module/_numpypy/interp_numarray.py
@@ -1,30 +1,101 @@
from pypy.interpreter.baseobjspace import Wrappable
-from pypy.interpreter.typedef import TypeDef
-from pypy.interpreter.gateway import unwrap_spec
+from pypy.interpreter.typedef import TypeDef, GetSetProperty
+from pypy.interpreter.gateway import unwrap_spec, interp2app
from pypy.interpreter.error import operationerrfmt
+from pypy.module._numpypy import interp_dtype, strides, interp_ufuncs
+from pypy.tool.sourcetools import func_with_new_name
+
class W_NDArray(Wrappable):
- def __init__(self, impl):
+ def __init__(self, impl, dtype):
self.impl = impl
+ self.dtype = dtype
+
+ def descr_get_shape(self, space):
+ return space.newtuple([space.wrap(i) for i in self.impl.getshape()])
+
+ def descr_get_dtype(self, space):
+ return self.dtype
+
+ def _binop_impl(ufunc_name):
+ def impl(self, space, w_other, w_out=None):
+ return getattr(interp_ufuncs.get(space), ufunc_name).call(space,
+ [self, w_other, w_out])
+ return func_with_new_name(impl, "binop_%s_impl" % ufunc_name)
+
+ descr_add = _binop_impl("add")
+ descr_sub = _binop_impl("subtract")
+ descr_mul = _binop_impl("multiply")
+ descr_div = _binop_impl("divide")
+ descr_truediv = _binop_impl("true_divide")
+ descr_floordiv = _binop_impl("floor_divide")
+ descr_mod = _binop_impl("mod")
+ descr_pow = _binop_impl("power")
+ descr_lshift = _binop_impl("left_shift")
+ descr_rshift = _binop_impl("right_shift")
+ descr_and = _binop_impl("bitwise_and")
+ descr_or = _binop_impl("bitwise_or")
+ descr_xor = _binop_impl("bitwise_xor")
+
+ def descr_divmod(self, space, w_other):
+ w_quotient = self.descr_div(space, w_other)
+ w_remainder = self.descr_mod(space, w_other)
+ return space.newtuple([w_quotient, w_remainder])
+
+ descr_eq = _binop_impl("equal")
+ descr_ne = _binop_impl("not_equal")
+ descr_lt = _binop_impl("less")
+ descr_le = _binop_impl("less_equal")
+ descr_gt = _binop_impl("greater")
+ descr_ge = _binop_impl("greater_equal")
class BaseArrayImpl(object):
pass
class Scalar(BaseArrayImpl):
- pass
+ def getshape(self):
+ return []
class ConcreteArray(BaseArrayImpl):
def __init__(self, shape):
self.shape = shape
+ def getshape(self):
+ return self.shape
+
+def descr_new_array(space, w_subtype, w_size, w_dtype=None):
+ dtype = space.interp_w(interp_dtype.W_Dtype,
+ space.call_function(space.gettypefor(interp_dtype.W_Dtype),
+ w_dtype))
+ shape = strides.find_shape_from_scalar(space, w_size)
+ return space.wrap(W_NDArray(ConcreteArray(shape), dtype=dtype))
+
W_NDArray.typedef = TypeDef('ndarray',
__module__ = 'numpypy',
+ __new__ = interp2app(descr_new_array),
+ shape = GetSetProperty(W_NDArray.descr_get_shape),
+ dtype = GetSetProperty(W_NDArray.descr_get_dtype),
+
+ __add__ = interp2app(W_NDArray.descr_add),
+ __sub__ = interp2app(W_NDArray.descr_sub),
+ __mul__ = interp2app(W_NDArray.descr_mul),
+ __div__ = interp2app(W_NDArray.descr_div),
+ __truediv__ = interp2app(W_NDArray.descr_truediv),
+ __floordiv__ = interp2app(W_NDArray.descr_floordiv),
+ __mod__ = interp2app(W_NDArray.descr_mod),
+ __divmod__ = interp2app(W_NDArray.descr_divmod),
+ __pow__ = interp2app(W_NDArray.descr_pow),
+ __lshift__ = interp2app(W_NDArray.descr_lshift),
+ __rshift__ = interp2app(W_NDArray.descr_rshift),
+ __and__ = interp2app(W_NDArray.descr_and),
+ __or__ = interp2app(W_NDArray.descr_or),
+ __xor__ = interp2app(W_NDArray.descr_xor),
)
@unwrap_spec(subok=bool, copy=bool, ownmaskna=bool)
def descr_array(space, w_item_or_iterable, w_dtype=None, copy=True,
- w_order=None, subok=False, ndmin=0, w_maskna=None,
+ w_order=None, subok=False, w_ndmin=None, w_maskna=None,
ownmaskna=False):
# find scalar
if w_maskna is None:
@@ -32,10 +103,7 @@
if subok or not space.is_w(w_maskna, space.w_None) or ownmaskna:
raise operationerrfmt(space.w_NotImplementedError,
"Unsupported args")
- xxx
-
-
- if not space.issequence_w(w_item_or_iterable):
+ if not strides.is_list_or_tuple(space, w_item_or_iterable):
if w_dtype is None or space.is_w(w_dtype, space.w_None):
w_dtype = interp_ufuncs.find_dtype_for_scalar(space,
w_item_or_iterable)
@@ -50,7 +118,7 @@
if order != 'C': # or order != 'F':
raise operationerrfmt(space.w_ValueError, "Unknown order: %s",
order)
- if isinstance(w_item_or_iterable, BaseArray):
+ if isinstance(w_item_or_iterable, W_NDArray):
if (not space.is_w(w_dtype, space.w_None) and
w_item_or_iterable.find_dtype() is not w_dtype):
raise OperationError(space.w_NotImplementedError, space.wrap(
@@ -63,7 +131,8 @@
else:
dtype = space.interp_w(interp_dtype.W_Dtype,
space.call_function(space.gettypefor(interp_dtype.W_Dtype), w_dtype))
- shape, elems_w = find_shape_and_elems(space, w_item_or_iterable, dtype)
+ shape, elems_w = strides.find_shape_and_elems(space, w_item_or_iterable,
+ dtype)
# they come back in C order
if dtype is None:
for w_elem in elems_w:
@@ -79,12 +148,12 @@
if ndmin > shapelen:
shape = [1] * (ndmin - shapelen) + shape
shapelen = ndmin
- arr = W_NDimArray(shape[:], dtype=dtype, order=order)
- arr_iter = arr.create_iter()
+ arr = W_NDArray(ConcreteArray(shape), dtype=dtype)
+ #arr_iter = arr.create_iter()
# XXX we might want to have a jitdriver here
- for i in range(len(elems_w)):
- w_elem = elems_w[i]
- dtype.setitem(arr, arr_iter.offset,
- dtype.coerce(space, w_elem))
- arr_iter = arr_iter.next(shapelen)
+ #for i in range(len(elems_w)):
+ # w_elem = elems_w[i]
+ # dtype.setitem(arr, arr_iter.offset,
+ # dtype.coerce(space, w_elem))
+ # arr_iter = arr_iter.next(shapelen)
return arr
diff --git a/pypy/module/_numpypy/interp_ufuncs.py b/pypy/module/_numpypy/interp_ufuncs.py
--- a/pypy/module/_numpypy/interp_ufuncs.py
+++ b/pypy/module/_numpypy/interp_ufuncs.py
@@ -2,7 +2,7 @@
from pypy.interpreter.error import OperationError, operationerrfmt
from pypy.interpreter.gateway import interp2app, unwrap_spec, NoneNotWrapped
from pypy.interpreter.typedef import TypeDef, GetSetProperty, interp_attrproperty
-from pypy.module._numpypy import interp_boxes, interp_dtype, loop
+from pypy.module._numpypy import interp_boxes, interp_dtype
from pypy.rlib import jit
from pypy.rlib.rarithmetic import LONG_BIT
from pypy.tool.sourcetools import func_with_new_name
@@ -267,15 +267,16 @@
",".join([str(x) for x in w_obj.shape]),
",".join([str(x) for x in out.shape]),
)
- w_res = Call1(self.func, self.name, out.shape, calc_dtype,
- res_dtype, w_obj, out)
+ xxx
+ #compute(w_res, self.func, )
+ # w_res = Call1(self.func, self.name, out.shape, calc_dtype,
+ # res_dtype, w_obj, out)
#Force it immediately
- w_res.get_concrete()
- else:
- w_res = Call1(self.func, self.name, w_obj.shape, calc_dtype,
- res_dtype, w_obj)
- w_obj.add_invalidates(space, w_res)
- return w_res
+ # w_res.get_concrete()
+ #else:
+ # w_res = Call1(self.func, self.name, w_obj.shape, calc_dtype,
+ # res_dtype, w_obj)
+ #return w_res
class W_Ufunc2(W_Ufunc):
@@ -292,8 +293,6 @@
@jit.unroll_safe
def call(self, space, args_w):
- from pypy.module._numpypy.interp_numarray import (Call2,
- convert_to_array, Scalar, shape_agreement, BaseArray)
if len(args_w) > 2:
[w_lhs, w_rhs, w_out] = args_w
else:
diff --git a/pypy/module/_numpypy/test/test_numarray.py b/pypy/module/_numpypy/test/test_numarray.py
--- a/pypy/module/_numpypy/test/test_numarray.py
+++ b/pypy/module/_numpypy/test/test_numarray.py
@@ -4,7 +4,7 @@
from pypy.conftest import option
from pypy.interpreter.error import OperationError
from pypy.module._numpypy.appbridge import get_appbridge_cache
-from pypy.module._numpypy.interp_iter import Chunk, Chunks
+#from pypy.module._numpypy.interp_iter import Chunk, Chunks
#from pypy.module._numpypy.interp_numarray import W_NDimArray, shape_agreement
from pypy.module._numpypy.test.test_base import BaseNumpyAppTest
More information about the pypy-commit
mailing list