Author: Brian Kearns <bdkea...@gmail.com> Branch: Changeset: r73836:d3789bd3b3ed Date: 2014-10-07 18:47 -0400 http://bitbucket.org/pypy/pypy/changeset/d3789bd3b3ed/
Log: fix validation of shape in empty/zeros diff --git a/pypy/module/micronumpy/ctors.py b/pypy/module/micronumpy/ctors.py --- a/pypy/module/micronumpy/ctors.py +++ b/pypy/module/micronumpy/ctors.py @@ -3,7 +3,7 @@ from rpython.rlib.buffer import SubBuffer from rpython.rlib.rstring import strip_spaces from rpython.rtyper.lltypesystem import lltype, rffi -from pypy.module.micronumpy import descriptor, loop +from pypy.module.micronumpy import descriptor, loop, support from pypy.module.micronumpy.base import ( W_NDimArray, convert_to_array, W_NumpyObject) from pypy.module.micronumpy.converters import shape_converter @@ -134,6 +134,15 @@ if dtype.is_str_or_unicode() and dtype.elsize < 1: dtype = descriptor.variable_dtype(space, dtype.char + '1') shape = shape_converter(space, w_shape, dtype) + for dim in shape: + if dim < 0: + raise OperationError(space.w_ValueError, space.wrap( + "negative dimensions are not allowed")) + try: + support.product(shape) + except OverflowError: + raise OperationError(space.w_ValueError, space.wrap( + "array is too big.")) return W_NDimArray.from_shape(space, shape, dtype=dtype, zero=zero) def empty(space, w_shape, w_dtype=None, w_order=None): diff --git a/pypy/module/micronumpy/support.py b/pypy/module/micronumpy/support.py --- a/pypy/module/micronumpy/support.py +++ b/pypy/module/micronumpy/support.py @@ -1,5 +1,6 @@ from pypy.interpreter.error import OperationError, oefmt from rpython.rlib import jit +from rpython.rlib.rarithmetic import ovfcheck def issequence_w(space, w_obj): @@ -23,7 +24,7 @@ def product(s): i = 1 for x in s: - i *= x + i = ovfcheck(i * x) return i diff --git a/pypy/module/micronumpy/test/test_ndarray.py b/pypy/module/micronumpy/test/test_ndarray.py --- a/pypy/module/micronumpy/test/test_ndarray.py +++ b/pypy/module/micronumpy/test/test_ndarray.py @@ -384,6 +384,19 @@ assert zeros((), dtype='S').shape == () assert zeros((), dtype='S').dtype == '|S1' + def test_check_shape(self): + import numpy as np + for func in [np.zeros, np.empty]: + exc = raises(ValueError, func, [0, -1, 1], 'int8') + assert str(exc.value) == "negative dimensions are not allowed" + exc = raises(ValueError, func, [2, -1, 3], 'int8') + assert str(exc.value) == "negative dimensions are not allowed" + + exc = raises(ValueError, func, [975]*7, 'int8') + assert str(exc.value) == "array is too big." + exc = raises(ValueError, func, [26244]*5, 'int8') + assert str(exc.value) == "array is too big." + def test_empty_like(self): import numpy as np a = np.empty_like(np.zeros(())) _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit