C type buffer copy

Discussion in 'Python' started by joe@gmail.com, Mar 22, 2005.

  1. Guest

    Hello,

    How does Python deal with C type memory buffers. Does Python return
    everything as an object irrespective of the data type?

    Here's what i am trying to achieve?

    testCode(unsigned char buf, unsigned long len)
    {
    unsigned long data=0x0;
    while (len--)
    {
    *buf++ = (unsigned char)data++
    }
    ....
    }

    What's the best way to deal with this in python?

    -Joe
    , Mar 22, 2005
    #1
    1. Advertising

  2. Hi All--

    def testCode(data):
    buf=data[:]
    # and I hope you're going to do something with buf,
    # because otherwise this function's a bit of a waste;-)

    "" wrote:
    >
    > Hello,
    >
    > How does Python deal with C type memory buffers. Does Python return
    > everything as an object irrespective of the data type?
    >
    > Here's what i am trying to achieve?
    >
    > testCode(unsigned char buf, unsigned long len)
    > {
    > unsigned long data=0x0;
    > while (len--)
    > {
    > *buf++ = (unsigned char)data++
    > }
    > ....
    > }
    >
    > What's the best way to deal with this in python?
    >


    Metta,
    Ivan
    ----------------------------------------------
    Ivan Van Laningham
    God N Locomotive Works
    http://www.andi-holmes.com/
    http://www.foretec.com/python/workshops/1998-11/proceedings.html
    Army Signal Corps: Cu Chi, Class of '70
    Author: Teach Yourself Python in 24 Hours
    Ivan Van Laningham, Mar 22, 2005
    #2
    1. Advertising

  3. Neil Hodgson Guest

    Joe:

    > testCode(unsigned char buf, unsigned long len)
    > {
    > unsigned long data=0x0;
    > while (len--)
    > {
    > *buf++ = (unsigned char)data++


    This C code will crash since buf is declared as an unsigned char, not
    an unsigned char*. Stop thinking in terms of translating low level C
    because Python is a higher level language and does not have machine
    address pointers. You should be implementing the intent of your design
    which we can't determine from tiny incorrect snippets. For example, we
    can't determine if the intent is to create a new string, translation:
    ''.join([chr(x) for x in range(len)]), or if you are modifying an
    existing sequence or inserting into a sequence. It is also impossible to
    tell if you should be using a list, array or string for this job.

    Neil
    Neil Hodgson, Mar 22, 2005
    #3
  4. Paul Rubin Guest

    "" <> writes:
    > What's the best way to deal with this in python?


    I can't tell what you're trying to do. If you want an array
    containing the numbers 0,1,...,n-1, just say
    buf = range(n)
    Paul Rubin, Mar 22, 2005
    #4
    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. Raja
    Replies:
    12
    Views:
    24,341
    John Harrison
    Jun 21, 2004
  2. Alex
    Replies:
    2
    Views:
    1,199
  3. Replies:
    26
    Views:
    2,083
    Roland Pibinger
    Sep 1, 2006
  4. kj
    Replies:
    5
    Views:
    396
  5. Replies:
    1
    Views:
    300
Loading...

Share This Page