[pypy-commit] pypy ufuncapi: test_ufunc leaks references but passes test, how to create an array of function pointers?
mattip
noreply at buildbot.pypy.org
Sat Aug 16 22:49:13 CEST 2014
Author: mattip <matti.picus at gmail.com>
Branch: ufuncapi
Changeset: r72829:422aa6836286
Date: 2014-08-16 23:41 +0300
http://bitbucket.org/pypy/pypy/changeset/422aa6836286/
Log: test_ufunc leaks references but passes test, how to create an array
of function pointers?
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
@@ -277,11 +277,10 @@
arg_i = args_w[i]
assert isinstance(arg_i, W_NDimArray)
raw_storage_setitem(dataps, CCHARP_SIZE * i, rffi.cast(rffi.CCHARP, arg_i.implementation.storage))
- #This assumes we iterate over the last dimension?
- raw_storage_setitem(dims, LONG_SIZE * i, rffi.cast(rffi.LONG, arg_i.get_shape()[0]))
- raw_storage_setitem(steps, LONG_SIZE * i, rffi.cast(rffi.LONG, arg_i.implementation.strides[0]))
+ #This assumes we iterate over the whole array (it should be a view...)
+ raw_storage_setitem(dims, LONG_SIZE * i, rffi.cast(rffi.LONG, arg_i.get_size()))
+ raw_storage_setitem(steps, LONG_SIZE * i, rffi.cast(rffi.LONG, arg_i.get_dtype().elsize))
try:
- import pdb;pdb.set_trace()
self.func(rffi.cast(rffi.CArrayPtr(rffi.CCHARP), dataps),
rffi.cast(npy_intpp, dims), rffi.cast(npy_intpp, steps), user_data)
except:
@@ -299,7 +298,7 @@
GenericUfunc = lltype.FuncType([rffi.CArrayPtr(rffi.CCHARP), npy_intpp, npy_intpp,
rffi.VOIDP], lltype.Void)
gufunctype = lltype.Ptr(GenericUfunc)
- at cpython_api([rffi.CArrayPtr(gufunctype), rffi.VOIDP, rffi.CCHARP, Py_ssize_t, Py_ssize_t,
+ at cpython_api([gufunctype, rffi.VOIDP, rffi.CCHARP, Py_ssize_t, Py_ssize_t,
Py_ssize_t, Py_ssize_t, rffi.CCHARP, rffi.CCHARP, Py_ssize_t,
rffi.CCHARP], PyObject)
def _PyUFunc_FromFuncAndDataAndSignature(space, funcs, data, types, ntypes,
@@ -307,7 +306,7 @@
funcs_w = [None] * ntypes
dtypes_w = [None] * ntypes * (nin + nout)
for i in range(ntypes):
- funcs_w[i] = W_GenericUFuncCaller(funcs[i])
+ funcs_w[i] = W_GenericUFuncCaller(funcs)
for i in range(ntypes*(nin+nout)):
dtypes_w[i] = get_dtype_cache(space).dtypes_by_num[ord(types[i])]
w_funcs = space.newlist(funcs_w)
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
@@ -314,14 +314,16 @@
raises(TypeError, "mod.check_array(42)")
def test_ufunc(self):
- from _numpypy.multiarray import ndarray
+ from _numpypy.multiarray import arange
mod = self.import_extension('foo', [
("create_ufunc", "METH_NOARGS",
"""
PyUFuncGenericFunction funcs[] = {&double_times2, &int_times2};
char types[] = { NPY_DOUBLE,NPY_DOUBLE, NPY_INT, NPY_INT };
void *array_data[] = {NULL, NULL};
- PyObject * retval = _PyUFunc_FromFuncAndDataAndSignature(funcs,
+ PyObject * retval;
+ /* XXX should be 'funcs', not 'funcs[1]' but how to define an array of function pointers? */
+ retval = _PyUFunc_FromFuncAndDataAndSignature(funcs[1],
array_data, types, 2, 1, 1, PyUFunc_None,
"times2", "times2_docstring", 0, "()->()");
Py_INCREF(retval);
@@ -361,7 +363,6 @@
char *in = args[0], *out=args[1];
npy_intp in_step = steps[0], out_step = steps[1];
int tmp;
-
for (i = 0; i < n; i++) {
/*BEGIN main ufunc computation*/
tmp = *(int *)in;
@@ -374,6 +375,6 @@
};
}; ''')
times2 = mod.create_ufunc()
- arr = ndarray((3, 4), dtype='i')
+ arr = arange(12, dtype='i').reshape(3, 4)
out = times2(arr)
- assert (out == [6, 8]).all()
+ assert (out == arr * 2).all()
More information about the pypy-commit
mailing list