Returning a byte buffer from C extension

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • forwardshortleg@gmail.com

    Returning a byte buffer from C extension

    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(P yObject* 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.
  • forwardshortleg@gmail.com

    #2
    Re: Returning a byte buffer from C extension

    I just realized that I could do this as follows:

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

    // do something to fill byteBuffer with values.

    return Py_BuildValue(" s#", byteBuffer, numberOfBytesTo Return);

    }

    Sorry for the unnecessary distraction.

    Eknath


    On Mar 5, 4:42 pm, forwardshort... @gmail.com 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(P yObject* 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.

    Comment

    Working...