[ctypes-users] Dereferencing a slice of pointer (was: Re: Dereferencing a pointer)
Brought to you by:
theller
|
From: Holger D. <H.D...@gm...> - 2004-07-09 11:10:07
|
On a related note:
I am currently trying to wrap a function which allocates a buffer for
me; something like
,----
| int foo(char **out_buffer, size_t *out_buffer_length);
`----
The data in the buffer can be binary, i.e. it may contain a 0-byte,
which is why we get a length returned as well.
Because of the 0-byte, I cannot use something like
,----
| out_buffer = c_char_p()
| out_buffer_length = c_int()
| success = the_lib.foo(byref(out_buffer), byref(out_buffer_length))
`----
What I currently have is:
,----
| out_buffer = POINTER(c_char)()
| out_buffer_length = c_int()
| success = the_lib.foo(pointer(out_buffer), byref(out_buffer_length))
| out_buffer = "".join([out_buffer[i] for i in range(out_buffer_length)])
`----
and this works.
Two questions:
a) Could this be done better?
b) Why doesn't the pointer metaclass support slicing? I.e. this could
have simplified my last line to
,----
| out_buffer = "".join(out_buffer[:out_buffer_length])
`----
I had a quick glance at the code and it is just missing -- is this
intentional or just not yet done?
Thanks,
Holger
|