[pypy-commit] pypy pypy-pyarray: change asserts to exceptions

mattip noreply at buildbot.pypy.org
Mon Sep 9 00:05:13 CEST 2013


Author: Matti Picus <matti.picus at gmail.com>
Branch: pypy-pyarray
Changeset: r66852:1331d831dad4
Date: 2013-09-04 15:56 +0300
http://bitbucket.org/pypy/pypy/changeset/1331d831dad4/

Log:	change asserts to exceptions

diff --git a/pypy/module/cpyext/ndarrayobject.py b/pypy/module/cpyext/ndarrayobject.py
--- a/pypy/module/cpyext/ndarrayobject.py
+++ b/pypy/module/cpyext/ndarrayobject.py
@@ -1,10 +1,11 @@
 """
+
 Numpy C-API for PyPy - S. H. Muller, 2013/07/26
 """
 
 from pypy.interpreter.error import OperationError
-from rpython.rtyper.lltypesystem import rffi, lltype
-from pypy.module.cpyext.api import cpython_api, Py_ssize_t, CANNOT_FAIL
+from rpython.rtyper.lltypesystem import rffi
+from pypy.module.cpyext.api import cpython_api, Py_ssize_t, CANNOT_FAIL, _NOT_SPECIFIED
 from pypy.module.cpyext.pyobject import PyObject
 from pypy.module.micronumpy.interp_numarray import W_NDimArray, convert_to_array, wrap_impl
 from pypy.module.micronumpy.interp_dtype import get_dtype_cache
@@ -56,9 +57,11 @@
     w_type = space.gettypeobject(W_NDimArray.typedef)
     return space.is_w(w_obj_type, w_type)
 
- at cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
+ at cpython_api([PyObject], rffi.INT_real, error=-1)
 def _PyArray_FLAGS(space, w_array):
-    assert isinstance(w_array, W_NDimArray)
+    if not isinstance(w_array, W_NDimArray):
+        raise OperationError(space.w_ValueError, space.wrap(
+            '_PyArray_FLAGS(ndarray) called with non-ndarray'))
     flags = NPY_BEHAVED_NS
     if isinstance(w_array.implementation, ConcreteArray):
         flags |= NPY_OWNDATA
@@ -70,56 +73,72 @@
         flags |= NPY_F_CONTIGUOUS
     return flags
 
- at cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
+ at cpython_api([PyObject], rffi.INT_real, error=-1)
 def _PyArray_NDIM(space, w_array):
-    assert isinstance(w_array, W_NDimArray)
+    if not isinstance(w_array, W_NDimArray):
+        raise OperationError(space.w_ValueError, space.wrap(
+            '_PyArray_NDIM(ndarray) called with non-ndarray'))
     return len(w_array.get_shape())
 
- at cpython_api([PyObject, Py_ssize_t], Py_ssize_t, error=CANNOT_FAIL)
+ at cpython_api([PyObject, Py_ssize_t], Py_ssize_t, error=-1)
 def _PyArray_DIM(space, w_array, n):
-    assert isinstance(w_array, W_NDimArray)
+    if not isinstance(w_array, W_NDimArray):
+        raise OperationError(space.w_ValueError, space.wrap(
+            '_PyArray_DIM called with non-ndarray'))
     return w_array.get_shape()[n]
 
- at cpython_api([PyObject, Py_ssize_t], Py_ssize_t, error=CANNOT_FAIL)
+ at cpython_api([PyObject, Py_ssize_t], Py_ssize_t, error=-1)
 def _PyArray_STRIDE(space, w_array, n):
-    assert isinstance(w_array, W_NDimArray)
+    if not isinstance(w_array, W_NDimArray):
+        raise OperationError(space.w_ValueError, space.wrap(
+            '_PyArray_STRIDE called with non-ndarray'))
     return w_array.implementation.get_strides()[n]
 
- at cpython_api([PyObject], Py_ssize_t, error=CANNOT_FAIL)
+ at cpython_api([PyObject], Py_ssize_t, error=-1)
 def _PyArray_SIZE(space, w_array):
