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

Armin Rigo arigo at tunes.org
Tue Sep 23 08:52:14 CEST 2014


Hi Lefteris,

On 22 September 2014 19:37, Eleytherios Stamatogiannakis
<estama at gmail.com> wrote:
> 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*'.

The issue is only that ffi.buffer() tries to guess how long a buffer
you're giving it, and with "char *" the guess is one (only
ffi.string() has logic to look for the final null character in the
array).  You need to get its length explicitly, for example like this:

p = clib.getString(...)        # a "char *"
length = clib.strlen(p)     # the standard strlen() function from C
b = unicode(ffi.buffer(p, length), 'utf-8')


A bientôt,

Armin.


More information about the pypy-dev mailing list