From c331d608aa64ea6028902daf88ee2b721cf130f7 Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 19 Feb 2021 17:26:33 +0100 Subject: [PATCH] App: [skip ci] replace plain C API with PyCXX to avoid memory leaks --- src/App/PropertyContainerPyImp.cpp | 80 ++++++++++++++---------------- 1 file changed, 36 insertions(+), 44 deletions(-) diff --git a/src/App/PropertyContainerPyImp.cpp b/src/App/PropertyContainerPyImp.cpp index a493ac811d..f1775bb127 100644 --- a/src/App/PropertyContainerPyImp.cpp +++ b/src/App/PropertyContainerPyImp.cpp @@ -357,34 +357,34 @@ PyObject* PropertyContainerPy::getDocumentationOfProperty(PyObject *args) else return Py::new_reference_to(Py::String("")); } - -PyObject* PropertyContainerPy::getEnumerationsOfProperty(PyObject *args) -{ - char *pstr; - if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C - return NULL; // NULL triggers exception - - Property* prop = getPropertyContainerPtr()->getPropertyByName(pstr); - if (!prop) { - PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", pstr); - return 0; - } - - PropertyEnumeration *enumProp = dynamic_cast(prop); - if (!enumProp) { - Py_INCREF(Py_None); - return Py_None; - } - - std::vector enumerations = enumProp->getEnumVector(); - - Py::List ret; - for (std::vector::const_iterator it = enumerations.begin(); it != enumerations.end(); ++it) { - ret.append(Py::String(*it)); - } - return Py::new_reference_to(ret); -} - + +PyObject* PropertyContainerPy::getEnumerationsOfProperty(PyObject *args) +{ + char *pstr; + if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C + return NULL; // NULL triggers exception + + Property* prop = getPropertyContainerPtr()->getPropertyByName(pstr); + if (!prop) { + PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", pstr); + return 0; + } + + PropertyEnumeration *enumProp = dynamic_cast(prop); + if (!enumProp) { + Py_INCREF(Py_None); + return Py_None; + } + + std::vector enumerations = enumProp->getEnumVector(); + + Py::List ret; + for (std::vector::const_iterator it = enumerations.begin(); it != enumerations.end(); ++it) { + ret.append(Py::String(*it)); + } + return Py::new_reference_to(ret); +} + Py::List PropertyContainerPy::getPropertiesList(void) const { Py::List ret; @@ -519,23 +519,15 @@ PyObject *PropertyContainerPy::getCustomAttributes(const char* attr) const // get the properties to the C++ PropertyContainer class std::map Map; getPropertyContainerPtr()->getPropertyMap(Map); - PyObject *dict = PyDict_New(); - if (dict) { - for ( std::map::iterator it = Map.begin(); it != Map.end(); ++it ) -#if PY_MAJOR_VERSION >= 3 - PyDict_SetItem(dict, PyUnicode_FromString(it->first.c_str()), PyUnicode_FromString("")); -#else - PyDict_SetItem(dict, PyString_FromString(it->first.c_str()), PyString_FromString("")); -#endif - if (PyErr_Occurred()) { - Py_DECREF(dict); - dict = NULL; - } + + Py::Dict dict; + for (std::map::iterator it = Map.begin(); it != Map.end(); ++it) { + dict.setItem(it->first, Py::String("")); } - return dict; - } else if(Base::streq(attr,"Shape") - && getPropertyContainerPtr()->isDerivedFrom(App::DocumentObject::getClassTypeId())) - { + return Py::new_reference_to(dict); + } + ///FIXME: For v0.20: Do not use stuff from Part module here! + else if(Base::streq(attr,"Shape") && getPropertyContainerPtr()->isDerivedFrom(App::DocumentObject::getClassTypeId())) { // Special treatment of Shape property static PyObject *_getShape = 0; if(!_getShape) {