Hi,
On 20 February 2016 at 16:34, Armin Rigo <
ar...@tunes.org> wrote:
> 1. ffi.rawstring(p, size)
> 2. ffi.string(p, total=size)
> 3. ffi.unpack(p, size)
After more thoughts: from a list of ints, for example, ffi.unpack() is
not necessary because PyPy already does the optimal memcpy for this
kind of code (the slicing only creates a <cdata 'int[size]'>, without
making a copy by itself):
p = some <cdata 'int *'>
lst = list(p[0:size])
From a <cdata 'char[size]'> to a byte string, we can use
ffi.buffer(p, size)[:]
which is equivalent to
ffi.buffer(p[0:size])[:]
but we have no equivalent from <cdata 'wchar_t[size]'> to a unicode
string. Calling unicode(p[0:size]) can't really work, because that's
just str() on Python 3. The missing piece seems thus to be either one
of the solutions 1 or 2.
Moreover, solution 2 behaves differently from the "list(p[0:size])":
right now, you can call "ffi.string(p[0:size])", but it will stop at
the first zero (it still uses 'size', but only as the maximum length).
That leaves solution 1 as the minimal missing piece:
ffi.rawstring(p[0:size]) => byte- or unicode string
with or without the equivalent "ffi.rawstring(p, size)". I'm tempted
to say "without", because it makes it clearer that ffi.rawstring() is
the string equivalent of list() in the usages above.
Note that the reverse direction already works, using for example
"p[0:size] = some_unicode" if p is a <cdata 'wchar_t *'>.
So it looks like I'll add ffi.rawstring(), and also add the content of
this mail to the documentation :-)
A bientôt,
Armin.