Author: Ronan Lamy <ronan.l...@gmail.com> Branch: ufunc-reduce Changeset: r78662:dbe051dac229 Date: 2015-07-25 18:14 +0100 http://bitbucket.org/pypy/pypy/changeset/dbe051dac229/
Log: hg merge default diff --git a/pypy/module/micronumpy/base.py b/pypy/module/micronumpy/base.py --- a/pypy/module/micronumpy/base.py +++ b/pypy/module/micronumpy/base.py @@ -2,6 +2,7 @@ from pypy.interpreter.error import OperationError, oefmt from rpython.tool.pairtype import extendabletype from pypy.module.micronumpy import support +from pypy.module.micronumpy import constants as NPY def wrap_impl(space, w_cls, w_instance, impl): if w_cls is None or space.is_w(w_cls, space.gettypefor(W_NDimArray)): @@ -39,6 +40,13 @@ def from_shape(space, shape, dtype, order='C', w_instance=None, zero=True): from pypy.module.micronumpy import concrete, descriptor, boxes from pypy.module.micronumpy.strides import calc_strides + if len(shape) > NPY.MAXDIMS: + raise oefmt(space.w_ValueError, + "sequence too large; must be smaller than %d", NPY.MAXDIMS) + try: + support.product(shape) * dtype.elsize + except OverflowError as e: + raise oefmt(space.w_ValueError, "array is too big") strides, backstrides = calc_strides(shape, dtype.base, order) impl = concrete.ConcreteArray(shape, dtype.base, order, strides, backstrides, zero=zero) @@ -56,13 +64,19 @@ from pypy.module.micronumpy.strides import (calc_strides, calc_backstrides) isize = dtype.elsize + if len(shape) > NPY.MAXDIMS: + raise oefmt(space.w_ValueError, + "sequence too large; must be smaller than %d", NPY.MAXDIMS) + try: + totalsize = support.product(shape) * isize + except OverflowError as e: + raise oefmt(space.w_ValueError, "array is too big") if storage_bytes > 0 : - totalsize = support.product(shape) * isize if totalsize > storage_bytes: raise OperationError(space.w_TypeError, space.wrap( "buffer is too small for requested array")) else: - storage_bytes = support.product(shape) * isize + storage_bytes = totalsize if strides is None: strides, backstrides = calc_strides(shape, dtype, order) else: diff --git a/pypy/module/micronumpy/concrete.py b/pypy/module/micronumpy/concrete.py --- a/pypy/module/micronumpy/concrete.py +++ b/pypy/module/micronumpy/concrete.py @@ -400,17 +400,11 @@ class ConcreteArrayNotOwning(BaseConcreteArray): def __init__(self, shape, dtype, order, strides, backstrides, storage, start=0): - if len(shape) > NPY.MAXDIMS: - raise oefmt(dtype.itemtype.space.w_ValueError, - "sequence too large; must be smaller than %d", NPY.MAXDIMS) make_sure_not_resized(shape) make_sure_not_resized(strides) make_sure_not_resized(backstrides) self.shape = shape - try: - self.size = support.product(shape) * dtype.elsize - except OverflowError as e: - raise oefmt(dtype.itemtype.space.w_ValueError, "array is too big") + self.size = support.product(shape) * dtype.elsize self.order = order self.dtype = dtype self.strides = strides @@ -425,6 +419,13 @@ box, 0, self.size, 0, self.gcstruct) def set_shape(self, space, orig_array, new_shape): + if len(new_shape) > NPY.MAXDIMS: + raise oefmt(space.w_ValueError, + "sequence too large; must be smaller than %d", NPY.MAXDIMS) + try: + support.product(new_shape) * self.dtype.elsize + except OverflowError as e: + raise oefmt(space.w_ValueError, "array is too big") strides, backstrides = calc_strides(new_shape, self.dtype, self.order) return SliceArray(self.start, strides, backstrides, new_shape, self, @@ -451,14 +452,9 @@ storage=lltype.nullptr(RAW_STORAGE), zero=True): gcstruct = V_OBJECTSTORE flags = NPY.ARRAY_ALIGNED | NPY.ARRAY_WRITEABLE - if len(shape) > NPY.MAXDIMS: - raise oefmt(dtype.itemtype.space.w_ValueError, - "sequence too large; must be smaller than %d", NPY.MAXDIMS) + length = support.product(shape) + self.size = length * dtype.elsize if storage == lltype.nullptr(RAW_STORAGE): - try: - length = support.product(shape) - except OverflowError as e: - raise oefmt(dtype.itemtype.space.w_ValueError, "array is too big") if dtype.num == NPY.OBJECT: storage = dtype.itemtype.malloc(length * dtype.elsize, zero=True) gcstruct = _create_objectstore(storage, length, dtype.elsize) @@ -559,6 +555,13 @@ loop.fill(self, box.convert_to(space, self.dtype)) def set_shape(self, space, orig_array, new_shape): + if len(new_shape) > NPY.MAXDIMS: + raise oefmt(space.w_ValueError, + "sequence too large; must be smaller than %d", NPY.MAXDIMS) + try: + support.product(new_shape) * self.dtype.elsize + except OverflowError as e: + raise oefmt(space.w_ValueError, "array is too big") if len(self.get_shape()) < 2 or self.size == 0: # TODO: this code could be refactored into calc_strides # but then calc_strides would have to accept a stepping factor diff --git a/pypy/module/micronumpy/converters.py b/pypy/module/micronumpy/converters.py --- a/pypy/module/micronumpy/converters.py +++ b/pypy/module/micronumpy/converters.py @@ -113,3 +113,12 @@ shape.append(space.int_w(w_item)) shape += dtype.shape return shape[:] + +def out_converter(space, w_out): + from .ndarray import W_NDimArray + if space.is_none(w_out): + return None + elif not isinstance(w_out, W_NDimArray): + raise oefmt(space.w_TypeError, 'output must be an array') + else: + return w_out diff --git a/pypy/module/micronumpy/ndarray.py b/pypy/module/micronumpy/ndarray.py --- a/pypy/module/micronumpy/ndarray.py +++ b/pypy/module/micronumpy/ndarray.py @@ -15,8 +15,9 @@ from pypy.module.micronumpy.base import W_NDimArray, convert_to_array, \ ArrayArgumentException, wrap_impl from pypy.module.micronumpy.concrete import BaseConcreteArray -from pypy.module.micronumpy.converters import multi_axis_converter, \ - order_converter, shape_converter, searchside_converter +from pypy.module.micronumpy.converters import ( + multi_axis_converter, order_converter, shape_converter, + searchside_converter, out_converter) from pypy.module.micronumpy.flagsobj import W_FlagsObject from pypy.module.micronumpy.strides import ( get_shape_from_iterable, shape_agreement, shape_agreement_multiple, @@ -1090,12 +1091,7 @@ def descr_dot(self, space, w_other, w_out=None): from .casting import find_result_type - if space.is_none(w_out): - out = None - elif not isinstance(w_out, W_NDimArray): - raise oefmt(space.w_TypeError, 'output must be an array') - else: - out = w_out + out = out_converter(space, w_out) other = convert_to_array(space, w_other) if other.is_scalar(): #Note: w_out is not modified, this is numpy compliant. @@ -1152,12 +1148,7 @@ def _reduce_ufunc_impl(ufunc_name, name, variant=ufuncs.REDUCE, bool_result=False): @unwrap_spec(keepdims=bool) def impl(self, space, w_axis=None, w_dtype=None, w_out=None, keepdims=False): - if space.is_none(w_out): - out = None - elif not isinstance(w_out, W_NDimArray): - raise oefmt(space.w_TypeError, 'output must be an array') - else: - out = w_out + out = out_converter(space, w_out) if bool_result: w_dtype = descriptor.get_dtype_cache(space).w_booldtype return getattr(ufuncs.get(space), ufunc_name).reduce( @@ -1336,7 +1327,9 @@ dtype = space.interp_w(descriptor.W_Dtype, space.call_function( space.gettypefor(descriptor.W_Dtype), w_dtype)) shape = shape_converter(space, w_shape, dtype) - + if len(shape) > NPY.MAXDIMS: + raise oefmt(space.w_ValueError, + "sequence too large; must be smaller than %d", NPY.MAXDIMS) if not space.is_none(w_buffer): if (not space.is_none(w_strides)): strides = [space.int_w(w_i) for w_i in @@ -1373,6 +1366,10 @@ if space.is_w(w_subtype, space.gettypefor(W_NDimArray)): return W_NDimArray.from_shape(space, shape, dtype, order) strides, backstrides = calc_strides(shape, dtype.base, order) + try: + totalsize = support.product(shape) * dtype.base.elsize + except OverflowError as e: + raise oefmt(space.w_ValueError, "array is too big") impl = ConcreteArray(shape, dtype.base, order, strides, backstrides) w_ret = space.allocate_instance(W_NDimArray, w_subtype) W_NDimArray.__init__(w_ret, impl) diff --git a/pypy/module/micronumpy/ufuncs.py b/pypy/module/micronumpy/ufuncs.py --- a/pypy/module/micronumpy/ufuncs.py +++ b/pypy/module/micronumpy/ufuncs.py @@ -20,6 +20,7 @@ from pypy.module.micronumpy.strides import shape_agreement from pypy.module.micronumpy.support import (_parse_signature, product, get_storage_as_int, is_rhs_priority_higher) +from .converters import out_converter from .casting import ( can_cast_type, can_cast_array, can_cast_to, find_result_type, promote_types) @@ -127,10 +128,7 @@ args_w, kwds_w = __args__.unpack() # sig, extobj are used in generic ufuncs w_subok, w_out, sig, w_casting, extobj = self.parse_kwargs(space, kwds_w) - if space.is_w(w_out, space.w_None): - out = None - else: - out = w_out + out = out_converter(space, w_out) if (w_subok is not None and space.is_true(w_subok)): raise oefmt(space.w_NotImplementedError, "parameter subok unsupported") if kwds_w: @@ -152,9 +150,6 @@ out = args_w[-1] else: args_w = args_w + [out] - if out is not None and not isinstance(out, W_NDimArray): - raise OperationError(space.w_TypeError, space.wrap( - 'output must be an array')) if w_casting is None: casting = 'unsafe' else: @@ -166,13 +161,7 @@ def descr_accumulate(self, space, w_obj, w_axis=None, w_dtype=None, w_out=None): if space.is_none(w_axis): w_axis = space.wrap(0) - if space.is_none(w_out): - out = None - elif not isinstance(w_out, W_NDimArray): - raise OperationError(space.w_TypeError, space.wrap( - 'output must be an array')) - else: - out = w_out + out = out_converter(space, w_out) return self.reduce(space, w_obj, w_axis, True, #keepdims must be true out, w_dtype, variant=ACCUMULATE) @@ -234,13 +223,7 @@ from pypy.module.micronumpy.ndarray import W_NDimArray if w_axis is None: w_axis = space.wrap(0) - if space.is_none(w_out): - out = None - elif not isinstance(w_out, W_NDimArray): - raise OperationError(space.w_TypeError, space.wrap( - 'output must be an array')) - else: - out = w_out + out = out_converter(space, w_out) return self.reduce(space, w_obj, w_axis, keepdims, out, w_dtype) @specialize.arg(7) @@ -497,11 +480,7 @@ w_obj = args_w[0] out = None if len(args_w) > 1: - out = args_w[1] - if space.is_w(out, space.w_None): - out = None - elif out is not None and not isinstance(out, W_NDimArray): - raise oefmt(space.w_TypeError, 'output must be an array') + out = out_converter(space, args_w[1]) w_obj = numpify(space, w_obj) dtype = w_obj.get_dtype(space) calc_dtype, dt_out, func = self.find_specialization(space, dtype, out, casting) @@ -599,10 +578,7 @@ def call(self, space, args_w, sig, casting, extobj): if len(args_w) > 2: [w_lhs, w_rhs, out] = args_w - if space.is_none(out): - out = None - elif not isinstance(out, W_NDimArray): - raise oefmt(space.w_TypeError, 'output must be an array') + out = out_converter(space, out) else: [w_lhs, w_rhs] = args_w out = None @@ -750,7 +726,8 @@ if use_min_scalar: w_arg1 = convert_to_array(space, w_arg1) w_arg2 = convert_to_array(space, w_arg2) - elif in_casting == 'safe' and l_dtype.num == 7 and r_dtype.num == 7: + elif (in_casting == 'safe' and l_dtype.num == 7 and r_dtype.num == 7 and + out is None and not self.promote_to_float): # while long (7) can be cast to int32 (5) on 32 bit, don't do it return l_dtype, l_dtype for dt_in, dt_out in self.dtypes: diff --git a/pypy/module/operator/test/test_operator.py b/pypy/module/operator/test/test_operator.py --- a/pypy/module/operator/test/test_operator.py +++ b/pypy/module/operator/test/test_operator.py @@ -10,13 +10,16 @@ class A(object): getx = operator.attrgetter('x') get3 = operator.itemgetter(3) + callx = operator.methodcaller("append", "x") a = A() a.x = 5 assert a.getx(a) == 5 assert a.get3("foobar") == "b" assert a.getx(*(a,)) == 5 assert a.get3(obj="foobar") == "b" - + l = [] + a.callx(l) + assert l == ["x"] def test_getter_multiple_gest(self): import operator diff --git a/pypy/module/pypyjit/test_pypy_c/test_micronumpy.py b/pypy/module/pypyjit/test_pypy_c/test_micronumpy.py --- a/pypy/module/pypyjit/test_pypy_c/test_micronumpy.py +++ b/pypy/module/pypyjit/test_pypy_c/test_micronumpy.py @@ -29,7 +29,7 @@ guard_true(i15, descr=...) guard_not_invalidated(descr=...) i17 = cast_float_to_int(f16) - i19 = int_and(i17, 255) + i19 = int_is_true(i17) guard_true(i19, descr=...) i20 = getfield_gc_pure(p2, descr=<FieldU pypy.module.micronumpy.boxes.W_BoolBox.inst_value \d+>) i21 = int_is_true(i20) diff --git a/pypy/objspace/test/test_descroperation.py b/pypy/objspace/test/test_descroperation.py --- a/pypy/objspace/test/test_descroperation.py +++ b/pypy/objspace/test/test_descroperation.py @@ -639,10 +639,10 @@ pass a = A() b = B() - assert isinstance(a, A) + assert isinstance(a, A) # "shortcut" does not go through metaclass assert not isinstance(a, B) assert isinstance(b, A) - assert not isinstance(b, B) + assert isinstance(b, B) # "shortcut" does not go through metaclass assert isinstance(4, A) assert not issubclass(A, A) assert not issubclass(B, A) diff --git a/rpython/rlib/buffer.py b/rpython/rlib/buffer.py --- a/rpython/rlib/buffer.py +++ b/rpython/rlib/buffer.py @@ -74,6 +74,8 @@ return "" if step == 1: assert 0 <= start <= stop + if start == 0 and stop == len(self.value): + return self.value return self.value[start:stop] return Buffer.getslice(self, start, stop, step, size) _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit