On Thu, Jul 2, 2015 at 11:08 AM, Sturla Molden <
sturla...@gmail.com> wrote:
> If you have used Cython you have written an extension module in Cython. It
> is the only thing Cython can do.
>
> Generally there are (at least) two ways of using C++ callable code from
> Cython and exposing it to Python:
>
> One it to declare your C++ classes as "cdef cppclass" and thus expose their
> interface to Cython. Then you can write your own "cdef class" which exposes
> this functionality to Python. The common design is to call new in __init__
> and call del in __dealloc__. The lifetime of your C++ class is then linked
> to the lifetime of your Cython class when using it from Python.
+1
Note that classes with nullary constructors can be used as members of
cdef classes directly, no need to manually call new/del at all. Also,
__cinit__ is always called exactly once, __init__ may not be.
> This is the
> method I would recommend.
>
> The other way of using C++ from Cython is to wrap all the C++ inside C
> callable functions. This is more tedious, but works well too. Another
> drawback is that you must manually take care of the conversion from C++
> standard exceptions to Python exceptions.
Before Cython had (decent) C++ support, creating a C shim was the only
option. Now I can't imagine any reason one would want to go this route
(unless it uses crazy C++ features, in which case you could even
create a "nicer" C++ API for some parts rather than being stuck with
just C).
> In my C++ rewrite of scipy.spatial.cKDTree to C++ (some of it merged in
> SciPy 0.16, the rest in a PR for 0.17) I ended up doing the latter. If I
> had not done the conversion gradually in multiple PRs, I would probably
> have gone for the first solution.
>
>
> Sturla
>