PyArg_ParseTuple help

Z

zyqnews

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
 
Q

Qiangning Hong

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.

[...]
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top