For point of comparison, my branch now has a Cython and C++ version of the same thing.

Here's the Cython version:

Here's the C++ version:

Some interesting things to note:

The Cython version isn't that much shorter than the C++ version.  It mostly consists of declarations.  These declarations aren't exact matches to what one would find in the header file(s) because Cython doesn't support exact-width data types etc.  The Cython documentation says "not to worry", but I do wonder how well this will work across different architectures etc.  I'm not sure why some of the Python/C API calls I needed were not defined in Cython's include wrappers.

The Cython extension only builds with "-fpermissive" because I can't seem to get the casts and const coercions to work.  Maybe there's a simple solution...

The exception handling in the png_core.cpp file will need to be updated because Cython only supports handling built-in C++ exceptions (and subclasses), and the Cython custom exception handler doesn't provide a way to get at the exception object that was thrown.

It seems that a lot of things pass through the Cython compiler, but then fail in the C compiler -- you then have to wade through the generated C code to figure out what's going wrong.  This reminds me of the bad old days of C++ when the error messages generated would be dozens of lines long and rather inscrutable.

Once things compiled, due to my own mistake, calling the function segfaulted.  Debugging that segfault in gdb required, again, wading through the generated code.  Using gdb on hand-written code is *much* nicer.

So, it's probably clear that I'm not much of a fan of this approach, but I am trying to find something that the whole community around matplotlib finds easier and more accessible so that the C/C++ experts among use are not exclusively burdened to maintain this part of the code base.  I would be interested to see what others think now that we have an apples-to-apples comparison.


On 12/01/2012 01:36 PM, Michael Droettboom wrote:
For the PNG extension specifically, it was creating callbacks that can 
be called from C and the setjmp magic that libpng requires.  I think 
it's possible to do it, but I was surprised at how non-obvious those 
pieces of Cython were.  I was really hoping by creating this experiment 
that a Cython expert would step up and show the way ;)

The Agg backend has more C++-specific challenges, particularly 
instantiating very complex template expressions -- but I haven't really 
followed that on through.


On 11/30/2012 09:13 AM, Jason Grout wrote:
On 11/29/12 10:59 AM, Michael Droettboom wrote:
I've not had
much luck with Cython for this kind of thing in the past, but I know it
is popular.
I'm curious about what problems you've run into and how long it was.  In
the past, Cython hasn't supported C++ very well, but the situation has
greatly improved recently.  See for some



Keep yourself connected to Go Parallel:
TUNE You got it built. Now make it sing. Tune shows you how.
Matplotlib-devel mailing list

Keep yourself connected to Go Parallel: 
INSIGHTS What's next for parallel hardware, programming and related areas?
Interviews and blogs by thought leaders keep you ahead of the curve.
Matplotlib-devel mailing list