Python3将整数列表传递给C并返回字符串数组

时间:2018-12-04 14:09:18

标签: python arrays python-extensions c-api

我试图将3d整数列表从Python3传递给C,然后使用Python C API将2d字符串数组从C返回给Python。我有支持功能的工作,以便procStrings是可调用的。但是,我很难弄清楚如何将数组传入/传出以及如何从数组中获取项目。

  1. 如何访问输入3d数组中的整数?
  2. 如何创建输出数组? (如果我分配了它,在哪里释放它?)
  3. 返回输出2d字符串数组的机制是什么?

================================================ ==========

static PyObject* my_module_procStrings(PyObject* self, PyObject* args)
{
    int rows, cols;
    int ii, jj;
    PyObject* listObj;

    // Get the 2d list and parameters from Python call
    if( !PyArg_ParseTuples(args, "O!ii", &PyList_Type, &listObj, &rows, &cols ))
    {
        return NULL;
    }

    // Convert the input to a 3d array in C here?

    // Process the input array / create the output array
    for( ii = 0; ii < rows; ii++ )
    {
        for( jj = 0; jj < cols; jj++ )
        {
            // Code for setting new array entry based on entry from input array goes here

        }
    }

    // How is the new 2d string array returned ?

}

0 个答案:

没有答案