PyObject* foo(PyObject* self, PyObject* args) {
PyObject* polyList;
// Verify that argument is a list
if (!PyArg_ParseTuple(args, "O!", &PyList_Type, &polyList)) {
return NULL;
}
std::vector<std::vector<double>> poly;
// for each elem in list
for (unsigned int i = 0; i < PyList_Size(polyList); ++i) {
PyObject* pypoint = PyList_GetItem(polyList, i);
std::vector<double> point;
// Verify that elem is a list (coord point)
if (!PyArg_ParseTuple(pypoint, "O!", &PyList_Type, &pypoint)) {
PyErr_SetString(PyExc_TypeError, "must pass in list of list");
return NULL;
}
for (unsigned int j = 0; j < PyList_Size(pypoint); ++i) {
PyObject* coord = PyList_GetItem(pypoint, j);
double val;
// Verify that coord is a double
if (!PyArg_ParseTuple(coord, "d", &val)) {
PyErr_SetString(PyExc_TypeError, "must pass in list of list of number");
return NULL;
}
point.push_back(val);
}
poly.push_back(point);
}
//...
}
当我传递一个像[[1.0, 1.0], [2.5, 3.7]]
这样的2d列表时,我会得到一个错误提示,我必须传递一个列表列表。所以我猜错误在if (!PyArg_ParseTuple(pypoint, "O!", &PyList_Type, &pypoint)) {
行的某个地方。但是pypoint
如果不是一个列表,那是什么呢?使用cout << Py_TYPE(pypoint)
我得到了66577D20
,但这并没有告诉我任何事情
我在这里读到另一篇文章,可能是因为Python列表是指针列表,但这仍然没有告诉我如何进行转换
多亏了@YiFei,我研究了PyList_Check和其他Py_Check函数,找到了一个可行的解决方案。代码如下:
当然,可以使用任何其他检查替换以下行,以将非长/双精度值的二维列表转换为二维向量
有关所需类型的特定Py_检查函数,请参见https://docs.python.org/3/c-api/concrete.html
它可能不是最优化的解决方案,但它是有效的
相关问题 更多 >
编程相关推荐