[Cython] Automatic C++ conversions

Robert Bradshaw robertwb at gmail.com
Thu Jun 28 10:59:59 CEST 2012


I've been looking how painful it is to constantly convert between
Python objects and string in C++. Yes, it's easy to write a utility,
but this should be as natural (if not more so, as the length is
explicit) than bytes <-> char*. Several other of the libcpp classes
(vector, map) have natural Python analogues too.

What would people think about making it possible to declare these in a
C++ file? Being able to make arbitrary mappings anywhere between types
is contextless global state that I'd rather avoid, but perhaps special
methods defined on the class such as

cdef extern from "<string>" namespace "std":
    cdef cppclass string:
        def __object__(sting s):
            return s.c_str()[s.size()]
        def __create__(object o):
            return string(<char*>o, len(o))
        ...

(names open to suggestions) Then one could write

cdef extern from *:
    string c_func(string)

def f(x):
    return c_func(x)

- Robert


More information about the cython-devel mailing list