PyArg_ParseTuple help

Discussion in 'Python' started by zyqnews@163.net, Jul 10, 2005.

  1. Guest

    hello all,
    how to parse the arguments of a c function with PyArg_ParseTuple?
    The prototype of the c function is :
    int func(unsigned char * in , int inlen, unsigned char * v, unsigned
    char * out, int * outlen);

    The problem is , when the func returns, there will be results in out
    and outlen. Is the following format is correct?

    PyObject* wrap_func(PyObject* self, PyObject* args)
    {
    unsigned char *in;
    int inlen;
    unsigned char *v;
    unsigned char *out;
    int outlen;

    PyArg_ParseTuple(args, "s|i|s|s|i", &in, &inlen, &v, &out, &outlen);

    result = func(in, inlen, v, out, &outlen);// notice out and &outlen

    return Py_BuildValue("i", result);
    }

    Thanks
    , Jul 10, 2005
    #1
    1. Advertising

  2. wrote:
    > hello all,
    > how to parse the arguments of a c function with PyArg_ParseTuple?
    > The prototype of the c function is :
    > int func(unsigned char * in , int inlen, unsigned char * v, unsigned
    > char * out, int * outlen);
    >
    > The problem is , when the func returns, there will be results in out
    > and outlen. Is the following format is correct?


    As the ``out'' and ``outlen'' are used for output, you should not pass
    them to a python function as parameters. Instead, you'd better make the
    return value of the python function to represent them (if the python
    caller wants them, of course). In your case, returning a python string
    object will be a good choice if outlen is the length of out array, since
    a python string knows its length.

    So, you need only parse the parameters of ``in'', ``inlen'' and ``v'' in
    the wrap function. And even further, the python caller can pass a
    python string to replace ``in'' and ``inlen''. Then, the prototype of
    the function is something like:

    def func(ins, v)

    which returns a string.

    [...]

    --
    Qiangning Hong
    Qiangning Hong, Jul 10, 2005
    #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. Michael Gross
    Replies:
    0
    Views:
    616
    Michael Gross
    Jul 30, 2003
  2. =?iso-8859-1?b?IkrpcvRtZQ==?= Laheurte

    Re: Python/C API - PyArg_ParseTuple problem ?

    =?iso-8859-1?b?IkrpcvRtZQ==?= Laheurte, Aug 22, 2003, in forum: Python
    Replies:
    1
    Views:
    392
    =?iso-8859-1?b?IkrpcvRtZQ==?= Laheurte
    Aug 22, 2003
  3. Skip Montanaro
    Replies:
    1
    Views:
    337
    Torsten Mohr
    Jun 7, 2004
  4. Patrick Stinson

    PyArg_ParseTuple

    Patrick Stinson, Jun 9, 2004, in forum: Python
    Replies:
    1
    Views:
    442
    Jeff Epler
    Jun 10, 2004
  5. Patrick Stinson

    lenght of char buffers in PyArg_ParseTuple

    Patrick Stinson, Jun 10, 2004, in forum: Python
    Replies:
    1
    Views:
    399
    Duncan Booth
    Jun 10, 2004
Loading...

Share This Page