access string data from within cython

Diez B. Roggisch deets at nospam.web.de
Wed Aug 5 17:22:56 EDT 2009


sturlamolden schrieb:
> On 5 Aug, 19:16, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> 
>> void foo(void *data, size_t length)
>>
>> The wrapper is supposed to be called with a python (byte)string instance,
>> which might contain more than one '\0'-character.
>>
>> So how do I access the raw data of a string?
> 
> cdef extern void foo(void *data, int length)
> cdef char *data
> cdef int length
> bytestring = ... #whatever
> 
> rawdata = <char *> bytestring
> length = <int> len(bytestring)

And that gives me the *full* bytestring, not only until the first zero? 
I can't try that right now (not at work anymore), but if it's that easy, 
that would be great.

Diez



More information about the Python-list mailing list