Dag Sverre Seljebotn wrote:
> jah wrote:
>> On Tue, May 18, 2010 at 11:33 PM, Dag Sverre Seljebotn 
>> <[email protected] <mailto:[email protected]>> 
>> wrote:
>>
>>     You basically have to stoop down to using C pointers:
>>
>>
>>     cpdef evaluate(FTYPE_t *x, FTYPE_t *p, FTYPE_t* out, Py_ssize_t 
>> len):
>>        # Access x[i], p[i]
>>
>>
>>     Then call like this:
>>
>>     func.evaluate(<FTYPE_t*>x.data, <FTYPE_t*>p.data, ...)
>>
>>     Note that this relies on mode='c' bit for correctness!
>>
>>
>>
>> Looks great!  Is it correct that you meant "cdef" rather than 
>> "cpdef"?  My cython complains about not being able to convert 
>> "FTYPE_t *" to a "Python object" otherwise.   So I had to do:
> Ah right. Doing this fully featured can be a little tricky, you 
> basically need to reimplement the "cpdef" functionality:
>
> cdef fast_evaluate(self, double* x, ...):
>    if is_overridden_in_python(self):
>        # Create NumPy arrays from ptrs, see e.g. PyArray_CreateXXX (or 
> was it NewXXX) functions). Then:
>        return self.evaluate(xarr)
>    # default computation
>
> cpdef evaluate(self, np.ndarray[...] x):
>    fast_evaluate(<double*>x.data, ...)
>
> Then, is_overridden_in_python contains this in C:
>
> return Py_TYPE(((PyObject *)__pyx_v_self))->tp_dictoffset != 0;
>
> This is taken from the implementation of cpdef in Cython; it basically 
> checks whether the object has a dict (= overriden in Python space). I 
> can't translate that to Cython off the top of my head, but you may be 
> able to just include it as a macro in a C file if you can't figure out 
> how:
>
> // In foo.h:
> #define is_overridden_in_python(x) (Py_TYPE(((PyObject 
> *)x))->tp_dictoffset != 0)
>
> cdef extern from "foo.h:
>    cdef bint is_overridden_in_python(object)
>
> Perhaps there's a Python C API function to do this as well.
>
BTW, there are features planned which would make all of this much much 
easier and friendlier, but don't expect them to show up soon.

Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to