access string data from within cython

Discussion in 'Python' started by Diez B. Roggisch, Aug 5, 2009.

  1. 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
     
    Diez B. Roggisch, Aug 5, 2009
    #1
    1. Advertising

  2. 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
     
    Philip Semanchuk, Aug 5, 2009
    #2
    1. Advertising

  3. Diez B. Roggisch

    sturlamolden Guest

    On 5 Aug, 19:16, "Diez B. Roggisch" <> 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)
     
    sturlamolden, Aug 5, 2009
    #3
  4. Diez B. Roggisch

    sturlamolden Guest

    On 5 Aug, 21:23, sturlamolden <> wrote:

    > foo(data, length)


    Oops, that would be

    foo(<void *> data, length)
     
    sturlamolden, Aug 5, 2009
    #4
  5. 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
     
    Diez B. Roggisch, Aug 5, 2009
    #5
  6. sturlamolden schrieb:
    > On 5 Aug, 19:16, "Diez B. Roggisch" <> 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
     
    Diez B. Roggisch, Aug 5, 2009
    #6
  7. 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.
     
    Philip Semanchuk, Aug 5, 2009
    #7
  8. Diez B. Roggisch

    sturlamolden Guest

    On 5 Aug, 23:22, "Diez B. Roggisch" <> 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
     
    sturlamolden, Aug 5, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lees

    HowTo Use Cython on a Windows XP Box?

    David Lees, Sep 1, 2007, in forum: Python
    Replies:
    5
    Views:
    1,178
    Paul McGuire
    Sep 1, 2007
  2. Replies:
    13
    Views:
    607
    Robert Hicks
    Mar 29, 2008
  3. Stefan Behnel

    Cython code generation for Py3 complete

    Stefan Behnel, May 22, 2008, in forum: Python
    Replies:
    0
    Views:
    265
    Stefan Behnel
    May 22, 2008
  4. Replies:
    1
    Views:
    372
    Robert Kern
    Jun 24, 2008
  5. Tommy Grav

    Cython dynamic library problem

    Tommy Grav, Sep 18, 2008, in forum: Python
    Replies:
    3
    Views:
    333
    Tommy Grav
    Sep 18, 2008
Loading...

Share This Page