Author: mattip <[email protected]>
Branch:
Changeset: r78648:614e58595142
Date: 2015-07-24 09:43 +0300
http://bitbucket.org/pypy/pypy/changeset/614e58595142/
Log: do not raise exceptions in __init__
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/ndarray.py
b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -1335,7 +1335,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
@@ -1372,6 +1374,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)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit