Returning a byte buffer from C extension

Discussion in 'Python' started by forwardshortleg@gmail.com, Mar 6, 2008.

  1. Guest

    Hello,

    I am new to Python programming. So, kindly excuse me if I don't use
    correct terminology here below.

    I am trying to write an extension function that returns an array of
    bytes as shown in the example below:


    static PyObject* GetByteBuffer(PyObject* self, PyObject* args)
    {
    char byteBuffer[100];

    // do something to fill byteBuffer with values.

    return Py_BuildValue("y", byteBuffer);
    }


    Is this valid? I get a run time error in Python 2.5 (actually 2.6) and
    Python 3.0 returns null terminated byte string.
    My byte buffer may contain one or more zeros and I want is the entire
    buffer regardless of its contents. How do I do it?

    Thanks,

    Eknath

    P.S: I know that 2.6 & 3.0 are not meant for a newcomers like me.
    Unfortunately 2.5.2 and older for windows are built using MSCVC 6.0
    and they pose problems building extensions.
     
    , Mar 6, 2008
    #1
    1. Advertising

  2. Guest

    I just realized that I could do this as follows:

    static PyObject* GetByteBuffer(PyObject* self, PyObject* args)
    {
    char byteBuffer[100];

    // do something to fill byteBuffer with values.

    return Py_BuildValue("s#", byteBuffer, numberOfBytesToReturn);

    }

    Sorry for the unnecessary distraction.

    Eknath


    On Mar 5, 4:42 pm, wrote:
    > Hello,
    >
    > I am new to Python programming. So, kindly excuse me if I don't use
    > correct terminology here below.
    >
    > I am trying to write an extension function that returns an array of
    > bytes as shown in the example below:
    >
    > static PyObject* GetByteBuffer(PyObject* self, PyObject* args)
    > {
    > char byteBuffer[100];
    >
    > // do something to fill byteBuffer with values.
    >
    > return Py_BuildValue("y", byteBuffer);
    >
    > }
    >
    > Is this valid? I get a run time error in Python 2.5 (actually 2.6) and
    > Python 3.0 returns null terminated byte string.
    > My byte buffer may contain one or more zeros and I want is the entire
    > buffer regardless of its contents. How do I do it?
    >
    > Thanks,
    >
    > Eknath
    >
    > P.S: I know that 2.6 & 3.0 are not meant for a newcomers like me.
    > Unfortunately 2.5.2 and older for windows are built using MSCVC 6.0
    > and they pose problems building extensions.
     
    , Mar 6, 2008
    #2
    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,415
    John Harrison
    Jun 21, 2004
  2. Replies:
    2
    Views:
    414
    Karl Heinz Buchegger
    Jan 12, 2005
  3. Replies:
    2
    Views:
    607
    sergejusz
    Mar 26, 2007
  4. Neal Becker

    buffer creates only read-only buffer?

    Neal Becker, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    415
    Neal Becker
    Jan 8, 2009
  5. xingye
    Replies:
    9
    Views:
    276
    Michael Lu
    Apr 19, 2004
Loading...

Share This Page