Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > access string data from within cython

Reply
Thread Tools

access string data from within cython

 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      08-05-2009
Hi,

I'm trying to wrap a C-API which has a call that takes a void* and a size_t
as arguments.

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? I tried looking into the
buffer-protocol, but to be honest - that's beyond me, I don't see where
that is actually giving me access to the real data.

Diez
 
Reply With Quote
 
 
 
 
Philip Semanchuk
Guest
Posts: n/a
 
      08-05-2009

On Aug 5, 2009, at 1:16 PM, Diez B. Roggisch wrote:

> Hi,
>
> I'm trying to wrap a C-API which has a call that takes a void* and a
> size_t
> as arguments.
>
> 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? I tried looking into the
> buffer-protocol, but to be honest - that's beyond me, I don't see
> where
> that is actually giving me access to the real data.


Hi Diez,
Would ctypes.create_string_buffer() work for you?


bye
P
 
Reply With Quote
 
 
 
 
sturlamolden
Guest
Posts: n/a
 
      08-05-2009
On 5 Aug, 19:16, "Diez B. Roggisch" <(E-Mail Removed)> 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)

foo(data, length)


 
Reply With Quote
 
sturlamolden
Guest
Posts: n/a
 
      08-05-2009
On 5 Aug, 21:23, sturlamolden <(E-Mail Removed)> wrote:

> foo(data, length)


Oops, that would be

foo(<void *> data, length)


 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      08-05-2009
Philip Semanchuk schrieb:
>
> On Aug 5, 2009, at 1:16 PM, Diez B. Roggisch wrote:
>
>> Hi,
>>
>> I'm trying to wrap a C-API which has a call that takes a void* and a
>> size_t
>> as arguments.
>>
>> 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? I tried looking into the
>> buffer-protocol, but to be honest - that's beyond me, I don't see where
>> that is actually giving me access to the real data.

>
> Hi Diez,
> Would ctypes.create_string_buffer() work for you?


I'm not using ctypes because I want a distributable egg with static
linkage. ctypes can't help there afaik.

Diez
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      08-05-2009
sturlamolden schrieb:
> On 5 Aug, 19:16, "Diez B. Roggisch" <(E-Mail Removed)> 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
 
Reply With Quote
 
Philip Semanchuk
Guest
Posts: n/a
 
      08-05-2009

On Aug 5, 2009, at 5:19 PM, Diez B. Roggisch wrote:

> Philip Semanchuk schrieb:
>> On Aug 5, 2009, at 1:16 PM, Diez B. Roggisch wrote:
>>> Hi,
>>>
>>> I'm trying to wrap a C-API which has a call that takes a void* and
>>> a size_t
>>> as arguments.
>>>
>>> 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? I tried looking into
>>> the
>>> buffer-protocol, but to be honest - that's beyond me, I don't see
>>> where
>>> that is actually giving me access to the real data.

>> Hi Diez,
>> Would ctypes.create_string_buffer() work for you?

>
> I'm not using ctypes because I want a distributable egg with static
> linkage. ctypes can't help there afaik.


Sorry, I read cython as CPython.


 
Reply With Quote
 
sturlamolden
Guest
Posts: n/a
 
      08-05-2009
On 5 Aug, 23:22, "Diez B. Roggisch" <(E-Mail Removed)> wrote:

> 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.


It should, and if it does not it is a bug in Cython. The len()
function should work similarly in Python and Cython: It should just
call the object's __len__ method. Cython has no idea that your Python
object bytestring is a byte string, nor does Cython care.

>>> bs = '\x00'*10 # bytes string with 10 zeros
>>> len(bs)

10
>>> bs.__len__()

10

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cython dynamic library problem Tommy Grav Python 3 09-18-2008 05:17 PM
Wrap Numpy with Cython? martin.nordstrom87@gmail.com Python 1 06-24-2008 08:09 PM
Cython code generation for Py3 complete Stefan Behnel Python 0 05-22-2008 08:10 AM
Types, Cython, program readability bearophileHUGS@lycos.com Python 13 03-29-2008 02:31 AM
HowTo Use Cython on a Windows XP Box? David Lees Python 5 09-01-2007 06:00 AM



Advertisments