Core: Fix memory leaks with PySequence_GetItem

This commit is contained in:
wmayer
2024-05-08 23:40:27 +02:00
parent 5b98af4591
commit 58988a7482
6 changed files with 63 additions and 66 deletions

View File

@@ -410,28 +410,28 @@ PyObject* ViewProviderPy::partialRender(PyObject* args)
return nullptr;
std::vector<std::string> values;
if(value != Py_None) {
PyObject *item = nullptr;
Py_ssize_t nSize;
if (PyList_Check(value) || PyTuple_Check(value))
nSize = PySequence_Size(value);
else {
item = value;
value = nullptr;
nSize = 1;
if (value != Py_None) {
std::vector<Py::Object> pylist;
if (PyList_Check(value) || PyTuple_Check(value)) {
Py::Sequence seq(value);
for (const auto& it : seq) {
pylist.emplace_back(it);
}
}
values.resize(nSize);
for (Py_ssize_t i = 0; i < nSize; ++i) {
if(value)
item = PySequence_GetItem(value, i);
if (PyUnicode_Check(item)) {
values[i] = PyUnicode_AsUTF8(item);
else {
pylist.emplace_back(value);
}
values.reserve(pylist.size());
for (const auto& it : pylist) {
if (it.isString()) {
values.push_back(Py::String(it));
}
else {
std::string error = std::string("type must be str");
error += " not, ";
error += item->ob_type->tp_name;
throw Base::TypeError(error);
error += it.ptr()->ob_type->tp_name;
throw Py::TypeError(error);
}
}
}