Author: Stefan H. Muller <shmuell...@gmail.com> Branch: pypy-pyarray Changeset: r66342:09db6e0e5c1d Date: 2013-08-06 00:12 +0200 http://bitbucket.org/pypy/pypy/changeset/09db6e0e5c1d/
Log: Add some missing numpypy features - ndarrayobject.py: PyArray_Check(), PyArray_CheckExact() - interp_numarray.py: Stub implementations for __array__(), __array_prepare__(), __array_wrap__(). diff --git a/pypy/module/cpyext/include/numpy/arrayobject.h b/pypy/module/cpyext/include/numpy/arrayobject.h --- a/pypy/module/cpyext/include/numpy/arrayobject.h +++ b/pypy/module/cpyext/include/numpy/arrayobject.h @@ -85,16 +85,17 @@ #ifndef PyArray_NDIM #define PyArray_ISCONTIGUOUS(arr) (1) -#define PyArray_Check(arr) (1) -#define PyArray_NDIM _PyArray_NDIM -#define PyArray_DIM _PyArray_DIM -#define PyArray_STRIDE _PyArray_STRIDE -#define PyArray_SIZE _PyArray_SIZE -#define PyArray_ITEMSIZE _PyArray_ITEMSIZE -#define PyArray_NBYTES _PyArray_NBYTES -#define PyArray_TYPE _PyArray_TYPE -#define PyArray_DATA _PyArray_DATA +#define PyArray_Check _PyArray_Check +#define PyArray_CheckExact _PyArray_CheckExact +#define PyArray_NDIM _PyArray_NDIM +#define PyArray_DIM _PyArray_DIM +#define PyArray_STRIDE _PyArray_STRIDE +#define PyArray_SIZE _PyArray_SIZE +#define PyArray_ITEMSIZE _PyArray_ITEMSIZE +#define PyArray_NBYTES _PyArray_NBYTES +#define PyArray_TYPE _PyArray_TYPE +#define PyArray_DATA _PyArray_DATA #define PyArray_Size PyArray_SIZE #define PyArray_BYTES(arr) ((char *)PyArray_DATA(arr)) 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 @@ -12,6 +12,20 @@ # the asserts are needed, otherwise the translation fails +@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL) +def _PyArray_Check(space, w_obj): + w_obj_type = space.type(w_obj) + w_type = space.gettypeobject(W_NDimArray.typedef) + return (space.is_w(w_obj_type, w_type) or + space.is_true(space.issubtype(w_obj_type, w_type))) + +@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL) +def _PyArray_CheckExact(space, w_obj): + w_obj_type = space.type(w_obj) + w_type = space.gettypeobject(W_NDimArray.typedef) + return space.is_w(w_obj_type, w_type) + + @cpython_api([PyObject], Py_ssize_t, error=CANNOT_FAIL) def _PyArray_NDIM(space, w_array): assert isinstance(w_array, W_NDimArray) 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 @@ -24,6 +24,14 @@ class TestNDArrayObject(BaseApiTest): + def test_Check(self, space, api): + a = array(space, [10, 5, 3]) + x = space.wrap(10.) + assert api._PyArray_Check(a) + assert api._PyArray_CheckExact(a) + assert not api._PyArray_Check(x) + assert not api._PyArray_CheckExact(x) + def test_NDIM(self, space, api): a = array(space, [10, 5, 3]) assert api._PyArray_NDIM(a) == 3 diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py --- a/pypy/module/micronumpy/interp_numarray.py +++ b/pypy/module/micronumpy/interp_numarray.py @@ -418,6 +418,26 @@ raise OperationError(space.w_NotImplementedError, space.wrap( "non-int arg not supported")) + def descr___array__(self, space): + # stub implementation of __array__() + return self + + def descr___array_prepare__(self, space, w_array): + # stub implementation of __array_prepare__() + if isinstance(w_array, W_NDimArray): + return w_array + else: + raise OperationError(space.w_TypeError, + space.wrap("can only be called with ndarray object")) + + def descr___array_wrap__(self, space, w_array): + # stub implementation of __array_wrap__() + if isinstance(w_array, W_NDimArray): + return w_array + else: + raise OperationError(space.w_TypeError, + space.wrap("can only be called with ndarray object")) + def descr_array_iface(self, space): addr = self.implementation.get_storage_as_int(space) # will explode if it can't @@ -1084,6 +1104,10 @@ __reduce__ = interp2app(W_NDimArray.descr_reduce), __setstate__ = interp2app(W_NDimArray.descr_setstate), __array_finalize__ = interp2app(W_NDimArray.descr___array_finalize__), + + __array__ = interp2app(W_NDimArray.descr___array__), + __array_prepare__ = interp2app(W_NDimArray.descr___array_prepare__), + __array_wrap__ = interp2app(W_NDimArray.descr___array_wrap__), ) @unwrap_spec(ndmin=int, copy=bool, subok=bool) _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit