Commits

mattip committed ef5c6f7

add tests, rpythonify, use proper w_* api interface

Comments (0)

Files changed (2)

pypy/module/cpyext/ndarrayobject.py

 """
 
 from pypy.interpreter.error import OperationError
-from rpython.rtyper.lltypesystem import rffi
+from rpython.rtyper.lltypesystem import rffi, lltype
 from pypy.module.cpyext.api import cpython_api, Py_ssize_t, CANNOT_FAIL
-from pypy.module.cpyext.pyobject import PyObject
+from pypy.module.cpyext.api import PyObject
 from pypy.module.micronumpy.interp_numarray import W_NDimArray, array
-from pypy.module.micronumpy.interp_dtype import get_dtype_cache
+from pypy.module.micronumpy.interp_dtype import get_dtype_cache, W_Dtype
 from pypy.module.micronumpy.arrayimpl.concrete import ConcreteArray
 from pypy.module.micronumpy.arrayimpl.scalar import Scalar
 from rpython.rlib.rawstorage import RAW_STORAGE_PTR
     assert isinstance(w_array, W_NDimArray)
     return rffi.cast(rffi.VOIDP, w_array.implementation.storage)
 
+PyArray_Descr = PyObject
+NULL = lltype.nullptr(rffi.VOIDP.TO)
 
-@cpython_api([PyObject, rffi.VOIDP, Py_ssize_t, Py_ssize_t, Py_ssize_t, rffi.VOIDP],
+@cpython_api([PyObject, PyArray_Descr, 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):
+def _PyArray_FromAny(space, w_obj, w_dtype, min_depth, max_depth, requirements, context):
     """ This is the main function used to obtain an array from any nested
          sequence, or object that exposes the array interface, op. The
          parameters allow specification of the required dtype, the
     if requirements not in (0, NPY_DEFAULT):
         raise OperationError(space.w_NotImplementedError, space.wrap(
             '_PyArray_FromAny called with not-implemented requirements argument'))
-    if not dtype:
-        w_array = array(space, w_obj, copy=False)
-    else:
-        w_array = array(space, w_obj, w_dtype=dtype, copy=False)
+    w_array = array(space, w_obj, w_dtype=w_dtype, copy=False)
     if w_array.is_scalar():
         # since PyArray_DATA() fails on scalars, create a 1D array and set empty
         # shape. So the following combination works for *reading* scalars:
         dtype = get_dtype_cache(space).dtypes_by_num[typenum]
     except KeyError:
         raise OperationError(space.w_ValueError, space.wrap(
-            '_PyArray_FromObject called with invalid dtype %r' % typenum))
+            '_PyArray_FromObject called with invalid dtype %d' % typenum))
     try:
         return _PyArray_FromAny(space, w_obj, dtype, min_depth, max_depth,
-                            0, None);
+                            0, NULL);
     except OperationError, e:
         if e.match(space, space.w_NotImplementedError):
             errstr = space.str_w(e.get_w_value(space))
-            errstr = errstr.replace('FromAny','FromObject')
+            errstr = '_PyArray_FromObject' + errstr[16:]
             raise OperationError(space.w_NotImplementedError, space.wrap(
                 errstr))
         raise

pypy/module/cpyext/test/test_ndarrayobject.py

                 return obj2;
                 '''
                 ),
+                ("test_FromAny", "METH_NOARGS",
+                '''
+                npy_intp dims[2] ={2, 3};
+                PyObject * obj1 = PyArray_SimpleNew(2, dims, 1);
+                PyArray_FILLWBYTE(obj1, 42);
+                PyObject * obj2 = _PyArray_FromAny(obj1, NULL, 0, 0, 0, NULL);
+                Py_DECREF(obj1);
+                return obj2;
+                '''
+                ),
+                 ("test_FromObject", "METH_NOARGS",
+                '''
+                npy_intp dims[2] ={2, 3};
+                PyObject * obj1 = PyArray_SimpleNew(2, dims, 1);
+                PyArray_FILLWBYTE(obj1, 42);
+                PyObject * obj2 = _PyArray_FromObject(obj1, 12, 0, 0);
+                Py_DECREF(obj1);
+                return obj2;
+                '''
+                ),
                 ], prologue='#include <numpy/arrayobject.h>')
         arr = mod.test_simplenew()
         assert arr.shape == (2, 3)
         assert (arr == 42).all()
         arr = mod.test_copy()
         assert (arr == 0).all()
+        #Make sure these work without errors
+        arr = mod.test_FromAny()
+        arr = mod.test_FromObject()