From d3500ecfabf8b3130fca416689696e7a5f2b327f Mon Sep 17 00:00:00 2001 From: "Zheng, Lei" Date: Fri, 2 Aug 2019 09:15:49 +0800 Subject: [PATCH] App: fix Python object leak in PropertyListT * Fix Python object leak in _setPyObject() * Add support for Python iterables * Minor performance improvement on setPyValues() --- src/App/Property.cpp | 55 ++++++++++++++++++++++++++++---------------- src/App/Property.h | 25 ++++++++++---------- 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/src/App/Property.cpp b/src/App/Property.cpp index 29c76f0a04..58ad4b556c 100644 --- a/src/App/Property.cpp +++ b/src/App/Property.cpp @@ -213,44 +213,59 @@ void Property::setStatus(Status pos, bool on) { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void PropertyListsBase::_setPyObject(PyObject *value) { - std::vector vals; std::vector indices; + std::vector vals; + Py::Object pySeq; + if (PyDict_Check(value)) { - PyObject* keyList = PyDict_Keys(value); - PyObject* itemList = PyDict_Values(value); - Py_ssize_t nSize = PyList_Size(keyList); - vals.reserve(nSize); - indices.reserve(nSize); + Py::Dict dict(value); + auto size = dict.size(); + vals.reserve(size); + indices.reserve(size); int listSize = getSize(); - for (Py_ssize_t i=0; ilistSize) - throw Base::RuntimeError("index out of bound"); + throw Base::ValueError("index out of bound"); if(idx==-1 || idx==listSize) { idx = listSize; ++listSize; } indices.push_back(idx); - vals.push_back(PyList_GetItem(itemList,i)); + vals.push_back(item.second.ptr()); } - }else if (PySequence_Check(value)) { - Py_ssize_t nSize = PySequence_Size(value); - vals.reserve(nSize); - for (Py_ssize_t i=0; i &vals, const std::vector &indices) override { - ListT values; - // old version boost::dynamic_bitset don't have reserve(). What a shame! - // values.reserve(vals.size()); - for(auto item : vals) - values.push_back(getPyValue(item)); - if(indices.empty()) - setValues(values); - else { - atomic_change guard(*this); - assert(values.size()==indices.size()); - for(int i=0,count=values.size();i