[pypy-dev] How to convert python string to C char*?

Armin Rigo arigo at tunes.org
Thu May 14 10:13:25 CEST 2015


Hi Yicong,

(CC to the cffi mailing list)

On 14 May 2015 at 05:02, Yicong Huang <hengha.mao at gmail.com> wrote:
> We had a python function that return a string value. The function will
> callback in C code.
> The below is an example of the code:
>
> @ffi.callback("char *(char *, char *)")
> def strconcat(x, y):
>     x1 = ffi.string(x)
>     y1 = ffi.string(y)
>     print x1 + y1
>     return x1 + y1
>
> The error messages are:
> Trying to convert the result back to C:
> TypeError: initializer for ctype 'char *' must be a cdata pointer, not str
>
> How could we convert the return python string value to C char*?

This is not obvious, because you have problems writing it in C too:
returning a "char *" is only possible when you return some constant
string literal (only in C, can't do that with cffi), or when you
allocate the resulting string and keep it alive for as long as the
caller needs it (and you don't really know how long that is).

If you have a callback to a C library that you didn't write, it must
be written in its documentation.  If it is not, and you really have no
choice, then you have to guess.  The following would work in all cases
but leak the strings by never releasing them:

ALL_RESULTS = []
@ffi.callback("char *(char *, char *)")
def strconcat(x, y):
    x1 = ffi.string(x)
    y1 = ffi.string(y)
    print x1 + y1
    p = ffi.new("char[]", x1 + y1)
    ALL_RESULTS.append(p)
    return p

There are chances that it works as expected if you only keep alive the
last returned result; try it out, but no guarantee.


A bientôt,

Armin.


More information about the pypy-dev mailing list