PythonC扩展通过引用传递参数

2024-04-30 23:37:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图为一个函数(libFunc)编写一个pythoncwrapper,它的原型是

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

如何使用PyArg_ParseTuple设置函数调用的参数。这是我目前所拥有的

^{pr2}$

我可以使用byref对ctypes执行相同的操作。在


Tags: 函数参数ctypes原型int函数调用charpr2
1条回答
网友
1楼 · 发布于 2024-04-30 23:37:27

最后我就这样做了

#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;
    }

相关问题 更多 >