I'm struggling with the Python c-api again... I try to extract strings, either directly passed or as partof a sequence. If the sting is always directly passed one could use PyArgs_ParseTuple, which works fine. This way I get the correct string. But I struggle to extract it from a sequence. Here is my code, and the string I retrieve is always "1" Does anyone have a idea how to extract string from a sequence?
Code: Select all
auto handleItem = [&](PyObject* item) {
if (PyObject_TypeCheck(item, &(Part::TopoShapePy::Type))) {
auto shape = static_cast<Part::TopoShapePy*>(item)->getTopoShapePtr();
//handle shape
}
else if (PyObject_TypeCheck(item, &PyBaseString_Type)) {
const char* _hash = PyString_AsString(item);
std::string hash(_hash);
//handle string
}
else {
PyErr_SetString(PyExc_TypeError, "wrong types");
return 0;
}
};
if(PySequence_Check(_base)) {
Py::Sequence list(_base);
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
PyObject* item = (*it).ptr();
handleItem(item);
}
}
else
handleItem(_base);