[pypy-commit] pypy pypy-pyarray: add tests, rpythonify, use proper w_* api interface

mattip noreply at buildbot.pypy.org
Sun Sep 22 17:19:10 CEST 2013


Author: Matti Picus <matti.picus at gmail.com>
Branch: pypy-pyarray
Changeset: r67055:ef5c6f72668f
Date: 2013-09-22 18:18 +0300
http://bitbucket.org/pypy/pypy/changeset/ef5c6f72668f/

Log:	add tests, rpythonify, use proper w_* api interface

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
@@ -4,11 +4,11 @@
 """
 
 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
@@ -113,10 +113,12 @@
     assert isinstance(w_array, W_NDimArray)
     return rffi.cast(rffi.VOIDP, w_array.implementation.storage)
 
+PyArray_Descr = PyObject
+NULL = lltype.nullptr(rffi.VOIDP.TO)
 
- at cpython_api([PyObject, rffi.VOIDP, Py_ssize_t, Py_ssize_t, Py_ssize_t, rffi.VOIDP],
+ at 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
@@ -153,10 +155,7 @@
     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:
@@ -175,14 +174,14 @@
         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
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
@@ -244,6 +244,26 @@
                 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)
@@ -254,3 +274,6 @@
         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()


More information about the pypy-commit mailing list