Writing a C extension - borrowed references

Stefan Behnel stefan_ml at behnel.de
Thu Mar 22 02:17:27 EDT 2018


Tom Evans via Python-list schrieb am 20.03.2018 um 18:03:
> On Tue, Mar 20, 2018 at 4:38 PM, Chris Angelico wrote:
>> BTW, have you looked into Cython? It's smart enough to take care of a
>> lot of this sort of thing for you.
> 
> I did a bit; this work is to replace our old python 2 SAML client,
> which used python-lasso and python-libxml2, both packages that are
> built as part of building the C library and thus an utter PITA to
> package for different versions of python (something our Infra team
> were extremely reluctant to do). When the latest (PITA to build)
> version of python-lasso started interfering with python-xmlsec
> (validating an invalid signature was causing segfaults), I got fed up
> of fighting it.

If rewriting is an option (just mentioning it ;) ), something like this
would probably be based on lxml these days. It also comes with a
C/Cython-level API, in case you need to a) do some kind of low-level tree
processing or b) integrate with some external library.


> I actually also maintain a C version of the same code, using the same
> libraries, so porting those few segments of code to Python/C seemed
> more expedient than rewriting in Cython. I'm not writing an API to
> these libraries, just a few functions.

Happy if that works for you, but let me still add a quick note that, from
my experience, rewriting C-API code in Cython tends to be a surprisingly
quick thing to do (it's basically just reverse engineering the Python code
from the C-API code), but it's a one-time investment that can reduce the
long-term maintenance cost a lot, often improves the performance, and
usually also leads to nicer Python APIs, as what you get in the end is more
obvious from the code, so you can focus on the handling more easily.

Stefan




More information about the Python-list mailing list