From 5cb31ece933631929a1b210b8cbeba736c2600fe Mon Sep 17 00:00:00 2001 From: marioalexis Date: Wed, 20 Jul 2022 01:16:27 -0300 Subject: [PATCH] App: Check Python types using Base::PyTypeCheck --- src/App/ApplicationPy.cpp | 13 ++++++------ src/App/DocumentObjectPyImp.cpp | 27 ++++++++++++------------ src/App/DocumentPyImp.cpp | 37 ++++++++++++++------------------- src/App/PropertyLinks.cpp | 23 ++++++-------------- 4 files changed, 41 insertions(+), 59 deletions(-) diff --git a/src/App/ApplicationPy.cpp b/src/App/ApplicationPy.cpp index 22b46350f9..105fa53580 100644 --- a/src/App/ApplicationPy.cpp +++ b/src/App/ApplicationPy.cpp @@ -845,21 +845,20 @@ PyObject *Application::sGetLinksTo(PyObject * /*self*/, PyObject *args) return nullptr; PY_TRY { + Base::PyTypeCheck(&pyobj, &DocumentObjectPy::Type, "Expect the first argument of type App.DocumentObject or None"); DocumentObject *obj = nullptr; - if(pyobj!=Py_None) { - if(!PyObject_TypeCheck(pyobj,&DocumentObjectPy::Type)) { - PyErr_SetString(PyExc_TypeError, "Expect the first argument of type document object"); - return nullptr; - } + if (pyobj) obj = static_cast(pyobj)->getDocumentObjectPtr(); - } + auto links = GetApplication().getLinksTo(obj,options,count); Py::Tuple ret(links.size()); int i=0; for(auto o : links) ret.setItem(i++,Py::Object(o->getPyObject(),true)); + return Py::new_reference_to(ret); - }PY_CATCH; + } + PY_CATCH; } PyObject *Application::sGetDependentObjects(PyObject * /*self*/, PyObject *args) diff --git a/src/App/DocumentObjectPyImp.cpp b/src/App/DocumentObjectPyImp.cpp index 415c38d037..810c0f33e0 100644 --- a/src/App/DocumentObjectPyImp.cpp +++ b/src/App/DocumentObjectPyImp.cpp @@ -581,31 +581,30 @@ PyObject* DocumentObjectPy::getLinkedObject(PyObject *args, PyObject *keywds) &PyBool_Type,&recursive,&pyMat,&PyBool_Type,&transform,&depth)) return nullptr; - Base::Matrix4D _mat; - Base::Matrix4D *mat = nullptr; - if(pyMat!=Py_None) { - if(!PyObject_TypeCheck(pyMat,&Base::MatrixPy::Type)) { - PyErr_SetString(PyExc_TypeError, "expect argument 'matrix' to be of type Base.Matrix"); - return nullptr; - } - _mat = *static_cast(pyMat)->getMatrixPtr(); - mat = &_mat; - } - PY_TRY { + Base::PyTypeCheck(&pyMat, &Base::MatrixPy::Type, "expect argument 'matrix' to be of type Base.Matrix"); + Base::Matrix4D _mat; + Base::Matrix4D *mat = nullptr; + if (pyMat) { + _mat = *static_cast(pyMat)->getMatrixPtr(); + mat = &_mat; + } + auto linked = getDocumentObjectPtr()->getLinkedObject( Base::asBoolean(recursive), mat, Base::asBoolean(transform), depth); - if(!linked) + if (!linked) linked = getDocumentObjectPtr(); auto pyObj = Py::Object(linked->getPyObject(),true); - if(mat) { + if (mat) { Py::Tuple ret(2); ret.setItem(0,pyObj); ret.setItem(1,Py::asObject(new Base::MatrixPy(*mat))); return Py::new_reference_to(ret); } + return Py::new_reference_to(pyObj); - } PY_CATCH; + } + PY_CATCH; } PyObject* DocumentObjectPy::isElementVisible(PyObject *args) diff --git a/src/App/DocumentPyImp.cpp b/src/App/DocumentPyImp.cpp index 42613447ef..697127c086 100644 --- a/src/App/DocumentPyImp.cpp +++ b/src/App/DocumentPyImp.cpp @@ -363,18 +363,13 @@ PyObject* DocumentPy::importLinks(PyObject *args) objs.push_back(static_cast(seq[i].ptr())->getDocumentObjectPtr()); } } - else if (obj == Py_None) { - // do nothing here - } - else if (!PyObject_TypeCheck(obj,&DocumentObjectPy::Type)) { - PyErr_SetString(PyExc_TypeError, - "Expect first argument to be either a document object or sequence of document objects"); - return nullptr; - } - else { - objs.push_back(static_cast(obj)->getDocumentObjectPtr()); - } - + else { + Base::PyTypeCheck(&obj, &DocumentObjectPy::Type, + "Expect first argument to be either a document object, sequence of document objects or None"); + if (obj) + objs.push_back(static_cast(obj)->getDocumentObjectPtr()); + } + if (objs.empty()) objs = getDocumentPtr()->getObjects(); @@ -385,7 +380,8 @@ PyObject* DocumentPy::importLinks(PyObject *args) for (size_t i=0;igetPyObject(),true)); return Py::new_reference_to(tuple); - } PY_CATCH + } + PY_CATCH } PyObject* DocumentPy::moveObject(PyObject *args) @@ -861,22 +857,21 @@ PyObject* DocumentPy::getLinksTo(PyObject *args) return nullptr; PY_TRY { + Base::PyTypeCheck(&pyobj, &DocumentObjectPy::Type, "Expect the first argument of type document object"); DocumentObject *obj = nullptr; - if (pyobj!=Py_None) { - if (!PyObject_TypeCheck(pyobj,&DocumentObjectPy::Type)) { - PyErr_SetString(PyExc_TypeError, "Expect the first argument of type document object"); - return nullptr; - } - obj = static_cast(pyobj)->getDocumentObjectPtr(); - } + if (pyobj) + obj = static_cast(pyobj)->getDocumentObjectPtr(); + std::set links; getDocumentPtr()->getLinksTo(links,obj,options,count); Py::Tuple ret(links.size()); int i=0; for (auto o : links) ret.setItem(i++,Py::Object(o->getPyObject(),true)); + return Py::new_reference_to(ret); - } PY_CATCH + } + PY_CATCH } Py::List DocumentPy::getInList() const diff --git a/src/App/PropertyLinks.cpp b/src/App/PropertyLinks.cpp index 2ef7b080ef..8bfb72f2d4 100644 --- a/src/App/PropertyLinks.cpp +++ b/src/App/PropertyLinks.cpp @@ -460,17 +460,13 @@ PyObject *PropertyLink::getPyObject() void PropertyLink::setPyObject(PyObject *value) { - if (PyObject_TypeCheck(value, &(DocumentObjectPy::Type))) { - DocumentObjectPy *pcObject = static_cast(value); + Base::PyTypeCheck(&value, &DocumentObjectPy::Type); + if (value) { + DocumentObjectPy *pcObject = static_cast(value); setValue(pcObject->getDocumentObjectPtr()); } - else if (Py_None == value) { - setValue(nullptr); - } else { - std::string error = std::string("type must be 'DocumentObject' or 'NoneType', not "); - error += value->ob_type->tp_name; - throw Base::TypeError(error); + setValue(nullptr); } } @@ -708,16 +704,9 @@ PyObject *PropertyLinkList::getPyObject() DocumentObject *PropertyLinkList::getPyValue(PyObject *item) const { - if (item == Py_None) { - return nullptr; - } - else if (PyObject_TypeCheck(item, &(DocumentObjectPy::Type))) { - return static_cast(item)->getDocumentObjectPtr(); - } + Base::PyTypeCheck(&item, &DocumentObjectPy::Type); - std::string error = std::string("type must be 'DocumentObject', list of 'DocumentObject', or NoneType, not "); - error += item->ob_type->tp_name; - throw Base::TypeError(error); + return item ? static_cast(item)->getDocumentObjectPtr() : nullptr; } void PropertyLinkList::Save(Base::Writer &writer) const