2

Is it possible to write to-python and from-python converters using Cython like it is possible for boost.python?

Edit (on comment request, with more details on why we'd like this):

We currently have code that is written in C++ that is bound to Python via boost.python. In this C++ code, we deal with Blitz::Array<>'s. In our current abstraction scheme, code in Python uses NumPy's ndarrays, while code in C++ uses Blitz::Arrays<>. We have written a bridge that can convert from numpy.ndarray's to Blitz::Array<>'s and vice-versa, in a quite transparent manner. This bridge can also avoid copying in some circumstances, what is bonus.

Issues with this approach:

  1. Portability: compiling for different versions of Python requires that Boost.Python is compiled for each version of Python;

  2. Documentability: including Python docstrings in C++ code is not very nice. It has discouraged our developers to implement nice looking documentation. It often looks patching and incomplete;

  3. Build speed: compiling boost.python templates can be quite slow. We do this in parallel using CMake to speed-up, but then we would like to create a stock Python egg with our project. As you are probably aware setuptools/distutils and CMake don't mix very well so we have some sort of patchy way to do things in this domain.

So, given the prospect of Cython, I thought we would give it a try and find a less hacky way to solve all these issues. We already have a code-base that relies on numpy.ndarray and I'd like to leverage from that as much as possible, with minimal writing and maximum reuse of our C++ code base. Therefore, my question.

André Anjos
  • 4,641
  • 2
  • 27
  • 34
  • What are you trying to convert to and from Python? – user2357112 Aug 27 '13 at 16:39
  • More explanation added to the question. – André Anjos Aug 27 '13 at 17:24
  • 1
    Sadly, I do not believe the Python C API provides hooks for this. Nevertheless, I have seen some projects use a trampoline function (or layer as in the case of Boost.Python) that performs type lookup and executes registered conversion functions. Alternatively, depending on the interactions, it may be possible to monkey patch parts of the API to perform conversions, using `ctypes` to interact with the C++ codebase through a C wrapper. – Tanner Sansbury Aug 29 '13 at 14:50
  • @AndréAnjos Once you are using only `numpy.arrays`, it should be very straightforward to wrap your C++ library. This question is too broad, but you could specialize it to part of your library, reducing the scope for one specific function maybe... – Saullo G. P. Castro May 27 '14 at 13:07

0 Answers0