Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CFFI: TypeError: initializer for ctype 'char[]' must be a bytes or list or tuple, not str

Using the CFFI library for Python, I'm trying to coax a Python string into a char*, so that I can pass it to a C function that accepts char*. I can't seem to figure out what the right incantation is.

Consider this example:

>>> from cffi import FFI
>>> ffi = FFI()
>>> ffi.new("char[]", "bob")

The result is:

TypeError: initializer for ctype 'char[]' must be a bytes or list or tuple, not str

The following does not work either:

>>> ffi.new("char*", "bob")

It says:

TypeError: initializer for ctype 'char' must be a bytes of length 1, not str
like image 981
101010 Avatar asked Aug 14 '17 22:08

101010


1 Answers

o11c solved this already:

Pick explicitly an encoding, e.g. "bob".encode('ascii'). This seems to be just necessary in Python 3.

like image 170
fotinsky Avatar answered Oct 19 '22 11:10

fotinsky