From 10edc03ec13ae6247d582f8bc358ce10fcb23a33 Mon Sep 17 00:00:00 2001 From: marioalexis Date: Tue, 7 Jun 2022 18:04:22 -0300 Subject: [PATCH] App: Clean up code in PropertyContainerPyImp.cpp --- src/App/PropertyContainerPyImp.cpp | 128 +++++++++++++++++------------ 1 file changed, 75 insertions(+), 53 deletions(-) diff --git a/src/App/PropertyContainerPyImp.cpp b/src/App/PropertyContainerPyImp.cpp index c49ba09df5..591d03544c 100644 --- a/src/App/PropertyContainerPyImp.cpp +++ b/src/App/PropertyContainerPyImp.cpp @@ -54,16 +54,24 @@ PyObject* PropertyContainerPy::getPropertyByName(PyObject *args) int checkOwner=0; if (!PyArg_ParseTuple(args, "s|i", &pstr, &checkOwner)) return nullptr; - App::Property* prop = getPropertyContainerPtr()->getPropertyByName(pstr); - if (prop) { - if(!checkOwner || (checkOwner==1 && prop->getContainer()==getPropertyContainerPtr())) - return prop->getPyObject(); - Py::TupleN res(Py::asObject(prop->getContainer()->getPyObject()), - Py::asObject(prop->getPyObject())); - return Py::new_reference_to(res); + + if (checkOwner < 0 || checkOwner > 2) { + PyErr_SetString(PyExc_ValueError, "'checkOwner' expected in the range [0, 2]"); + return nullptr; } - PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", pstr); - return nullptr; + + App::Property* prop = getPropertyContainerPtr()->getPropertyByName(pstr); + if (!prop) { + PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", pstr); + return nullptr; + } + + if (!checkOwner || (checkOwner==1 && prop->getContainer()==getPropertyContainerPtr())) + return prop->getPyObject(); + + Py::TupleN res(Py::asObject(prop->getContainer()->getPyObject()), Py::asObject(prop->getPyObject())); + + return Py::new_reference_to(res); } PyObject* PropertyContainerPy::getPropertyTouchList(PyObject *args) @@ -71,6 +79,7 @@ PyObject* PropertyContainerPy::getPropertyTouchList(PyObject *args) char *pstr; if (!PyArg_ParseTuple(args, "s", &pstr)) return nullptr; + App::Property* prop = getPropertyContainerPtr()->getPropertyByName(pstr); if (prop && prop->isDerivedFrom(PropertyLists::getClassTypeId())) { const auto &touched = static_cast(prop)->getTouchList(); @@ -80,11 +89,14 @@ PyObject* PropertyContainerPy::getPropertyTouchList(PyObject *args) ret.setItem(i++,Py::Long(idx)); return Py::new_reference_to(ret); } - else if(!prop) + else if (!prop) { PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", pstr); - else + return nullptr; + } + else { PyErr_Format(PyExc_AttributeError, "Property '%s' is not of list type", pstr); - return nullptr; + return nullptr; + } } PyObject* PropertyContainerPy::getTypeOfProperty(PyObject *args) @@ -168,7 +180,7 @@ PyObject* PropertyContainerPy::setEditorMode(PyObject *args) status.reset(Property::ReadOnly); status.reset(Property::Hidden); for (Py::Sequence::iterator it = seq.begin();it!=seq.end();++it) { - std::string str = (std::string)Py::String(*it); + std::string str = static_cast(Py::String(*it)); if (str == "ReadOnly") status.set(Property::ReadOnly); else if (str == "Hidden") @@ -210,28 +222,30 @@ PyObject* PropertyContainerPy::setPropertyStatus(PyObject *args) PyObject *pyValue; if (!PyArg_ParseTuple(args, "sO", &name, &pyValue)) return nullptr; + App::Property* prop = getPropertyContainerPtr()->getPropertyByName(name); if (!prop) { PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", name); return nullptr; } - auto linkProp = Base::freecad_dynamic_cast(prop); + auto linkProp = Base::freecad_dynamic_cast(prop); std::bitset<32> status(prop->getStatus()); size_t count = 1; bool isSeq = false; - if(PyList_Check(pyValue) || PyTuple_Check(pyValue)) { + if (PyList_Check(pyValue) || PyTuple_Check(pyValue)) { isSeq = true; count = PySequence_Size(pyValue); } - for(size_t i=0;i1 && v[0] == '-') { @@ -248,7 +262,8 @@ PyObject* PropertyContainerPy::setPropertyStatus(PyObject *args) return nullptr; } status.set(it->second,value); - }else if(item.isNumeric()) { + } + else if (item.isNumeric()) { int v = Py::Int(item); if(v<0) { value = false; @@ -257,7 +272,8 @@ PyObject* PropertyContainerPy::setPropertyStatus(PyObject *args) if(v==0 || v>31) PyErr_Format(PyExc_ValueError, "Status value out of range '%d'", v); status.set(v,value); - } else { + } + else { PyErr_SetString(PyExc_TypeError, "Expects status type to be Int or String"); return nullptr; } @@ -275,31 +291,34 @@ PyObject* PropertyContainerPy::getPropertyStatus(PyObject *args) Py::List ret; const auto &statusMap = getStatusMap(); - if(!name[0]) { + if (!name[0]) { for(auto &v : statusMap) ret.append(Py::String(v.first.c_str())); - }else{ + } + else { App::Property* prop = getPropertyContainerPtr()->getPropertyByName(name); - if (prop) { + if (!prop) { + PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", name); + return nullptr; + } - auto linkProp = Base::freecad_dynamic_cast(prop); - if(linkProp && linkProp->testFlag(App::PropertyLinkBase::LinkAllowPartial)) - ret.append(Py::String("AllowPartial")); + auto linkProp = Base::freecad_dynamic_cast(prop); + if (linkProp && linkProp->testFlag(App::PropertyLinkBase::LinkAllowPartial)) + ret.append(Py::String("AllowPartial")); - std::bitset<32> bits(prop->getStatus()); - for(size_t i=1;i bits(prop->getStatus()); + for(size_t i=1; i(i)) { + ret.append(Py::String(v.first.c_str())); + found = true; + break; } - if(!found) - ret.append(Py::Int((long)i)); } + if (!found) + ret.append(Py::Int(static_cast(i))); } } return Py::new_reference_to(ret); @@ -312,6 +331,11 @@ PyObject* PropertyContainerPy::getEditorMode(PyObject *args) return nullptr; App::Property* prop = getPropertyContainerPtr()->getPropertyByName(name); + if (!prop) { + PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", name); + return nullptr; + } + Py::List ret; if (prop) { short Type = prop->getType(); @@ -357,7 +381,8 @@ PyObject* PropertyContainerPy::setGroupOfProperty(PyObject *args) } prop->getContainer()->changeDynamicProperty(prop,group,nullptr); Py_Return; - } PY_CATCH + } + PY_CATCH } @@ -373,9 +398,9 @@ PyObject* PropertyContainerPy::getDocumentationOfProperty(PyObject *args) return nullptr; } - const char* Group = getPropertyContainerPtr()->getPropertyDocumentation(prop); - if (Group) - return Py::new_reference_to(Py::String(Group)); + const char* docstr = getPropertyContainerPtr()->getPropertyDocumentation(prop); + if (docstr) + return Py::new_reference_to(Py::String(docstr)); else return Py::new_reference_to(Py::String("")); } @@ -395,7 +420,8 @@ PyObject* PropertyContainerPy::setDocumentationOfProperty(PyObject *args) } prop->getContainer()->changeDynamicProperty(prop,nullptr,doc); Py_Return; - } PY_CATCH + } + PY_CATCH } PyObject* PropertyContainerPy::getEnumerationsOfProperty(PyObject *args) @@ -411,13 +437,10 @@ PyObject* PropertyContainerPy::getEnumerationsOfProperty(PyObject *args) } PropertyEnumeration *enumProp = dynamic_cast(prop); - if (!enumProp) { - Py_INCREF(Py_None); - return Py_None; - } + if (!enumProp) + Py_Return; 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)); @@ -432,7 +455,7 @@ Py::List PropertyContainerPy::getPropertiesList(void) const getPropertyContainerPtr()->getPropertyMap(Map); - for (std::map::const_iterator It=Map.begin();It!=Map.end();++It) + for (std::map::const_iterator It=Map.begin(); It!=Map.end(); ++It) ret.append(Py::String(It->first)); return ret; @@ -442,12 +465,11 @@ Py::List PropertyContainerPy::getPropertiesList(void) const PyObject* PropertyContainerPy::dumpPropertyContent(PyObject *args, PyObject *kwds) { int compression = 3; - char* property; - static char* kwds_def[] = {"Property", "Compression",nullptr}; + const char* property; + static char* kwds_def[] = {"Property", "Compression", nullptr}; PyErr_Clear(); - if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|i", kwds_def, &property, &compression)) { + if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|i", kwds_def, &property, &compression)) return nullptr; - } Property* prop = getPropertyContainerPtr()->getPropertyByName(property); if (!prop) {