-    assert isinstance(w_array, W_NDimArray)
+    if not isinstance(w_array, W_NDimArray):
+        raise OperationError(space.w_ValueError, space.wrap(
+            '_PyArray_SIZE called with non-ndarray'))
     return w_array.get_size()
 
- at cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
+ at cpython_api([PyObject], rffi.INT_real, error=-1)
 def _PyArray_ITEMSIZE(space, w_array):
-    assert isinstance(w_array, W_NDimArray)
+    if not isinstance(w_array, W_NDimArray):
+        raise OperationError(space.w_ValueError, space.wrap(
+            '_PyArray_ITEMSIZE called with non-ndarray'))
     return w_array.get_dtype().get_size()
 
- at cpython_api([PyObject], Py_ssize_t, error=CANNOT_FAIL)
+ at cpython_api([PyObject], Py_ssize_t, error=-1)
 def _PyArray_NBYTES(space, w_array):
-    assert isinstance(w_array, W_NDimArray)
+    if not isinstance(w_array, W_NDimArray):
+        raise OperationError(space.w_ValueError, space.wrap(
+            '_PyArray_NBYTES called with non-ndarray'))
     return w_array.get_size() * w_array.get_dtype().get_size()
 
- at cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
+ at cpython_api([PyObject], rffi.INT_real, error=-1)
 def _PyArray_TYPE(space, w_array):
-    assert isinstance(w_array, W_NDimArray)
+    if not isinstance(w_array, W_NDimArray):
+        raise OperationError(space.w_ValueError, space.wrap(
+            '_PyArray_TYPE called with non-ndarray'))
     return w_array.get_dtype().num
 
 
- at cpython_api([PyObject], rffi.VOIDP, error=CANNOT_FAIL)
+ at cpython_api([PyObject], rffi.VOIDP, error=_NOT_SPECIFIED)
 def _PyArray_DATA(space, w_array):
     # fails on scalars - see PyArray_FromAny()
-    assert isinstance(w_array, W_NDimArray)
+    if not isinstance(w_array, W_NDimArray):
+        raise OperationError(space.w_ValueError, space.wrap(
+            '_PyArray_DATA called with non-ndarray'))
     return rffi.cast(rffi.VOIDP, w_array.implementation.storage)
 
 
