Re: [cython-users] Elaborating on the Rectangle example from the docs

14 views
Skip to first unread message

Robert Bradshaw

unread,
May 23, 2013, 5:23:24 PM5/23/13
to cython...@googlegroups.com
On Sun, May 19, 2013 at 12:53 PM, Jan Domański <jan...@gmail.com> wrote:
> Hi,
>
> Say Rectangle.h has an intersectRectangles function that, given two
> rectangles, will return a rectangle with the area shared by the two rects.
>
> public:
> ...
>
> void move(int dx, int dy);
>
>
> Rectangle intersectRectangles(Rectangle* rec1, Rectangle* rec2);
>
> How could one wrap this function in cython? I've tried with the following:
>
> def PyIntersectRectangles(PyRectangle r1, PyRectangle r2):
> Rectangle rec = intersectRectangles(deref(r1.thisptr), deref(r2.thisptr))
> pyrec = PyRectangle()
> del pyrec.thisptr
> pyrec.thisptr = &rec
> return pyrec
>
> The code above gives me a core dump, and I think I understand why, but I see
> no clear solution.

rec is returned to the stack. You need to do

pyrec = PyRectangle()
pyrec[0] = intersectRectangles(deref(r1.thisptr),
deref(r2.thisptr)) # are you sure you want to deref here, given that
your new function takes pointers?\
return pyrec

Note that thisptr is now not needed for classes with nullary
constructors unless polymorphism is desired, just let it have a
Rectangle member directly.

- Robert

Chris Barker - NOAA Federal

unread,
May 23, 2013, 6:15:56 PM5/23/13
to cython...@googlegroups.com
On Sun, May 19, 2013 at 12:53 PM, Jan Domański <jan...@gmail.com> wrote:
> Say Rectangle.h has an intersectRectangles function that, given two
> rectangles, will return a rectangle with the area shared by the two rects.

I hope your question was answered, and I"m not helping, but when you
get it figured out, could you please add it to the example in the
Wiki?

Great to have examples of this sort of thing published.

-Thanks
-Chris



> public:
> ...
>
> void move(int dx, int dy);
>
>
> Rectangle intersectRectangles(Rectangle* rec1, Rectangle* rec2);
>
> How could one wrap this function in cython? I've tried with the following:
>
> def PyIntersectRectangles(PyRectangle r1, PyRectangle r2):
> Rectangle rec = intersectRectangles(deref(r1.thisptr), deref(r2.thisptr))
> pyrec = PyRectangle()
> del pyrec.thisptr
> pyrec.thisptr = &rec
> return pyrec
>
> The code above gives me a core dump, and I think I understand why, but I see
> no clear solution.
>
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cython-users...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris....@noaa.gov
Reply all
Reply to author
Forward
0 new messages