Hi Sarvi,
On Wed, Nov 14, 2012 at 7:36 PM, Sarvi Shanmugham <
sarv...@gmail.com> wrote:
> But it raised a question of how CFFI deals with it when passing string
> arguments between python/clibrary and back?
>
> Is the null termination stripped/added?
When you pass a Python object as a "TYPE *" argument to a CFFI
function, it can be either a CFFI pointer object whose type is already
"TYPE *" or "TYPE[]", or else a Python object that will be converted
to a "TYPE[]". So in case it is a Python string passed to a "char *"
argument, the conversion is to "char[]". You get the same object as
ffi.new("char[]", "hello") --- which means you get an extra \0.
A return value of type "char *" is never automatically converted to a
Python string. You need to call ffi.string(). This will convert the
string until the first null character, not included.
If you read some field of a structure declared as an array of chars,
and call ffi.string() on it, then CFFI knows the length and uses it as
an upper bound. It still stops at the first \0, if any. In other
words ffi.string() never returns a \0 character.
To convert a char*-and-length pair to a Python string that may contain
\0, you need ffi.buffer(charp, length)[:].
A bientôt,
Armin.