- at cpython_api([PyObject, rffi.VOIDP, Py_ssize_t, Py_ssize_t, Py_ssize_t, rffi.VOIDP], 
+ at cpython_api([PyObject, rffi.VOIDP, Py_ssize_t, Py_ssize_t, Py_ssize_t, rffi.VOIDP],
              PyObject)
 def _PyArray_FromAny(space, w_obj, dtype, min_depth, max_depth, requirements, context):
     # ignore all additional arguments for now
     w_array = convert_to_array(space, w_obj)
     if w_array.is_scalar():
-        # since PyArray_DATA() fails on scalars, create a 1D array and set empty 
+        # since PyArray_DATA() fails on scalars, create a 1D array and set empty
         # shape. So the following combination works for *reading* scalars:
         #     PyObject *arr = PyArray_FromAny(obj);
         #     int nd = PyArray_NDIM(arr);
@@ -156,15 +175,15 @@
     shape, dtype = get_shape_and_dtype(space, nd, dims, typenum)
     return W_NDimArray.from_shape(space, shape, dtype)
 
-def simple_new_from_data(space, nd, dims, typenum, data, 
+def simple_new_from_data(space, nd, dims, typenum, data,
         order='C', owning=False, w_subtype=None):
     shape, dtype = get_shape_and_dtype(space, nd, dims, typenum)
     storage = rffi.cast(RAW_STORAGE_PTR, data)
     if nd == 0:
         w_val = dtype.itemtype.box_raw_data(storage)
         return W_NDimArray(Scalar(dtype, w_val))
-    else:        
-        return W_NDimArray.from_shape_and_storage(space, shape, storage, dtype, 
+    else:
+        return W_NDimArray.from_shape_and_storage(space, shape, storage, dtype,
                 order=order, owning=owning, w_subtype=w_subtype)
 
 
@@ -188,7 +207,7 @@
     rffi.VOIDP, Py_ssize_t, Py_ssize_t, PyObject], PyObject)
 def _PyArray_New(space, subtype, nd, dims, typenum, strides, data, itemsize, flags, obj):
     if strides:
-        raise OperationError(space.w_NotImplementedError, 
+        raise OperationError(space.w_NotImplementedError,
                              space.wrap("strides must be NULL"))
 
     order = 'C' if flags & NPY_C_CONTIGUOUS else 'F'
@@ -196,11 +215,9 @@
     w_subtype = None
 
     if data:
-        return simple_new_from_data(space, nd, dims, typenum, data, 
+        return simple_new_from_data(space, nd, dims, typenum, data,
             order=order, owning=owning, w_subtype=w_subtype)
     else:
         return simple_new(space, nd, dims, typenum,
             order=order, owning=owning, w_subtype=w_subtype)
 
-
-
diff --git a/pypy/module/cpyext/test/test_ndarrayobject.py b/pypy/module/cpyext/test/test_ndarrayobject.py
--- a/pypy/module/cpyext/test/test_ndarrayobject.py
+++ b/pypy/module/cpyext/test/test_ndarrayobject.py
@@ -42,7 +42,7 @@
         assert api._PyArray_FLAGS(f) & 0x0002
         assert not api._PyArray_FLAGS(c) & 0x0002
         assert not api._PyArray_FLAGS(f) & 0x0001
-        
+
     def test_NDIM(self, space, api):
         a = array(space, [10, 5, 3])
         assert api._PyArray_NDIM(a) == 3
@@ -58,7 +58,7 @@
     def test_ITEMSIZE(self, space, api):
         a = array(space, [10, 5, 3])
         assert api._PyArray_ITEMSIZE(a) == 8
-    
+
     def test_NBYTES(self, space, api):
         a = array(space, [10, 5, 3])
         assert api._PyArray_NBYTES(a) == 1200
@@ -79,7 +79,7 @@
 
         a = api._PyArray_FromAny(a0, NULL, 0, 0, 0, NULL)
         assert api._PyArray_NDIM(a) == 0
-        
+
         ptr = rffi.cast(rffi.DOUBLEP, api._PyArray_DATA(a))
         assert ptr[0] == 10.
 
@@ -112,9 +112,9 @@
     def test_SimpleNew_scalar(self, space, api):
         ptr_s = lltype.nullptr(rffi.LONGP.TO)
         a = api._PyArray_SimpleNew(0, ptr_s, 12)
-        
+
         dtype = get_dtype_cache(space).w_float64dtype
-        
+
         a.set_scalar_value(dtype.itemtype.box(10.))
         assert a.get_scalar_value().value == 10.
 
@@ -127,7 +127,7 @@
         x[0] = float(10.)
 
         ptr_s = lltype.nullptr(rffi.LONGP.TO)
-        
+
         res = api._PyArray_SimpleNewFromData(0, ptr_s, num, ptr_a)
         assert res.is_scalar()
         assert res.get_scalar_value().value == 10.
@@ -141,9 +141,9 @@
         ptr_s[0] = 10
         ptr_s[1] = 5
         ptr_s[2] = 3
-        
+
         a = api._PyArray_SimpleNew(nd, ptr_s, 12)
-        
+
         #assert list(api._PyArray_DIMS(a))[:3] == shape
 
         ptr_a = api._PyArray_DATA(a)
@@ -185,13 +185,13 @@
     def test_SimpleNewFromData_complex(self, space, api):
         a = array(space, [2])
         ptr_a = api._PyArray_DATA(a)
-        
+
         x = rffi.cast(rffi.DOUBLEP, ptr_a)
         x[0] = 3.
         x[1] = 4.
-                
+
         ptr_s = lltype.nullptr(rffi.LONGP.TO)
-        
+
         res = api._PyArray_SimpleNewFromData(0, ptr_s, 15, ptr_a)
         assert res.get_scalar_value().real == 3.
         assert res.get_scalar_value().imag == 4.


More information about the pypy-commit mailing list