[Cython] [cython-users] Re: How to use Cython to wrap a C++ template class for use in Python?

mark florisson markflorisson88 at gmail.com
Wed May 30 21:07:27 CEST 2012

On 30 May 2012 19:49, Robert Bradshaw <robertwb at gmail.com> wrote:
> On Tue, May 29, 2012 at 5:42 PM, Paul Leopardi
> <paul.leopardi at iinet.net.au> wrote:
>> On Wednesday, 30 May 2012 02:40:47 UTC+10, Chris Barker wrote:
>>> Well, the third option is to use your own home-made templatesto
>>> auto-generate cython code for each type. That's actually not as
>>> painful as it sounds -- there are lot's of templating systems for
>>> python -- Cheetah, for instance is designed for "any" use, not just
>>> html (though I'm never used it for Cython).
>>> http://www.cheetahtemplate.org/
>>> You might want to look at the "bottleneck" project -- they did
>>> something like this -- not for calling a C++ templates, but the
>>> principle is the same.
>>> http://berkeleyanalytics.com/bottleneck/
>> Chris, thanks for your suggestions. I will take a look at Cheetah, but I'm
>> afraid it would add extra complication to an already very complicated build
>> process for PyClical. What I would like is a way for Cython to make
>> instantiations of my C++ template classes visible to Python. Cython supports
>> C++ templates, and Cython now has fused types, but as far as I can tell,
>> these two template concepts within Cython do not work with each other in any
>> meaningful, documented way, let alone support what I am proposing. Maybe I
>> am just too late to propose another Google Summer of Code project, or
>> perhaps the whole idea is all too hard? Surely there must be a wider
>> use-case for this idea than just me and my one library? Should I repost to
>> the core developers list?
> I think there are several issues with why Cython does not (yet?) have
> these capabilities, primarily:
> (1) Metaprogramming done Right can be very nice, but done wrong is disastrous,
> (2) The AST of Cython is really not that nice to work with, and
> (3) If it's just about specifying types, template preprocessing and,
> eventually, using JITs may be sufficient, more flexible, and certainly
> better than a half-baked solution.
> The thread you linked to also has some good discussion.
> We actually had a lot of discussion about this issue at the Cython
> Days workshop last year, and never hit upon a metaprogramming
> framework that seemed Right. Consensus was that until a clean proposal
> was put forward, we would focus on making it easy to use your
> templating engine of choice and implement fused types which would
> cover 85% or more of the need for metaproramming (especially tight
> loops over numeric types, as opposed to more generic cases where
> Python objects and vtables are often good enough).

Definitely, fused types are far from ideal, even if cdef class fused
attributes would be supported.

> The fact that fused types don't work with C++ specializations is
> certainly a bug, though this still limits us to a fixed number of
> instantiations (and separate Python classes) in Python space due to
> the nature of C++ templates.
> - Robert

If there really is a bug, then please report the use case (anyone who
can find one). C++ and fused types *do* work together (unless there
really is a bug), but the thing that is not supported (for anything)
is fused types for cdef class attributes. One, entirely terrible but
working, way to get around from Python space is the metaclass hack I
posted earlier.

More information about the cython-devel mailing list