[pypy-commit] pypy default: fix tests, error messages to be strictly numpy 1.9.0
mattip
noreply at buildbot.pypy.org
Sat May 30 23:35:01 CEST 2015
Author: mattip <matti.picus at gmail.com>
Branch:
Changeset: r77717:acba2565aeee
Date: 2015-05-29 16:21 +0300
http://bitbucket.org/pypy/pypy/changeset/acba2565aeee/
Log: fix tests, error messages to be strictly numpy 1.9.0
diff --git a/pypy/module/micronumpy/boxes.py b/pypy/module/micronumpy/boxes.py
--- a/pypy/module/micronumpy/boxes.py
+++ b/pypy/module/micronumpy/boxes.py
@@ -579,7 +579,9 @@
try:
ofs, dtype = self.dtype.fields[item]
except KeyError:
- raise oefmt(space.w_ValueError, "field named %s not found", item)
+ raise oefmt(space.w_IndexError, "222only integers, slices (`:`), "
+ "ellipsis (`...`), numpy.newaxis (`None`) and integer or "
+ "boolean arrays are valid indices")
dtype.itemtype.store(self.arr, self.ofs, ofs,
dtype.coerce(space, w_value))
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
@@ -207,7 +207,7 @@
raise ArrayArgumentException
return self._lookup_by_index(space, view_w)
if shape_len == 0:
- raise oefmt(space.w_IndexError, "0-d arrays can't be indexed")
+ raise oefmt(space.w_IndexError, "too many indices for array")
elif shape_len > 1:
raise IndexError
idx = support.index_w(space, w_idx)
@@ -218,7 +218,11 @@
if space.isinstance_w(w_idx, space.w_str):
idx = space.str_w(w_idx)
dtype = self.dtype
- if not dtype.is_record() or idx not in dtype.fields:
+ if not dtype.is_record():
+ raise oefmt(space.w_IndexError, "only integers, slices (`:`), "
+ "ellipsis (`...`), numpy.newaxis (`None`) and integer or "
+ "boolean arrays are valid indices")
+ elif idx not in dtype.fields:
raise oefmt(space.w_ValueError, "field named %s not found", idx)
return RecordChunk(idx)
elif (space.isinstance_w(w_idx, space.w_int) or
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
@@ -100,10 +100,10 @@
def getitem_filter(self, space, arr):
if arr.ndims() > 1 and arr.get_shape() != self.get_shape():
- raise OperationError(space.w_ValueError, space.wrap(
+ raise OperationError(space.w_IndexError, space.wrap(
"boolean index array should have 1 dimension"))
if arr.get_size() > self.get_size():
- raise OperationError(space.w_ValueError, space.wrap(
+ raise OperationError(space.w_IndexError, space.wrap(
"index out of range for array"))
size = loop.count_all_true(arr)
if arr.ndims() == 1:
@@ -116,10 +116,10 @@
def setitem_filter(self, space, idx, val):
if idx.ndims() > 1 and idx.get_shape() != self.get_shape():
- raise OperationError(space.w_ValueError, space.wrap(
+ raise OperationError(space.w_IndexError, space.wrap(
"boolean index array should have 1 dimension"))
if idx.get_size() > self.get_size():
- raise OperationError(space.w_ValueError, space.wrap(
+ raise OperationError(space.w_IndexError, space.wrap(
"index out of range for array"))
size = loop.count_all_true(idx)
if size > val.get_size() and val.get_size() != 1:
@@ -205,9 +205,13 @@
def descr_getitem(self, space, w_idx):
if space.is_w(w_idx, space.w_Ellipsis):
return self
- elif isinstance(w_idx, W_NDimArray) and w_idx.get_dtype().is_bool() \
- and w_idx.ndims() > 0:
- w_ret = self.getitem_filter(space, w_idx)
+ elif isinstance(w_idx, W_NDimArray) and w_idx.get_dtype().is_bool():
+ if w_idx.ndims() > 0:
+ w_ret = self.getitem_filter(space, w_idx)
+ else:
+ raise oefmt(space.w_IndexError,
+ "in the future, 0-d boolean arrays will be "
+ "interpreted as a valid boolean index")
else:
try:
w_ret = self.implementation.descr_getitem(space, self, w_idx)
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
@@ -19,7 +19,9 @@
try:
return space.int_w(space.int(w_obj))
except OperationError:
- raise oefmt(space.w_IndexError, "cannot convert index to integer")
+ raise oefmt(space.w_IndexError, "only integers, slices (`:`), "
+ "ellipsis (`...`), numpy.newaxis (`None`) and integer or "
+ "boolean arrays are valid indices")
@jit.unroll_safe
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
@@ -413,7 +413,7 @@
b = np.empty_like(a, dtype='i4')
assert b.shape == a.shape
assert b.dtype == np.dtype('i4')
- assert b[0,0] != 1
+ # assert b[0,0] != 1 # no guarantees on values in b
b = np.empty_like([1,2,3])
assert b.shape == (3,)
assert b.dtype == np.int_
@@ -667,9 +667,9 @@
from numpy import arange
a = arange(10)
exc = raises(IndexError, "a[ErrorIndex()] == 0")
- assert exc.value.message == 'cannot convert index to integer'
+ assert exc.value.message.startswith('only integers, slices')
exc = raises(IndexError, "a[ErrorInt()] == 0")
- assert exc.value.message == 'cannot convert index to integer'
+ assert exc.value.message.startswith('only integers, slices')
def test_setslice_array(self):
from numpy import array
@@ -785,9 +785,9 @@
from numpy import array, dtype, int_
a = array(3)
exc = raises(IndexError, "a[0]")
- assert exc.value[0] == "0-d arrays can't be indexed"
+ assert exc.value[0] == "too many indices for array"
exc = raises(IndexError, "a[0] = 5")
- assert exc.value[0] == "0-d arrays can't be indexed"
+ assert exc.value[0] == "too many indices for array"
assert a.size == 1
assert a.shape == ()
assert a.dtype is dtype(int)
@@ -2156,16 +2156,14 @@
[7, 8, 9]])
assert (a[np.array(0)] == a[0]).all()
assert (a[np.array(1)] == a[1]).all()
- assert (a[np.array(True)] == a[1]).all()
- assert (a[np.array(False)] == a[0]).all()
+ exc = raises(IndexError, "a[np.array(True)]")
+ assert exc.value.message == 'in the future, 0-d boolean arrays will be interpreted as a valid boolean index'
exc = raises(IndexError, "a[np.array(1.1)]")
assert exc.value.message == 'arrays used as indices must be of ' \
'integer (or boolean) type'
a[np.array(1)] = a[2]
assert a[1][1] == 8
- a[np.array(True)] = a[0]
- assert a[1][1] == 2
exc = raises(IndexError, "a[np.array(1.1)] = a[2]")
assert exc.value.message == 'arrays used as indices must be of ' \
'integer (or boolean) type'
@@ -2174,8 +2172,8 @@
from numpy import arange, array
b = arange(10)
assert (b[array([True, False, True])] == [0, 2]).all()
- raises(ValueError, "array([1, 2])[array([True, True, True])]")
- raises(ValueError, "b[array([[True, False], [True, False]])]")
+ raises(IndexError, "array([1, 2])[array([True, True, True])]")
+ raises(IndexError, "b[array([[True, False], [True, False]])]")
a = array([[1,2,3],[4,5,6],[7,8,9]],int)
c = array([True,False,True],bool)
b = a[c]
@@ -2186,7 +2184,7 @@
b = arange(5)
b[array([True, False, True])] = [20, 21, 0, 0, 0, 0, 0]
assert (b == [20, 1, 21, 3, 4]).all()
- raises(ValueError, "array([1, 2])[array([True, False, True])] = [1, 2, 3]")
+ raises(IndexError, "array([1, 2])[array([True, False, True])] = [1, 2, 3]")
def test_weakref(self):
import _weakref
@@ -2433,12 +2431,19 @@
def test_ellipsis_indexing(self):
import numpy as np
+ import sys
a = np.array(1.5)
- assert a[...] is a
+ if '__pypy__' in sys.builtin_module_names:
+ assert a[...] is a
+ else:
+ assert a[...].base is a
a[...] = 2.5
assert a == 2.5
a = np.array([1, 2, 3])
- assert a[...] is a
+ if '__pypy__' in sys.builtin_module_names:
+ assert a[...] is a
+ else:
+ assert a[...].base is a
a[...] = 4
assert (a == [4, 4, 4]).all()
@@ -3604,7 +3609,7 @@
arr['x'] = 2
assert arr['x'] == 2
exc = raises(IndexError, "arr[3L]")
- assert exc.value.message == "0-d arrays can't be indexed"
+ assert exc.value.message == "too many indices for array"
exc = raises(ValueError, "arr['xx'] = 2")
assert exc.value.message == "field named xx not found"
assert arr['y'].dtype == a
@@ -3807,8 +3812,8 @@
a[0, 0] = 500
assert (a[0, 0, 0] == 500).all()
assert a[0, 0, 0].shape == (10,)
- exc = raises(ValueError, "a[0, 0]['z']")
- assert exc.value.message == 'field named z not found'
+ exc = raises(IndexError, "a[0, 0]['z']")
+ assert exc.value.message.startswith('only integers, slices')
import sys
a = array(1.5, dtype=float)
@@ -3976,7 +3981,10 @@
def test_create_from_memory(self):
import numpy as np
- dat = np.array(__builtins__.buffer('1.0'), dtype=np.float64)
+ import sys
+ builtins = getattr(__builtins__, '__dict__', __builtins__)
+ _buffer = builtins.get('buffer')
+ dat = np.array(_buffer('1.0'), dtype=np.float64)
assert (dat == [49.0, 46.0, 48.0]).all()
diff --git a/pypy/module/micronumpy/test/test_scalar.py b/pypy/module/micronumpy/test/test_scalar.py
--- a/pypy/module/micronumpy/test/test_scalar.py
+++ b/pypy/module/micronumpy/test/test_scalar.py
@@ -141,9 +141,9 @@
assert f.round() == 13.
assert f.round(decimals=-1) == 10.
assert f.round(decimals=1) == 13.4
+ assert b.round(decimals=5) is b
assert f.round(decimals=1, out=None) == 13.4
assert b.round() == 1.0
- raises(TypeError, b.round, decimals=5)
def test_astype(self):
import numpy as np
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -437,9 +437,8 @@
@specialize.argtype(1)
def round(self, v, decimals=0):
if decimals != 0:
- # numpy incompatible message
- raise oefmt(self.space.w_TypeError,
- "Cannot use float math on bool dtype")
+ # numpy 1.9.0 compatible
+ return v
return Float64(self.space).box(self.unbox(v))
class Integer(Primitive):
More information about the pypy-commit
mailing list