[Cython] Automatic conversion with fixed-size C arrays

Kurt Smith kwmsmith at gmail.com
Fri Jul 25 17:24:48 CEST 2014


On Thu, Jul 24, 2014 at 11:16 PM, Robert Bradshaw <robertwb at gmail.com>
wrote:

> On Thu, Jul 24, 2014 at 7:13 PM, Kurt Smith <kwmsmith at gmail.com> wrote:
> >
> > On Thu, Jul 24, 2014 at 2:07 PM, Robert Bradshaw <robertwb at gmail.com>
> wrote:
> >>
> >> On Fri, Jul 18, 2014 at 12:44 PM, Robert Bradshaw <robertwb at gmail.com>
> >> wrote:
> >> > On Fri, Jul 18, 2014 at 12:08 PM, Kurt Smith <kwmsmith at gmail.com>
> wrote:
> >> >> On Wed, Jul 16, 2014 at 1:02 PM, Robert Bradshaw <robertwb at gmail.com
> >
> >> >> wrote:
> >> >>>
> >> >>>
> >> >>> Yes, this'd be nice to have. One difficulty with arrays is that they
> >> >>> can't be returned by value, and so the ordinary from_py_function
> >> >>> mechanism (which gets called recursively) would need to be adapted.
> >> >>> (Allowing to_py_function to be optinally be called by reference
> >> >>> instead of by value could be nice as well from a performance
> >> >>> standpoint.)
> >> >>
> >> >>
> >> >> OK, thanks for the pointers.  I'll put some time on this over the
> >> >> weekend.
> >> >> Should I just make a PR when things are ready to review, or should I
> >> >> put up
> >> >> an issue first?
> >> >
> >> > I think this thread is sufficient; looking forward to a pull request.
> >>
> >> Don't know if you had time to look at this yet,
> >
> >
> > Yes, I've put in some time.  Initial focus is getting
> >
> >     cdef int a[10] = obj
> >
> > working, and then using that for structs.
>
> Sounds good.
>
> > Took a little while to re-orient myself with the codebase.
> >
> > I'm working on getting the `to_py_function` and `from_py_function`
> > infrastructure to take arguments by reference; right now I'm getting
> > something hacked into place, and I'd appreciate your review to point out
> the
> > right way (or at least a better way) to do it.
>
> from_py_function always takes its argument (a PyObject*) by reference.
> It's used as an rvalue, so might not make sense for arrays.
>

My bad, I worded my response poorly -- what I mean is that currently the
generated code is something like:

    __pyx_v_t1 = xxx_from_py_function_xxx(temp_py_obj);

    ___pyx_v_a = __pyx_v_t1;

where __pyx_v_a and __pyx_v_t1 are declared as fixed-size C arrays.

This won't work, for reasons pointed out.

So I'm trying to generate instead:

    err_code = xxx_from_py_function_xxx(temp_py_obj, &__pyx_v_a[0], 10);
 if (err_code == -1) { ... }

Where the 10 is the array length.  The function would initialize the array
internally.  The python object is passed by reference as before, and the
array is also passed by reference.  The array is assigned to just once,
minimizing copying.  We can return a status code to propagate errors, etc.
 This seems like the cleanest code to me.  Thoughts?

That's what I mean about "taking arguments by reference".


> > Will likely have a PR for you by this weekend.
>
> Cool.
>
> >> but another possible
> >> hack would be to use the fact that struct { int x[N]; } is bit
> >> compatible with int[N] and can be passed/assigned by value. E.g.
> >
> >
> > Yes, this is a good point, although if I can get the pass-by-reference
> > working for fixed-size arrays, ideally we can do the same for structs
> with
> > array members.
>
> Agreed.
>
> - Robert
> _______________________________________________
> cython-devel mailing list
> cython-devel at python.org
> https://mail.python.org/mailman/listinfo/cython-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cython-devel/attachments/20140725/664fd3e4/attachment.html>


More information about the cython-devel mailing list