[pypy-dev] Declaring a function that returns a string in CFFI

Eleytherios Stamatogiannakis estama at gmail.com
Mon Sep 22 19:37:00 CEST 2014


Hello,

First the problem that i have. Right now, when i get a string back from 
a C function, i have to do 2 copies of it:

ffi.cdef("""
const char *getString(...);
""")

tmp = ffi.string(clib.getString(...)) # 1st copy
pystring = tmp.decode('utf-8')      # 2nd copy

So i thought why not use an ffi.buffer on it and do the decoding 
directly on the buffer:

cstr = ffi.new('char []', 'abcd')
b = unicode(ffi.buffer(cstr), 'utf-8')

Above works.

But the problem is that in C a function that returns an array cannot be 
declared. So i cannot do a:

b = unicode( ffi.buffer( clib.getString(...) ) ,'utf-8')

because it'll only return the first character of getString, due to being 
declared as a 'char*'.

Is there any way in CFFI to declare a function as returning a 'char[]' 
so as a buffer can be directly used on its results?

Thank you.

l.





More information about the pypy-dev mailing list