Python C Extension for passing arguments by reference

I am trying to write a python C shell for a function (libFunc), the prototype of which is

libFunc(char**, int*, char*, int) 

How to use PyArg_ParseTuple to configure arguments for calling a function. That's what i currently

 #include <Python.h> PyObject* libFunc_py(PyObject* self, PyObject* args) { char* input; char** output; int inputlen; int* outputlen; PyArg_ParseTuple(args, "sisi" , output, outputlen, &input, &inputlen); int ret = libFunc(output, outlen, input, inputlen); return Py_BuildValue("i", ret); } 

I can do the same with ctypes using byref.

+4
source share
1 answer

I ended it up like this.

 #include <Python.h> PyObject* libFunc_py(PyObject* self, PyObject* args) { char* input; char* output; int inputlen; int outputlen; PyArg_ParseTuple(args, "s#" , &input, &inputlen); int ret = libFunc(&output, &outlen, input, inputlen); if (ret !=0) { Py_ErrSetString(PyExc_RunTimeError, "ErrorMessage"); return NULL; } PyObject* retStr = Py_BuildValue("s#", output,outputlen); if(output) free(output) return retStr; } 
0
source

All Articles